SPUG: finding directory of script
Scott Blachowicz
scott at mail.dsab.rresearch.com
Fri Feb 15 20:21:45 CST 2002
On Thu, Feb 14, 2002 at 10:43:51PM -0800, Richard Anderson wrote:
> FindBin is the way to go. Scott's and dancerboy's code fails when the
> script is invoked using a relative pathname, e.g., cd $HOME;
> bin/myscript.pl.
In what way does it "fail"? It seems to work fine for me (given the caveat I
mentioned about not doing any chdir type stuff). And I never knew about
FindBin before...and yes, that "s" command of mine looks pretty stupid (don't
know how that evolved to the way it was).
Scott
> You could try further hacking on $0, e.g.
>
> use Cwd;
> ($dir = $0) =~ s,(.*)/[^/]+$,$1,;
> unless ($dir =~ /^\//) {
> $dir = getcwd() . '/' . $dir; # Prefix current working
> directory to relative pathname
> $dir =~ s/\/\.$//; # Strip trailing /.
> $dir =~ s#/\.\./#/#g; # Not necessary, but
> aesthetically better
> }
>
> but this flunks on Windows and other OSes. Use FindBin.
>
> Cheers,
> Richard
> richard at richard-anderson.org
> www.richard-anderson.org
> www.raycosoft.com
> ----- Original Message -----
> From: "dancerboy" <dancerboy at strangelight.com>
> To: "Scott Blachowicz" <scott at mail.dsab.rresearch.com>; "Daryn Nakhuda"
> <daryn at marinated.org>
> Cc: <spug-list at pm.org>
> Sent: Thursday, February 14, 2002 8:23 PM
> Subject: Re: SPUG: finding directory of script
>
>
> > 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...)
> >
> > -jason
> >
> > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> > 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
> >
> >
> >
>
--
Scott Blachowicz
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
mailing list