File Coverage

blib/lib/Debug/Simple.pm
Criterion Covered Total %
statement 12 45 26.6
branch 0 20 0.0
condition 0 41 0.0
subroutine 4 10 40.0
pod 5 5 100.0
total 21 121 17.3


line stmt bran cond sub pod time code
1             # Copyright (c) 2008 Behan Webster. All rights reserved. This program is free
2             # software; you can redistribute it and/or modify it under the same terms
3             # as Perl itself.
4              
5             package Debug::Simple;
6              
7 1     1   23023 use strict;
  1         2  
  1         43  
8             require Exporter;
9 1     1   1185 use Data::Dumper;
  1         11216  
  1         79  
10 1     1   14248 use Term::ANSIColor qw(:constants);
  1         19520  
  1         856  
11             $Term::ANSIColor::AUTORESET = 1;
12              
13 1     1   9 use vars qw(@EXPORT @ISA $VERSION);
  1         2  
  1         476  
14              
15             $VERSION = "0.10";
16             @ISA = qw(Exporter);
17             @EXPORT = qw(debuglevels warning debug verbose test);
18              
19             my $opt;
20              
21             ###############################################################################
22             sub debuglevels {
23             # Needs to have the keys: quiet, debug, verbose, test
24 0     0 1   ($opt) = @_;
25 0           &debug(5, "Command line options: ", OPTS => $opt);
26             }
27              
28             ###############################################################################
29             sub _list {
30 0 0   0     return (ref($_[0]) eq 'ARRAY') ? @{$_[0]} : @_;
  0            
31             }
32              
33             ###############################################################################
34             sub warning {
35 0 0   0 1   return if $opt->{quiet};
36             #warn ("Warning: ", @_);
37 0           print YELLOW ("Warning: ", @_);
38 0           print "";
39             }
40              
41             ###############################################################################
42             sub debug {
43 0 0   0 1   return if $opt->{quiet};
44 0           my @level = (&_list(shift), 0, 0, 0);
45              
46 0 0 0       return unless (defined $opt->{debug} && $level[0] <= $opt->{debug})
      0        
      0        
      0        
      0        
      0        
47             || ($opt->{verbose} && $level[1] && $level[1] <= $opt->{verbose})
48             || ($opt->{test} && $level[2]);
49              
50             #print "DEBUG\n" if defined $opt->{debug} && $level[0] <= $opt->{debug};
51             #print "VERBOSE: $opt->{verbose} && $level[1]\n" if defined $opt->{verbose} && $level[1] <= $opt->{verbose};
52             #print "TEST\n" if defined $opt->{test} && $level[3];
53              
54 0   0       my $str = shift || '';
55 0           my $name = shift;
56              
57 0 0 0       print BOLD "Debug: " unless $level[1] || $level[2];
58 0           print BOLD "$str";
59 0 0 0       if ($name && @_) {
60 0           my $save = $Data::Dumper::Varname;
61 0           $Data::Dumper::Varname = $name;
62 0           print BOLD Dumper(@_);
63 0           $Data::Dumper::Varname = $save;
64             }
65 0           print "";
66             }
67              
68             ###############################################################################
69             sub verbose {
70 0 0   0 1   return if $opt->{quiet};
71 0           my @level = (&_list(shift), 0, 0);
72              
73 0 0 0       return unless ($opt->{verbose} && $level[0] && $level[0] <= $opt->{verbose})
      0        
      0        
      0        
74             || ($opt->{test} && $level[1]);
75              
76 0           print @_;
77 0           print "";
78             }
79              
80             ###############################################################################
81             sub test {
82 0     0 1   my $code = shift;
83 0   0       my $str = shift || $code;
84              
85 0 0         if ($opt->{test}) {
86 0           print CYAN "$str\n";
87             } else {
88 0           eval $code;
89 0 0         print MAGENTA "Eval failed: $@\n" if $@;
90 0           return $@;
91             }
92 0           print "";
93             }
94              
95             1;
96             ###############################################################################
97             __END__