File Coverage

blib/lib/Devel/Messenger.pm
Criterion Covered Total %
statement 87 98 88.7
branch 41 52 78.8
condition 27 37 72.9
subroutine 17 18 94.4
pod 0 1 0.0
total 172 206 83.5


line stmt bran cond sub pod time code
1             package Devel::Messenger;
2              
3 1     1   2091 use strict;
  1         3  
  1         59  
4 1     1   6 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK @trap);
  1         2  
  1         1163  
5              
6             require Exporter;
7              
8             @ISA = qw(Exporter);
9             @EXPORT_OK = qw(note);
10             @EXPORT = ();
11             $VERSION = '0.02';
12             local @trap = ();
13              
14             sub note {
15 1 50   1 0 36 return _initialize({}, shift, "Using Devel::Messenger version $VERSION\n", @_) if (ref($_[0]) eq 'HASH');
16 0         0 return '';
17             }
18              
19             sub _initialize {
20 18     18   24 my $prev = shift; # HASH ref
21 18         24 my $opts = shift; # HASH ref
22             # inherit from previous opts
23 18         48 foreach my $key (keys %$prev) {
24 51 100       178 $opts->{$key} = $prev->{$key} unless exists($opts->{$key});
25             }
26             # suppress version announcement
27 18 50       45 my $quiet = defined($opts->{quiet}) ? $opts->{quiet} : 0;
28 18 0 33     43 shift if ($quiet and @_ and substr($_[0], 0, 31) eq 'Using Devel::Messenger version ');
      33        
29             # output function to use
30 18   50     56 my $output = '_' . ($opts->{output} || 'none');
31             # filename or filehandle
32 18         25 my $file = '';
33 18 100 66     75 if (defined($opts->{output}) and ref($opts->{output})) {
  17 100       68  
34 1         2 $output = '_handle';
35 1         3 $file = $opts->{output};
36             } elsif (!defined(&{"Devel::Messenger::$output"})) {
37 1         2 $output = '_file';
38 1         3 $file = $opts->{output};
39             }
40             # level of debugging (0 for unlimited)
41 18 100 66     72 my $level = (defined($opts->{level}) and ($opts->{level} =~ m/^\d$/)) ? $opts->{level} : 1;
42             # prefix function for each line
43 18         26 my $prefix = '';
44 18   100     62 my $pkgname = $opts->{pkgname} || 0;
45 18   100     59 my $linenum = $opts->{linenumber} || 0;
46 18 100       53 if ($pkgname) {
    100          
47 2 100       6 if ($linenum) {
48 1         2 $prefix = '_prefix';
49             } else {
50 1         2 $prefix = '_prefix_name';
51             }
52             } elsif ($linenum) {
53 1         2 $prefix = '_prefix_line';
54             }
55             # text to wrap around each note
56 18   100     70 my ($begin, $end) = _wrapper($opts->{wrap} || '');
57             # globalize new subroutine definition?
58 18   50     65 my $global = $opts->{global} || 0;
59             # set up CODE ref to return
60             my $note = sub {
61 40 100   40   723 return _initialize($opts, @_) if (ref($_[0]) eq 'HASH');
62 23 100       45 my $debug = (ref($_[0]) eq 'SCALAR' ? ${shift()} : 1);
  3         6  
63 23 50       41 return '' if ($output eq '_none');
64 23 100 100     56 return '' if ($debug > $level and $level);
65 1     1   7 no strict 'refs';
  1         106  
  1         503  
66 22 100 100     2455 &$output($file, splice @trap) if (@trap and $output ne '_trap');
67 22         33 my $pre = $prefix;
68 22         34 my @message = grep { defined($_) } @_;
  23         66  
69 22 100 100     95 if (@message and $message[0] eq 'continue') {
70 1         2 shift @message;
71 1         2 $pre = '';
72             }
73 22 100       46 return '' unless @message;
74 20 100       59 chomp($message[$#message]) if (substr($end, -1, 1) eq "\n");
75 20 100       84 &$output($file, $begin, ($pre ? &$pre(caller) : ''), @message, $end);
76 18         99 };
77             # export subroutine
78 18 50       43 if ($global) {
79             #my $caller = (caller)[0];
80 0         0 foreach my $pkg (sort grep { $_ ne 'Devel/Messenger.pm' } 'main', keys %INC) {
  0         0  
81 0         0 (my $module = $pkg) =~ s/\.pm$//;
82 0         0 $module =~ s/\//::/g;
83 0 0       0 if (defined(&{"$module\::note"})) {
  0         0  
84 1     1   41 no strict 'refs';
  1         13  
  1         1048  
85             #undef &{"$module\::note"} unless ($module eq $caller);
86 0         0 *{"$module\::note"} = $note;
  0         0  
87             }
88             }
89             }
90             # note anything needful
91 18 100 66     76 &$note(@_) if (@_ or (@trap and $output ne '_trap'));
      66        
92 18         68 return $note;
93             }
94              
95             # --------------------------- N O T E - M A R K U P -------------------------- #
96              
97             sub _prefix {
98 1     1   2 my ($package, $filename, $line) = @_;
99 1         3 my ($pkgname) = _prefix_name($package, $filename, $line);
100 1         4 my ($linenum) = _prefix_line($package, $filename, $line);
101 1         4 return ($pkgname, ' '.$linenum, ': ');
102             }
103              
104             sub _prefix_name {
105 2     2   3 my ($package, $filename, $line) = @_;
106 2 50       9 return (($package eq 'main' ? $filename : $package), ': ');
107             }
108              
109             sub _prefix_line {
110 2     2   2 my ($package, $filename, $line) = @_;
111 2         8 return ("($line)", ': ');
112             }
113              
114             sub _wrapper {
115 18 100   18   46 if (ref($_[0]) eq 'ARRAY') {
116 4         4 return @{shift()};
  4         11  
117             } else {
118 14         18 my $wrapping = shift;
119 14         33 return ($wrapping, $wrapping);
120             }
121             }
122              
123             # ---------------------- O U T P U T - F U N C T I O N S --------------------- #
124              
125             sub _file {
126 1     1   4 my $file = shift;
127 1 50       87 if (open NOTE, ">>$file") {
128 1         15 print NOTE @_;
129 1         56 close NOTE;
130             } else {
131 0         0 warn "Cannot append to file $file: $!\n";
132             }
133             }
134              
135             sub _handle {
136 1     1   3 my $file = shift;
137 1         21 print $file @_;
138             }
139              
140 3     3   11 sub _print { local $| = 1; shift; print @_; }
  3         3  
  3         19  
141              
142 2     2   4 sub _warn { shift; warn @_; }
  2         18  
143              
144 12 50   12   13 sub _return { shift; return @_ if wantarray; join('', @_); }
  12         25  
  12         48  
145              
146 3     3   4 sub _trap { shift; push @trap, @_; return ''; }
  3         10  
  3         11  
147              
148 0     0     sub _none {}
149              
150             1;
151              
152             __END__