Question

I don't really understand how scoping works in Perl modules. This doesn't print anything. I would like it if running a.pl printed 1

b.pm

$f=1;

a.pl

use b;

print $f
Was it helpful?

Solution

OK, you have a lot of misconceptions that we can best address by fixing your immediate problem and pointing you to good resources.

b.pm should be:

package b;
our $f = 1;
1;

a.pl should be

use b;
print $b::f

run the whole thing with perl -I. a.pl

Now go read perldocperlmod very carefully.

Also read perldocstrict.

OTHER TIPS

You should start off by reading about Perl modules in the manual: perldoc perlmod at the commandline, or go to http://perldoc.perl.org/perlmod.html.

Short answer: Most probably because you're running this code on a case-insensitive file system, where asking for the module b loads the built-in module B. Your module is not getting loaded at all. If you rename b, you get the result you expect.

The longer answer included lots of chiding for failing to observe even minimal good practice, and has been elided.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top