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.

In reply to Re^2: Poor man's logging by adrianh
in thread Poor man's logging by adrianh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.