Can someone please explain why the following doesn't work?

#!/usr/bin/perl # use strict; use warnings; BEGIN { @INC = ( "/opt/app/lib" ); use Application::Module; }

It fails with:

Can't locate Application/Module.pm in @INC (@INC contains: /usr/lib/pe +rl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/ +5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-lin +ux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-mult +i /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/ +lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/per +l5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi + /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi /usr/lib/pe +rl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/vendor_p +erl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /u +sr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/ +lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib/perl5 +/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./test2.pl +line 8. BEGIN failed--compilation aborted at ./test2.pl line 8.

Both the following work:

#!/usr/bin/perl # use strict; use warnings; BEGIN { @INC = ( "/opt/app/lib" ); eval "use Application::Module"; }
#!/usr/bin/perl # use strict; use warnings; BEGIN { @INC = ( "/opt/app/lib" ); } use Application::Module;

What I really want is:

#!/usr/bin/perl # use strict; use warnings; BEGIN { local @INC = ( "/opt/app/lib" ); use Application::Module; }

I can add the 'eval' to make it work but I don't understand why it is necessary which bothers me no end...

update: BEGIN and compile-time has an explanation that I think I understand (I finally found the right super search!!). I hadn't contemplated nested BEGIN blocks before. My thinking to date on "compile time" versus "run time" has obviously been too simplistic.

This made me curious about when various nested bits run, so I have begun to experiment with the likes of the following...

#!/usr/bin/perl # use strict; use warnings; BEGIN { print "here we are in our begin block\n"; eval { print "here we are in our eval block\n"; INIT { print "here we are in an init block in an eval\n"; } BEGIN { print "here we are in a BEGIN block in an eval in a BEGIN +block\n"; } }; BEGIN { print "here we are in our nested begin block\n"; } } CHECK { print "here we are in our check block\n"; } INIT { print "here we are in our init block\n"; }

Which produces:

here we are in a BEGIN block in an eval in a BEGIN block here we are in our nested begin block here we are in our begin block here we are in our eval block here we are in our check block here we are in an init block in an eval here we are in our init block

Most curious!! No doubt, when I come around to the Perl way of thinking it will seem quite correct.

update2: dufus me! I used an eval block when I wanted an eval string. Using a string makes the order of execution more what I expected (i.e. "run time" (relatively speaking) execution of the evaluated string.

#!/usr/bin/perl # use strict; use warnings; BEGIN { print "here we are in our begin block\n"; eval ' print "here we are in our eval block\n"; INIT { print "here we are in an init block in an eval\n"; } BEGIN { print "here we are in a BEGIN block in an eval in a BEGIN +block\n"; } '; BEGIN { print "here we are in our nested begin block\n"; } } CHECK { print "here we are in our check block\n"; } INIT { print "here we are in our init block\n"; }

Produces:

here we are in our nested begin block here we are in our begin block here we are in a BEGIN block in an eval in a BEGIN block here we are in our eval block here we are in our check block here we are in an init block in an eval here we are in our init block

In reply to Changing @INC before use'ing a module in a BEGIN block by ig

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.