SPUG: finding directory of script
dancerboy at strangelight.com
Thu Feb 14 22:23:13 CST 2002
At 7:24 pm -0800 2/14/02, Scott Blachowicz wrote:
>On Thu, Feb 14, 2002 at 06:12:05PM -0800, Daryn Nakhuda wrote:
>> Is there a best way to get the directory containing the script
>> you're running? or a way to use relative paths from that
>> directory to read files?
>> 1. pwd,cwd both return the directory you're running the script from
>> (working directory), not where it lives.
>> 2. using ./ or ../ is going to be relative to the working dir, not the
>> script's dir
>> 3. $0 could be used, but you'd have to parse for a ./ or no /, and in
>> those cases use the working dir.
>> I don't mind using #3, that would always work, right?
You might also want to take a look at the standard FindBin module,
which is supposed to do what you're trying to do (though IME it's not
quite as robust as it should be: e.g. it never seems to get the paths
right when my scripts are run as CGI...)
> (my $dir = $0) =~ s,(.*)/[^/]+$,$1,;
A little verbose, isn't this? Wouldn't
(my $dir = $0) =~ s,/[^/]+$,,;
do exactly the same thing, with less work? (And personally, I find
the shorter version easier to read...)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
More information about the spug-list