SPUG: finding directory of script

Scott Blachowicz scott at mail.dsab.rresearch.com
Thu Feb 14 21:24:30 CST 2002


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?

I usually use something like this:

    BEGIN {
      (my $dir = $0) =~ s,(.*)/[^/]+$,$1,;
      push (@INC, $dir);
    }

Which should work unless you do chdir's in your perl.

Scott

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