开发者

How does scoping work in Perl modules?

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜