File Coverage

blib/lib/Debug/CodeBlock.pm
Criterion Covered Total %
statement 25 25 100.0
branch 3 4 75.0
condition 4 6 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 40 43 93.0


line stmt bran cond sub pod time code
1             package Debug::CodeBlock;
2 2     2   112994 use 5.006; use strict; use warnings; our $VERSION = '0.03';
  2     2   13  
  2     2   11  
  2         3  
  2         57  
  2         8  
  2         4  
  2         78  
3 2     2   13 use base 'Import::Export';
  2         3  
  2         830  
4 2     2   29839 use Data::Dumper qw//;
  2         11397  
  2         171  
5             our %EX = ( DEBUG => [qw/all/] );
6              
7             our %PACKAGES;
8             sub DEBUG (&) {
9 2     2 1 992 my $code = shift;
10 2         7 my ($package) = caller;
11 2 100 66     15 if (($package->can('DEBUG_ENABLED') && $package->DEBUG_ENABLED) or $ENV{DEBUG_PERL}) {
      66        
12 1 50       9 unless ($PACKAGES{$package}++) {
13 2     2   13 no strict 'refs';
  2         4  
  2         194  
14 1         2 *{"${package}::Dump"} = \&{"Data::Dumper::Dumper"};
  1         4  
  1         5  
15             }
16 1         3 $code->();
17             }
18             }
19              
20             =head1 NAME
21              
22             Debug::CodeBlock - Add DEBUG codeblocks to your code.
23              
24             =head1 VERSION
25              
26             Version 0.03
27              
28             =cut
29              
30             =head1 SYNOPSIS
31              
32             Quick summary of what the module does.
33              
34             Use %ENV and set DEBUG_PERL=1
35              
36             package House;
37            
38             sub rooms {
39             my $rooms = model('House')->get_rooms();
40             DEBUG {
41             print Dump({ struct => { ... } });
42             print "The house has ${rooms} rooms.";
43             }
44             return $rooms;
45             }
46            
47              
48             ... or you can define a DEBUG_ENABLED function in your package
49              
50             package House;
51            
52             our $DEBUG = 1;
53              
54             sub DEBUG_ENABLED { $DEBUG }
55            
56             sub rooms {
57             my $rooms = model('House')->get_rooms();
58             DEBUG {
59             print Dump({ struct => { ... } });
60             print "The house has ${rooms} rooms.";
61             }
62             return $rooms;
63             }
64              
65              
66             =head1 EXPORT
67              
68             =head2 DEBUG
69              
70             A codeblock that is only called if DEBUG_PERL is set in %ENV or you have a DEBUG_ENABLED function which returns true. It's pupose is to add debugging logic to your code without it being executed in production.
71              
72             A Dump function, which is an alias for Data::Dumper::Dumper, is also imported IF debug is enabled and a DEBUG codeblock is found. NOTE: you must use parenthesis as it is imported at runtime.
73              
74             =cut
75              
76             =head1 AUTHOR
77              
78             LNATION, C<< >>
79              
80             =head1 BUGS
81              
82             Please report any bugs or feature requests to C, or through
83             the web interface at L. I will be notified, and then you'll
84             automatically be notified of progress on your bug as I make changes.
85              
86             =head1 SUPPORT
87              
88             You can find documentation for this module with the perldoc command.
89              
90             perldoc Debug::CodeBlock
91              
92             You can also look for information at:
93              
94             =over 4
95              
96             =item * RT: CPAN's request tracker (report bugs here)
97              
98             L
99              
100             =item * CPAN Ratings
101              
102             L
103              
104             =item * Search CPAN
105              
106             L
107              
108             =back
109              
110             =head1 ACKNOWLEDGEMENTS
111              
112             =head1 LICENSE AND COPYRIGHT
113              
114             This software is Copyright (c) 2022 by LNATION.
115              
116             This is free software, licensed under:
117              
118             The Artistic License 2.0 (GPL Compatible)
119              
120             =cut
121              
122             1; # End of Debug::CodeBlock