in reply to Re: removing debugging code at compile time ?
in thread removing debugging code at compile time ?

cool...now how to push this BEGIN{} block from the MyUtil.pm to the package which is using it, so I dont repeat the same thing in every package
Otherwise I have to do :
debug 'this' if MyUtils::LVL1;
which is ugly ;). Is this OK.
... eval "sub main::LVL$_ () {$debug{$_}}" ...

Replies are listed 'Best First'.
Re^3: removing debugging code at compile time ?
by Jenda (Abbot) on Jun 09, 2007 at 08:35 UTC

    If you define the constants in MyUtils.pm and only use them outside you do not need to do anything special. Define them any way you need and include them in the @EXPORT array. Something like

    package MyUtils; use base Exporter; our @EXPORT = qw(LVL1 LVL2 LVL3 debug); sub debug { print "$_\n" for @_; } my $count_of_levels = 3; our %debug = ( # fake initialization 1 => 1, 2 => 1, 3 => 0 ); for (1 .. $count_of_levels) { eval "sub LVL$_ () {$debug{$_}};"; } 1;
    and
    use strict; use MyUtils; print "Starting\n"; debug "blah 1" if LVL1; debug "blah 2" if LVL2; debug "blah 3" if LVL3; print "Ende\n";
    The reason you do not need to use a BEGIN block this way is that the code in MyUtils.pm outside any subroutines is evaluated before perl returns to the file containing the use statement and continues parsing it. So the LVLx constants do get defined in time.