Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How To Get The File's Directory?

by Anonymous Monk
on Nov 19, 2009 at 14:44 UTC ( [id://808161]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a file, c:/temp/temp.pl
It has only 2 lines:
use Cwd; print getcwd;
Now, if I run it in c:/temp/, it gives "c:/temp".
If I run it in c:/, it gives "c:/".

How do I ensure that it always gives "c:/temp" (the location of the file itself) as the output, regardless of where I run the file?

Thanks in advance.

Replies are listed 'Best First'.
Re: How To Get The File's Directory?
by keszler (Priest) on Nov 19, 2009 at 14:52 UTC
    use File::Basename; print dirname $0;

    If you're willing to risk the regex:

    print $0 =~ /(.*)\\/; # or if you want the trailing backslash print $0 =~ /(.*\\)/;
      Both solutions are unreliable in some cases: The one with Class::Path relies on the assumption that no chdir has happened so far (which is reasonable at the top of a file, but not otherwise), and $0 is very platform specific.

      (I don't want to critique them, just point to potential problems)

      Other methods include FindBin in scripts and looking in %INC in modules. I'm sure both can be fooled too, though.

        Hi guys, thanks for the replies... Actually, $0 solves the problem, it always gives "c:/temp/temp.pl", regardless of where I run the scripts.

        I've tried running this scripts from various different directories, and I find that the only exception being, when I run it from "c:/temp/", then it'll give only "temp.pl", without any directory. Simply work around this exception will solve my problem at the moment.

        At this moment, I'm only working on the Windows platform, so I'll be safe, for now. :)

        Again, thanks a bunch, really appreciate it, guys!
        Time for a __DIR__ token?
Re: How To Get The File's Directory?
by Khen1950fx (Canon) on Nov 19, 2009 at 23:18 UTC
    I used File::Util. This script will get the cwd, strip and return the filename and path.

    #!/usr/bin/perl use strict; use warnings; use Cwd; use File::Util; my ($f) = File::Util->new(); print getcwd, "\n", $f->strip_path('/master/bin/perl5.10.0'), "\n", $f->return_path('/master/bin/perl5.10.0'), "\n";
Re: How To Get The File's Directory?
by Anonymous Monk on Nov 19, 2009 at 14:51 UTC
    use Path::Class; my $filedir = file(__FILE__)->absolute->dir;
Re: How To Get The File's Directory?
by skx (Parson) on Nov 20, 2009 at 06:43 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://808161]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-18 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found