Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^6: @INC from $0

by afoken (Chancellor)
on Jul 24, 2015 at 08:15 UTC ( [id://1136133]=note: print w/replies, xml ) Need Help??


in reply to Re^5: @INC from $0
in thread @INC from $0

BEGIN{($path = $0) =~ s[[\\/][^\\/]+$][];} BEGIN{ use lib $path;}

use implies BEGIN, so that BEGIN should not be needed.

There is a simpler way: use lib do { ... }, shown in Re^2: Calculations before using lib;. Combined with the s///r syntax from newer perls, this can be collapsed to use lib do { $0=~s[[\\/][^\\/]+$][]r };.

Also note that $0 may not always contain an absolute path:

>cat tmp.pl #!/usr/bin/perl use strict; use warnings; print "$0\n"; >chmod +x tmp.pl >perl tmp.pl tmp.pl >perl ./tmp.pl ./tmp.pl >perl /tmp/tmp.pl /tmp/tmp.pl >./tmp.pl ./tmp.pl >tmp.pl ./tmp.pl >

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2025-07-09 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.