blib/lib/Devel/Messenger.pm

filter: Statementstmt 90.3% 94 / 104 Branchbran 80.7% 42 / 52 Conditioncond 75.6% 28 / 37 MC/DCmcdc 61.9% 13 / 21 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 1 totaltotal 83.2% 194 / 233 CC 43 SCAR 27.7
CC
43
Cov
85%
_initialize
29.2
__ANON__
13.0
note
2.5
CRAP 16.0
↑ Summary
1 package Devel::Messenger;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK @trap);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require Exporter;
7  
8 1 @ISA = qw(Exporter);
9 1 @EXPORT_OK = qw(note);
10 1 @EXPORT = ();
11 1 $VERSION = '0.02';
12 1 local @trap = ();
13  
14 1 ▶sub note {
Subroutine note: called CC=2 SCAR=9.2
Pod undocumented
15 1 ▶ return _initialize({}, shift, "Using Devel::Messenger version $VERSION\n", @_) if (ref($_[0]) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 1 0 1
16 0 return '';
17 }
18  
19 18 ▶sub _initialize {
Subroutine _initialize: called CC=26 SCAR=33.7
20 18 my $prev = shift; # HASH ref
21 18 my $opts = shift; # HASH ref
22 # inherit from previous opts
23 18 foreach my $key (keys %$prev) {
24 51 ▶ $opts->{$key} = $prev->{$key} unless exists($opts->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$key} 35 16 51
25 }
26 # suppress version announcement
27 18 ▶ my $quiet = defined($opts->{quiet}) ? $opts->{quiet} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'quiet'} ? : 0 18 18
28 18 ▶ shift if ($quiet and @_ and substr($_[0], 0, 31) eq 'Using Devel::Messenger version ');
Branch: 1 branch
BranchTrueFalseTotal
if $quiet and @_ and substr($_[0], 0, 31) eq "Using Devel::Messenger version " 0 18 18
Condition33%2/6
$quiet and @_ and substr($_[0], 0, 31) eq "Using Devel::Messenger version "
!ll&&!rl&&r
1800
$quiet and @_
!ll&&!rl&&r
1800
MC/DC0%0/3
$quiet and @_ and substr($_[0], 0, 31) eq "Using Devel::Messenger version "
$quiet @_ substr($_[0], 0, 31) eq "Using Devel::Messenger version "
Truth table25%1/4
A and B and C
A= $quietB= @_C= substr($_[0], 0, 31) eq "Using Devel::Messenger version "
ABCresult
0XX0
10X0
1100
1111
29 # output function to use
30 18 ▶ my $output = '_' . ($opts->{output} || 'none');
Condition50%1/2
$opts->{'output'} || 'none'
l!l
180
MC/DC0%0/1
$opts->{'output'} || 'none'
$opts->{'output'}
Truth table50%1/2
A
A= $opts->{'output'}
Aresult
11
00
31 # filename or filehandle
32 18 my $file = '';
33 18 ▶ if (defined($opts->{output}) and ref($opts->{output})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts->{'output'} and ref $opts->{'output'}) { } 1 17 18
elsif (not defined &{"Devel::Messenger::$output";}) { } 1 16 17
Condition66%2/3
defined $opts->{"output"} and ref $opts->{"output"}
!ll&&!rl&&r
0171
MC/DC50%1/2
defined $opts->{"output"} and ref $opts->{"output"}
defined $opts->{"output"} ref $opts->{"output"}
Truth table66%2/3
A and B
A= defined $opts->{"output"}B= ref $opts->{"output"}
ABresult
0X0
100
111
34 1 $output = '_handle';
35 1 $file = $opts->{output};
36 17 } elsif (!defined(&{"Devel::Messenger::$output"})) {
37 1 $output = '_file';
38 1 $file = $opts->{output};
39 }
40 # level of debugging (0 for unlimited)
41 18 ▶ my $level = (defined($opts->{level}) and ($opts->{level} =~ m/^\d$/)) ? $opts->{level} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'level'} && $opts->{'level'} =~ /^\d$/ ? : 6 12 18
Condition66%2/3
defined $opts->{"level"} and $opts->{"level"} =~ /^\d$/
!ll&&!rl&&r
1206
MC/DC50%1/2
defined $opts->{"level"} and $opts->{"level"} =~ /^\d$/
defined $opts->{"level"} $opts->{"level"} =~ /^\d$/
Truth table66%2/3
A and B
A= defined $opts->{"level"}B= $opts->{"level"} =~ /^\d$/
ABresult
0X0
100
111
42 # prefix function for each line
43 18 my $prefix = '';
44 18 ▶ my $pkgname = $opts->{pkgname} || 0;
Condition100%2/2
$opts->{'pkgname'} || 0
l!l
216
MC/DC100%1/1
$opts->{'pkgname'} || 0
$opts->{'pkgname'}
Truth table100%2/2
A
A= $opts->{'pkgname'}
Aresult
11
00
45 18 ▶ my $linenum = $opts->{linenumber} || 0;
Condition100%2/2
$opts->{'linenumber'} || 0
l!l
216
MC/DC100%1/1
$opts->{'linenumber'} || 0
$opts->{'linenumber'}
Truth table100%2/2
A
A= $opts->{'linenumber'}
Aresult
11
00
46 18 ▶ if ($pkgname) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pkgname) { } 2 16 18
elsif ($linenum) { } 1 15 16
47 2 ▶ if ($linenum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linenum) { } 1 1 2
48 1 $prefix = '_prefix';
49 } else {
50 1 $prefix = '_prefix_name';
51 }
52 } elsif ($linenum) {
53 1 $prefix = '_prefix_line';
54 }
55 # text to wrap around each note
56 18 ▶ my ($begin, $end) = _wrapper($opts->{wrap} || '');
Condition100%2/2
$opts->{'wrap'} || ''
l!l
513
MC/DC100%1/1
$opts->{'wrap'} || ''
$opts->{'wrap'}
Truth table100%2/2
A
A= $opts->{'wrap'}
Aresult
11
00
57 # globalize new subroutine definition?
58 18 ▶ my $global = $opts->{global} || 0;
Condition50%1/2
$opts->{'global'} || 0
l!l
018
MC/DC0%0/1
$opts->{'global'} || 0
$opts->{'global'}
Truth table50%1/2
A
A= $opts->{'global'}
Aresult
11
00
59 # set up CODE ref to return
60 40 ▶ my $note = sub {
Subroutine __ANON__: called CC=13 SCAR=25.7
61 40 ▶ return _initialize($opts, @_) if (ref($_[0]) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 17 23 40
62 23 ▶ my $debug = (ref($_[0]) eq 'SCALAR' ? ${shift()} : 1);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 3 20 23
63 23 ▶ return '' if ($output eq '_none');
Branch: 1 branch
BranchTrueFalseTotal
if $output eq "_none" 0 23 23
64 23 ▶ return '' if ($debug > $level and $level);
Branch: 1 branch
BranchTrueFalseTotal
if $debug > $level and $level 1 22 23
Condition100%3/3
$debug > $level and $level
!ll&&!rl&&r
2111
MC/DC100%2/2
$debug > $level and $level
$debug > $level $level
Truth table100%3/3
A and B
A= $debug > $levelB= $level
ABresult
0X0
100
111
65 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 22 ▶ &$output($file, splice @trap) if (@trap and $output ne '_trap');
Branch: 1 branch
BranchTrueFalseTotal
if @Devel::Messenger::trap and $output ne "_trap" 2 20 22
Condition100%3/3
@Devel::Messenger::trap and $output ne "_trap"
!ll&&!rl&&r
1912
MC/DC100%2/2
@Devel::Messenger::trap and $output ne "_trap"
@Devel::Messenger::trap $output ne "_trap"
Truth table100%3/3
A and B
A= @Devel::Messenger::trapB= $output ne "_trap"
ABresult
0X0
100
111
67 22 my $pre = $prefix;
68 22 my @message = grep { defined($_) } @_;
69 22 ▶ if (@message and $message[0] eq 'continue') {
Branch: 1 branch
BranchTrueFalseTotal
if (@message and $message[0] eq "continue") 1 21 22
Condition100%3/3
@message and $message[0] eq "continue"
!ll&&!rl&&r
2191
MC/DC100%2/2
@message and $message[0] eq "continue"
@message $message[0] eq "continue"
Truth table100%3/3
A and B
A= @messageB= $message[0] eq "continue"
ABresult
0X0
100
111
70 1 shift @message;
71 1 $pre = '';
72 }
73 22 ▶ return '' unless @message;
Branch: 1 branch
BranchTrueFalseTotal
unless @message 2 20 22
74 20 ▶ chomp($message[$#message]) if (substr($end, -1, 1) eq "\n");
Branch: 1 branch
BranchTrueFalseTotal
if substr($end, -1, 1) eq "\n" 3 17 20
75 20 ▶ &$output($file, $begin, ($pre ? &$pre(caller) : ''), @message, $end);
Branch: 1 branch
BranchTrueFalseTotal
$pre ? : 3 17 20
76 18 };
77 # export subroutine
78 18 ▶ if ($global) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global) 0 18 18
79 #my $caller = (caller)[0];
80 0 foreach my $pkg (sort grep { $_ ne 'Devel/Messenger.pm' } 'main', keys %INC) {
81 0 (my $module = $pkg) =~ s/\.pm$//;
82 0 $module =~ s/\//::/g;
83 0 ▶ if (defined(&{"$module\::note"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"${module}::note";}) 0 0 0
84 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 #undef &{"$module\::note"} unless ($module eq $caller);
86 0 *{"$module\::note"} = $note;
87 }
88 }
89 }
90 # note anything needful
91 18 ▶ &$note(@_) if (@_ or (@trap and $output ne '_trap'));
Branch: 1 branch
BranchTrueFalseTotal
if @_ or @Devel::Messenger::trap and $output ne "_trap" 3 15 18
Condition83%5/6
@_ or @Devel::Messenger::trap and $output ne "_trap"
l!l&&r!l&&!r
1215
@Devel::Messenger::trap and $output ne "_trap"
!ll&&!rl&&r
1502
MC/DC66%2/3
@_ or @Devel::Messenger::trap and $output ne "_trap"
@_ @Devel::Messenger::trap $output ne "_trap"
Truth table75%3/4
A or B and C
A= @_B= @Devel::Messenger::trapC= $output ne "_trap"
ABCresult
1XX1
0111
00X0
0100
92 18 return $note;
93 }
94  
95 # --------------------------- N O T E - M A R K U P -------------------------- #
96  
97 1 ▶sub _prefix {
Subroutine _prefix: called CC=1 SCAR=0.0
98 1 my ($package, $filename, $line) = @_;
99 1 my ($pkgname) = _prefix_name($package, $filename, $line);
100 1 my ($linenum) = _prefix_line($package, $filename, $line);
101 1 return ($pkgname, ' '.$linenum, ': ');
102 }
103  
104 2 ▶sub _prefix_name {
Subroutine _prefix_name: called CC=2 SCAR=7.2
105 2 my ($package, $filename, $line) = @_;
106 2 ▶ return (($package eq 'main' ? $filename : $package), ': ');
Branch: 1 branch
BranchTrueFalseTotal
$package eq 'main' ? : 2 0 2
107 }
108  
109 2 ▶sub _prefix_line {
Subroutine _prefix_line: called CC=1 SCAR=0.0
110 2 my ($package, $filename, $line) = @_;
111 2 return ("($line)", ': ');
112 }
113  
114 18 ▶sub _wrapper {
Subroutine _wrapper: called CC=2 SCAR=6.9
115 18 ▶ if (ref($_[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 4 14 18
116 4 return @{shift()};
117 } else {
118 14 my $wrapping = shift;
119 14 return ($wrapping, $wrapping);
120 }
121 }
122  
123 # ---------------------- O U T P U T - F U N C T I O N S --------------------- #
124  
125 1 ▶sub _file {
Subroutine _file: called CC=2 SCAR=7.4
126 1 my $file = shift;
127 1 ▶ if (open NOTE, ">>$file") {
Branch: 1 branch
BranchTrueFalseTotal
if (open NOTE, ">>$file") { } 1 0 1
128 1 print NOTE @_;
129 1 close NOTE;
130 } else {
131 0 warn "Cannot append to file $file: $!\n";
132 }
133 }
134  
135 1 ▶sub _handle {
Subroutine _handle: called CC=1 SCAR=0.0
136 1 my $file = shift;
137 1 print $file @_;
138 }
139  
140 3 ▶sub _print { local $| = 1; shift; print @_; }
Subroutine _print: called CC=1 SCAR=0.0
141  
142 2 ▶sub _warn { shift; warn @_; }
Subroutine _warn: called CC=1 SCAR=0.0
143  
144 12 ▶sub _return { shift; return @_ if wantarray; join('', @_); }
Subroutine _return: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 12 12
145  
146 3 ▶sub _trap { shift; push @trap, @_; return ''; }
Subroutine _trap: called CC=1 SCAR=0.0
147  
148 0 ▶sub _none {}
Subroutine _none: not called CC=1 SCAR=0.0
149  
150 1 1;
151  
152 __END__
↑ Summary