youwin has asked for the wisdom of the Perl Monks concerning the following question:
Oh wise and omniscient monks,
Im trying to understand why this works to import strict into the callers package:
# Foo.pm package Foo; sub import { strict->import; } # script use Foo; $yup = 5; # bang! (Global symbol "$yup" requires explicit...)
I know that normally to import package based modules, you can write something like this:
# Foo.pm package Foo; sub import { my $caller = caller; eval "package $caller; use Data::Dumper;"; } # script use Foo; print Dumper(\@ARGV); # $VAR1 = ...
Which makes sense because the use statement thinks it in the callers package. strict is strange because it imports into the lexical scope of the caller, and I thought that would be the block of the import routine in the first example above. So how does it know to enable strict for the callers lexical scope?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Import pragmas like strict and warnings into callers lexical scope
by ikegami (Patriarch) on Feb 11, 2011 at 19:44 UTC | |
by youwin (Beadle) on Feb 11, 2011 at 22:35 UTC | |
by ikegami (Patriarch) on Feb 11, 2011 at 23:12 UTC |