File Coverage

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


line stmt bran cond sub pod time code
1             package Debug::CodeBlock;
2 3     3   172853 use 5.006; use strict; use warnings; our $VERSION = '0.04';
  3     3   30  
  3     3   13  
  3         5  
  3         70  
  3         14  
  3         6  
  3         133  
3 3     3   17 use base 'Import::Export';
  3         4  
  3         1270  
4 3     3   45290 use Data::Dumper qw//;
  3         17221  
  3         257  
5             our %EX = ( DEBUG => [qw/all/] );
6              
7             our %PACKAGES;
8             sub DEBUG (&) {
9 6     6 1 1122 my $code = shift;
10 6         15 my ($package) = caller;
11 6 100 100     48 if (($package->can('DEBUG_ENABLED') && $package->DEBUG_ENABLED) or $ENV{DEBUG_PERL}) {
      100        
12 4 100       20 unless ($PACKAGES{$package}++) {
13 3     3   19 no strict 'refs';
  3         6  
  3         265  
14 2         5 *{"${package}::Dump"} = \&{"Data::Dumper::Dumper"};
  2         10  
  2         10  
15             }
16 4         9 $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.04
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