use lib '.';

Jacinta Richardson jarich at perltraining.com.au
Wed Oct 2 20:54:27 CDT 2002


> > > Doesn't a Perl program automatically have access to programs
> > > in the current directory?

> Is the context such that '.' would ever be removed from @INC?
> That's the only reason I could see for "use lib '.'".

Nope pay attention to below.  The @INC is searched in order.

> [107]mryan at host:~> perl -e 'print join("\n", @INC), "\n"'
> /usr/libdata/perl/5.00503/mach
> /usr/libdata/perl/5.00503
> /usr/local/lib/perl5/site_perl/5.005/i386-freebsd
> /usr/local/lib/perl5/site_perl/5.005
> .

. is ONLY searched after everything else has been checked.

> [108]mryan at host:~> perl -e 'use lib "."; print join("\n", @INC), "\n"'
> .
> /usr/libdata/perl/5.00503/mach
> /usr/libdata/perl/5.00503
> /usr/local/lib/perl5/site_perl/5.005/i386-freebsd
> /usr/local/lib/perl5/site_perl/5.005
> .

Now . is checked first.
This means that if you've got your own version of CGI.pm (perhaps
because you can't get the sysadmin to upgrade theirs) and it's in the same
location as your script then your script will use your version.  Now
usually it's recommended that you created a separate directory for all
your library files, but you get the idea.

A less obvious problem that this fixes is if you call a library after a
system module you didn't know existed (for example "Config.pm").  If
you've added 
	use lib '.';
to your scripts you'll safely use YOUR Config.pm and all will be good.  If
you don't add the use lib, you'll start to wonder why your Config.pm isn't
being loaded.

	Jacinta

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |




More information about the Melbourne-pm mailing list