| 1 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Net::IMP::Debug; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | our @EXPORT = qw($DEBUG debug); | |||||||||||||||||||||||||||
| 7 | 8 | our @EXPORT_OK = qw($DEBUG_RX set_debug); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 8 | our $DEBUG = 0; # is debugging enabled at all | |||||||||||||||||||||||||||
| 10 | 8 | our $DEBUG_RX = undef; # debug only packages matching given regex | |||||||||||||||||||||||||||
| 11 | 8 | my $DEBUG_SUB = undef; # external debug function to call instead of internal | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # import exported stuff, special case var => \$debug, func => \$code | ||||||||||||||||||||||||||||
| 14 | sub import { | ||||||||||||||||||||||||||||
| 15 | # on default import go immediately to Exporter | ||||||||||||||||||||||||||||
| 16 | 39 | ▶ | goto &Exporter::import if @_ == 1; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=32.3
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # extract var => \$var from import and alias it to DEBUG | ||||||||||||||||||||||||||||
| 19 | # extract sub => \$code and set $DEBUG_SUB | ||||||||||||||||||||||||||||
| 20 | 8 | ▶ | for(my $i=1;$i<@_-1;$i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if ( $_[$i] eq 'var' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 22 | 0 | *DEBUG = $_[$i+1]; | |||||||||||||||||||||||||||
| 23 | 0 | splice(@_,$i,2); | |||||||||||||||||||||||||||
| 24 | 0 | $i-=2; | |||||||||||||||||||||||||||
| 25 | } elsif ( $_[$i] eq 'rxvar' ) { | ||||||||||||||||||||||||||||
| 26 | 0 | *DEBUG_RX = $_[$i+1]; | |||||||||||||||||||||||||||
| 27 | 0 | splice(@_,$i,2); | |||||||||||||||||||||||||||
| 28 | 0 | $i-=2; | |||||||||||||||||||||||||||
| 29 | } elsif ( $_[$i] eq 'sub' ) { | ||||||||||||||||||||||||||||
| 30 | 0 | $DEBUG_SUB = $_[$i+1]; | |||||||||||||||||||||||||||
| 31 | 0 | splice(@_,$i,2); | |||||||||||||||||||||||||||
| 32 | 0 | $i-=2; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # call Exporter only if we have remaining args, because we don't | ||||||||||||||||||||||||||||
| 37 | # want to trigger the default export if user gave args (e.g. var,sub) | ||||||||||||||||||||||||||||
| 38 | 8 | ▶ | goto &Exporter::import if @_>1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # set debugging properties | ||||||||||||||||||||||||||||
| 42 | sub set_debug { | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $DEBUG = $_[1] if @_>1 && defined $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
set_debug: not called CC=4 SCAR=30.0
Pod
documented
Branch: 1 branch
Condition0%0/3
@_ > 1 and defined $_[1]
MC/DC0%0/2
@_ > 1 and defined $_[1]
@_ > 1
defined $_[1]
Truth table0%0/3
A and B
A= @_ > 1B= defined $_[1]
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $DEBUG_RX = $_[2] if @_>2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # print out debug message or forward to external debug func | ||||||||||||||||||||||||||||
| 48 | sub debug { | ||||||||||||||||||||||||||||
| 49 | 293 | ▶ | $DEBUG or return; | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=11 SCAR=47.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | my $msg = shift; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | $msg = sprintf($msg,@_) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ( $DEBUG_SUB ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | @_ = ($msg); | |||||||||||||||||||||||||||
| 54 | # goto foreign debug sub | ||||||||||||||||||||||||||||
| 55 | # if DEBUG_RX is set this ill be done later | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | goto &$DEBUG_SUB if ! $DEBUG_RX; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my ($pkg,$line) = (caller(0))[0,2]; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( $DEBUG_RX ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $pkg ||= 'main'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$pkg ||= "main"
MC/DC0%0/1
$pkg ||= "main"
$pkg
Truth table0%0/2
A
A= $pkg
| |||||||||||||||||||||||||||||
| 62 | # does not match wanted package | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return if $pkg !~ $DEBUG_RX; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | # goto foreign debug sub | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | goto &$DEBUG_SUB if $DEBUG_SUB; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $sub = (caller(1))[3]; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | $sub =~s{^main::}{} if $sub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $sub ||= 'Main'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$sub ||= "Main"
MC/DC0%0/1
$sub ||= "Main"
$sub
Truth table0%0/2
A
A= $sub
| |||||||||||||||||||||||||||||
| 71 | 0 | $msg = "${sub}[$line]: ".$msg; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | $msg =~s{^}{DEBUG: }mg; | |||||||||||||||||||||||||||
| 74 | 0 | print STDERR $msg,"\n"; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 8 | 1; | |||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||