in reply to How To Get The File's Directory?

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 =~ /(.*\\)/;

Replies are listed 'Best First'.
Re^2: How To Get The File's Directory?
by moritz (Cardinal) on Nov 19, 2009 at 15:15 UTC
    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?