http://qs321.pair.com?node_id=1223017


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". ;-)