export $debug and control warn output
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 9 Aug 2010 14:16:10 +0000 (16:16 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 9 Aug 2010 14:16:10 +0000 (16:16 +0200)
I have convention in which i use warn for debug output, and
prefix output with ## up to warning level

This then gets passed through $SIG{__WARN__} which filters
messages which I don't want to see.

lib/RFID/Biblio.pm

index 78341da..88014a4 100644 (file)
@@ -4,7 +4,7 @@ use warnings;
 use strict;
 
 use base 'Exporter';
-our @EXPORT = qw( hex2bytes as_hex hex_tag );
+our @EXPORT = qw( hex2bytes as_hex hex_tag $debug );
 
 use Data::Dump qw(dump);
 
@@ -16,7 +16,7 @@ RFID::Biblio - perl tools to use different RFID readers for library use
 
 our $VERSION = '0.02';
 
-my $debug = 0;
+our $debug = 0;
 
 
 =head1 DESCRIPTION
@@ -108,6 +108,24 @@ sub as_hex {
 
 sub hex_tag { uc(unpack('H16', shift)) }
 
+=head1 WARN
+
+We are installing L<perldoc/warn> handler to controll debug output
+based on C<$Biblio::RFID::debug> level
+
+=cut
+
+BEGIN {
+       $SIG{'__WARN__'} = sub {
+               my $msg = join(' ', @_);
+               if ( $msg =~ m/^(#+)/ ) {
+                       my $l = length $1;
+                       return if $l > $debug;
+               }
+               warn join(' ', @_);
+       }
+}
+
 =for readme continue
 
 =head1 HARDWARE SUPPORT