SPUG: finding directory of script

Richard Anderson richard at richard-anderson.org
Sat Feb 16 11:16:34 CST 2002


Your method fails in the sense that it does not always return the full
pathname to the script's directory.  Given the script getdir.pl:
#! /usr/bin/perl
use Cwd;
($dir = $0) =~ s,(.*)/[^/]+$,$1,;
print "Script directory: ", $dir, "\n";
unless ($dir =~ /^\//) {
    $dir = getcwd() . '/' . $dir; # Prefix current working directory
    $dir =~ s/\/\.$//;            # Strip trailing /.
    $dir =~ s#/\.\./#/#g;         # Not necessary, but aesthetically better
}
print "Better script directory: ", $dir, "\n";

Here are the results on Linux:
$ cd $HOME
$ cd bin
$ getdir.pl
Script directory: /home/s/starfire/bin
Better script directory: /home/s/starfire/bin
$ ./getdir.pl
Script directory: .
Better script directory: /home/s/starfire/bin
$ cd ../misc
$ ../bin/getdir.pl
Script directory: ../bin
Better script directory: /home/s/starfire/misc/bin

If all you want is a relative pathname, your method is fine.  However, both
our methods fails completely on Windows and MacOS.  My recommendation is
still to use the CPAN module FindBin.

Cheers,
Richard
richard at richard-anderson.org
www.richard-anderson.org
www.raycosoft.com
----- Original Message -----
From: "Scott Blachowicz" <scott at mail.dsab.rresearch.com>
To: <spug-list at pm.org>
Sent: Friday, February 15, 2002 6:21 PM
Subject: Re: SPUG: finding directory of script


> 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
>
>
>


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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