|
Prev: Time::HiRes < 1.91 and glibc 2.4 incompatibility
Next: FAQ 9.1 What is the correct form of response from a CGI script?
From: PerlFAQ Server on 12 Apr 2008 09:03 This is an excerpt from the latest version perlfaq8.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 8.48: How do I add the directory my program lives in to the module/library search path? (contributed by brian d foy) If you know the directory already, you can add it to @INC as you would for any other directory. You might <use lib> if you know the directory at compile time: use lib $directory; The trick in this task is to find the directory. Before your script does anything else (such as a "chdir"), you can get the current working directory with the "Cwd" module, which comes with Perl: BEGIN { use Cwd; our $directory = cwd; } use lib $directory; You can do a similar thing with the value of $0, which holds the script name. That might hold a relative path, but "rel2abs" can turn it into an absolute path. Once you have the BEGIN { use File::Spec::Functions qw(rel2abs); use File::Basename qw(dirname); my $path = rel2abs( $0 ); our $directory = dirname( $path ); } use lib $directory; The "FindBin" module, which comes with Perl, might work. It searches through $ENV{PATH} (so your script has to be in one of those directories). You can then use that directory (in $FindBin::Bin) to locate nearby directories you want to add: use FindBin; use lib "$FindBin::Bin/../lib"; -------------------------------------------------------------------- The perlfaq-workers, a group of volunteers, maintain the perlfaq. They are not necessarily experts in every domain where Perl might show up, so please include as much information as possible and relevant in any corrections. The perlfaq-workers also don't have access to every operating system or platform, so please include relevant details for corrections to examples that do not work on particular platforms. Working code is greatly appreciated. If you'd like to help maintain the perlfaq, see the details in perlfaq.pod. |