in reply to Re: Poor man's logging
in thread Poor man's logging

More readable, but doesn't work (or works differently :-)

Have a test suite :-)

#! /usr/bin/perl use strict; use warnings; use Symbol qw(delete_package); use Test::More; my %variants = ( adrianh_original => q{ use constant DEBUG => do { my $p=__PACKAGE__; ",$ENV{DEBUG}," =~ m/,($p|all)(=(.*?))?,/s && ($2 ? $3 : 1) }; }, adrianh_no_warnings => q{ use constant DEBUG => do { my ($package, $debug) = (__PACKAGE__, $ENV{DEBUG} || ''); ",$debug," =~ m/,($package|all)(=(.*?))?,/s && ($2 ? $3 : 1) }; }, aristotle => q{ use constant DEBUG => do { my %dlv = map /^(.*)=(\d+)$/, split /,/, $ENV{DEBUG}; $dlv{__PACKAGE__} || $dlv{all} || 1; }; }, ); my %test_values = ( 'Foo::Bar' => 1, 'all' => 1, '' => '', 'bar' => '', undef => '', 'Foo::Bar=99,bar' => 99, 'bar,Foo::Bar=99', => 99, 'all=99,bar' => 99, 'bar,all=99', => 99, ); plan tests => scalar(keys(%test_values)) * scalar(keys(%variants)); while (my ($variant, $test_constant) = each %variants) { while (my ($debug, $expected) = each %test_values ) { delete_package('Foo::Bar'); if ($debug eq 'undef') { delete $ENV{'DEBUG'}; } else { $ENV{'DEBUG'} = $debug; }; eval qq{ package Foo::Bar; use strict; use warnings; $test_constant; main::is(DEBUG, \$expected, "\$variant: \$debug"); }; fail($@) if $@; }; };

Which produces

1..27 ok 1 - adrianh_original: ok 2 - adrianh_original: bar ok 3 - adrianh_original: Foo::Bar=99,bar Use of uninitialized value in concatenation (.) or string at (eval 7) +line 6. ok 4 - adrianh_original: undef ok 5 - adrianh_original: all=99,bar ok 6 - adrianh_original: bar,Foo::Bar=99 ok 7 - adrianh_original: bar,all=99 ok 8 - adrianh_original: all ok 9 - adrianh_original: Foo::Bar not ok 10 - aristotle: # Failed test ((eval 19) at line 11) # got: '1' # expected: '' not ok 11 - aristotle: bar # Failed test ((eval 21) at line 11) # got: '1' # expected: '' not ok 12 - aristotle: Foo::Bar=99,bar # Failed test ((eval 23) at line 11) # got: '1' # expected: '99' Use of uninitialized value in split at (eval 25) line 7. not ok 13 - aristotle: undef # Failed test ((eval 25) at line 11) # got: '1' # expected: '' ok 14 - aristotle: all=99,bar not ok 15 - aristotle: bar,Foo::Bar=99 # Failed test ((eval 29) at line 11) # got: '1' # expected: '99' ok 16 - aristotle: bar,all=99 ok 17 - aristotle: all ok 18 - aristotle: Foo::Bar ok 19 - adrianh_no_warnings: ok 20 - adrianh_no_warnings: bar ok 21 - adrianh_no_warnings: Foo::Bar=99,bar ok 22 - adrianh_no_warnings: undef ok 23 - adrianh_no_warnings: all=99,bar ok 24 - adrianh_no_warnings: bar,Foo::Bar=99 ok 25 - adrianh_no_warnings: bar,all=99 ok 26 - adrianh_no_warnings: all ok 27 - adrianh_no_warnings: Foo::Bar # Looks like you failed 5 tests of 27.

Replies are listed 'Best First'.
Re^3: Poor man's logging
by Aristotle (Chancellor) on Dec 12, 2002 at 20:31 UTC
    Didn't read close enough I guess - but the necessary changes are trivial.
    use constant DEBUG => do { my %dlv = map /^(.+?)(?:=(\d+))?$/, split /,/, $ENV{DEBUG} || ''; $dlv{(__PACKAGE__)} || exists $dlv{(__PACKAGE__)} || $dlv{all} || exists $dlv{all}; };

    Makeshifts last the longest.

      Nice :-) Much more readable than mine.