ccn has asked for the wisdom of the Perl Monks concerning the following question:
Sometimes I need to use modules which placed in the same directory where the script is. I can't write use lib qw(./lib); because the script can be called from any current working directory.
Now I write:
use vars qw($lib); BEGIN {$lib = $0; $lib =~ s/[^\/\\]+$//} use lib $lib . 'lib';
Can it be done better?
UPDATE: Thanks to Aristotle for the nice snippet
use File::Spec::Functions qw( rel2abs catdir ); use File::Basename; use lib catdir( dirname( rel2abs $0 ), 'lib' );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: use lib qw(relative path)
by davorg (Chancellor) on Sep 25, 2004 at 11:52 UTC | |
by ccn (Vicar) on Sep 25, 2004 at 19:09 UTC | |
by ihb (Deacon) on Sep 25, 2004 at 23:02 UTC | |
by ccn (Vicar) on Sep 26, 2004 at 10:28 UTC | |
by davorg (Chancellor) on Sep 26, 2004 at 09:00 UTC | |
by ccn (Vicar) on Sep 26, 2004 at 10:23 UTC | |
by ccn (Vicar) on Sep 25, 2004 at 12:30 UTC | |
by Aristotle (Chancellor) on Sep 26, 2004 at 15:33 UTC | |
|
Re: use lib qw(relative path)
by amonroy (Scribe) on Sep 25, 2004 at 21:26 UTC | |
by amonroy (Scribe) on Sep 25, 2004 at 21:48 UTC | |
|
Re: use lib qw(relative path)
by Aristotle (Chancellor) on Sep 26, 2004 at 15:38 UTC | |
by ccn (Vicar) on Sep 26, 2004 at 20:24 UTC |