Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Assignment to @INC in BEGIN block gives puzzling results

by jkeenan1 (Deacon)
on Dec 23, 2006 at 17:21 UTC ( [id://591454]=perlquestion: print w/replies, xml ) Need Help??

jkeenan1 has asked for the wisdom of the Perl Monks concerning the following question:

Suppose that I have a valid Perl module First.pm in a non-@INC directory. Then this program DWIMs:

#!/usr/local/bin/perl use strict; use warnings; use lib ("/Users/jimk/tmp/First/lib"); use First; print "Hello, world\n";

Now suppose I assign that directory to @INC in a BEGIN block, then print out the elements of @INC immediately after the BEGIN block, without use-ing First.pm:

#!/usr/local/bin/perl BEGIN: { unshift @INC, qq{/Users/jimk/tmp/First/lib}; } print "$_\n" for @INC; use strict; use warnings; print "Hello, world\n";

Again, that DWIMs:

/Users/jimk/tmp/First/lib /usr/local/lib/perl5/5.8.8/darwin-2level /usr/local/lib/perl5/5.8.8 ... . Hello, world

But when I attempt to use First, Perl can't find it!

#!/usr/local/bin/perl BEGIN: { unshift @INC, qq{/Users/jimk/tmp/First/lib}; } print "$_\n" for @INC; use strict; use warnings; use First; print "Hello, world\n";

Gives:

Can't locate First.pm in @INC (@INC contains: /usr/local/lib/perl5/5.8 +.8/darwin-2level /usr/local/lib/perl5/5.8.8 ... .) at inc.pl line 8. BEGIN failed--compilation aborted at inc.pl line 8.

What am I failing to understand about @INC?

Thank you very much.

Jim Keenan

Replies are listed 'Best First'.
Re: Assignment to @INC in BEGIN block gives puzzling results
by Corion (Patriarch) on Dec 23, 2006 at 17:30 UTC

    The problem is not with @INC but with "BEGIN" versus "LABEL:" : </c>

    perl -le "BEGIN: { print 'LABEL: block' }; BEGIN{ print 'BEGIN block'} + "

    Perl sees your BEGIN: and interprets it as a label - you sure meant it as that because you followed it with a colon (:)...

      Ah! The damage a single misplaced character can cause!

      Thanks, corion!

      Jim Keenan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://591454]
Approved by moklevat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found