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.
|