Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Load module from local dir

by afoken (Canon)
on Sep 25, 2018 at 21:56 UTC ( #1223017=note: print w/replies, xml ) Need Help??


in reply to Re: Load module from local dir
in thread Load module from local dir

/usr/bin/env perl use strict; use warnings; BEGIN { unshift @INC, '~/perl5/lib/perl5'; } use Excel::Writer::XSLX;
/usr/bin/env perl use strict; use warnings; use Array::Contains; BEGIN { if(contains('--debug', \@ARGV)) { print("Development INC activated\n\n"); unshift @INC, '~/perl5/lib/perl5'; } } use Excel::Writer::XSLX;

Did you try your examples? I don't think so.

Both are missing a shebang (#!) in line 1 and would cause a syntax error. Plus, as shown in Load module from local dir, neither manipulating @INC nor use lib expand the tilde. unshift never should, and use lib doesn't, at least not in my Perl:

/home/alex>cd /tmp/ /tmp>echo 'package Foo;1;' > ~/Foo.pm /tmp>perl -E 'use Foo; say "ok"' Can't locate Foo.pm in @INC (you may need to install the Foo module) ( +@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib +64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /u +sr/share/perl5 .) at -e line 1. BEGIN failed--compilation aborted at -e line 1. /tmp>perl -E 'use lib "~"; use Foo; say "ok"' Can't locate Foo.pm in @INC (you may need to install the Foo module) ( +@INC contains: ~ /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/l +ib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 +/usr/share/perl5 .) at -e line 1. BEGIN failed--compilation aborted at -e line 1. /tmp>perl -E 'use lib "~/."; use Foo; say "ok"' Can't locate Foo.pm in @INC (you may need to install the Foo module) ( +@INC contains: ~/. /usr/local/lib64/perl5 /usr/local/share/perl5 /usr +/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl +5 /usr/share/perl5 .) at -e line 1. BEGIN failed--compilation aborted at -e line 1. /tmp>perl -E 'use lib $ENV{"HOME"}; use Foo; say "ok"' ok /tmp>perl -v This is perl 5, version 22, subversion 2 (v5.22.2) built for x86_64-li +nux-thread-multi Copyright 1987-2015, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. /tmp>

And by the way:

Saying

use lib LIST;

is almost the same as saying

BEGIN { unshift(@INC, LIST) }
(from lib)

Except that newer versions of lib do some additional fancy stuff with architecture-specific directories:

For each directory in LIST (called $dir here) the lib module also checks to see if a directory called $dir/$archname/auto exists. If so the $dir/$archname directory is assumed to be a corresponding architecture specific directory and is added to @INC in front of $dir. lib.pm also checks if directories called $dir/$version and $dir/$version/$archname exist and adds these directories to @INC.

Also, if can be used to conditionally load other modules, including lib:

/tmp>perl -E 'use if -f(qq[$ENV{"HOME"}/Foo.pm]), lib => $ENV{"HOME"}; + use Foo; say "ok"' ok /tmp>

Alexander

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

Replies are listed 'Best First'.
Re^3: Load module from local dir
by cavac (Priest) on Oct 02, 2018 at 06:43 UTC

    Damn! I know i shouldn't just copy&paste from my source repo without testing. Sorry!

    "For me, programming in Perl is like my cooking. The result may not always taste nice, but it's quick, painless and it get's food on the table."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2022-01-27 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (70 votes). Check out past polls.

    Notices?