[Columbus-pm] use lib and BEGIN @INC

Jonathan Hogue jon at hogue.org
Fri Nov 12 15:10:49 PST 2010


print "I'm a print\n";
BEGIN {
   say "main BEGIN";
}
use Foo;
use 5.10.1;

it does two passes. use and BEGIN are run as they appear. They are on
the same level. then everything else.

This should do

main BEGIN
I am Foo
I'm a print

(although, I don't have a perl here.)



2010/11/12 Len Jaffe <lenjaffe at jaffesystems.com>:
>
>
> On Fri, Nov 12, 2010 at 5:04 PM, Jonathan Hogue <jon at hogue.org> wrote:
>>
>> no, BEGIN runs at compile-time too.
>>
>> found my error... it wasn't related to the difference between the 2 at
>> all... I had a trailing space in my path name when doing the latter.
>>
>> On Fri, Nov 12, 2010 at 5:02 PM, Len Jaffe <lenjaffe at jaffesystems.com>
>> wrote:
>> > use executes at compile-time, BEGIN runs after compilation, so you're
>> > use-ing a module before you unshift its home to @INC.
>> >
>> > On Fri, Nov 12, 2010 at 4:45 PM, Jonathan Hogue <jon at hogue.org> wrote:
>> >>
>> >> What's the difference between
>> >> use lib "/somepath";
>> >> and
>> >> BEGIN{ unshift( @INC, "/somepath" ) };
>> >>
>> >> When I do
>> >> use DQ::SomeModule after the "use lib" approach, it works, but not
>> >> after the BEGIN block, even though when I dump @INC, it shows the
>> >> /somepath for both scenarios
>> >
>> > use executes at compile-time, BEGIN runs after compilation, so you're
>> > use-ing a module before you unshift its home to @INC.
>> >
>> > By the time you dump, the @INC arrays look the same, but the got there
>> > by a
>> > temporally different execution path
>
>
>
> BEGIN runs after use.
>
> main.pl
> ----------------
> use Foo;
> use 5.10.1;
> BEGIN {
>    say "main BEGIN";
> }
> --------
>
> Foo.pm
> -----------------------
> package Foo;
> use 5.10.1;
> say "I am Foo";
> 1;
> ----------------------------
> C:\perlstuff> perl main.pl
> I am Foo
> main BEGIN
> C:\perlstuff>
>
> the use executes before the BEGIN.
>
>
>
>
>
>
> --
> lenjaffe at jaffesystems.com   614-404-4214
> Scoutmaster Troop 156 - www.bsatroop156.org
> Proprietor: http://www.theycomewithcheese.com/ - An Homage to Fromage
>
> _______________________________________________
> Columbus-pm mailing list
> http://columbus.pm.org/
> Columbus-pm at pm.org
> http://mail.pm.org/mailman/listinfo/columbus-pm
>


More information about the Columbus-pm mailing list