I have a module misc with variable $verbose:
use strict;
use diagnostics;
package misc;
my $verbose = 1;
and module mymod which uses misc:
use strict;
use diagnostics;
use misc;
package mymod;
sub mysub ($) {
...
($misc::verbose > 0) and print "verbose!\n";
}
which is, in turn, used by myprog:
use strict;
use diagnostics;
use misc;
use mymod;
mymod::mysub("foo");
when I execute myprog, I get this warning:
Use of uninitialized value $misc::verbose in numeric gt (>) at mymod.pm line ...
what am I doing wrong?
In
mymod.pmyou should be using:instead of:
The warning is because
$misc::verbosetries to access the package variable$verbosein themiscpackage, which incidentally, is not declared.The
myfunction creates a lexically scoped variable. In this case, you require a package scoped variable, which is created by using theourfunction.Please pay attention to daxim‘s comment.