http://www.perlmonks.org?node_id=40553

clemburg has asked for the wisdom of the Perl Monks concerning the following question: (files)

How do I get the full path to the script executing?

Originally posted as a Categorized Question.

  • Comment on How do I get the full path to the script executing?

Replies are listed 'Best First'.
Re: How do I get the full path to the script executing?
by l2kashe (Deacon) on Oct 01, 2004 at 14:23 UTC
    or even Cwd
    use stict; use Cwd qw(abs_path); my $path = abs_path($0);
      Coincidentally, that's what File::Spec uses underneath
Re: How do I get the full path to the script executing?
by clemburg (Curate) on Nov 08, 2000 at 21:28 UTC

    If you want only the directory part, use File::Basename, like this:

    #!/usr/bin/perl -w use strict; use File::Spec::Functions qw(rel2abs); use File::Basename; print dirname(rel2abs($0));

    Using a regex here is calling for trouble.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com reply

Re: How do I get the full path to the script executing?
by Dominus (Parson) on Nov 12, 2000 at 22:19 UTC
    Use the standard FindBin module:

    use FindBin '$Bin'; print "The script is located in $Bin.\n";

      Well, I've complained about this particular module before because I think the perverse way in which it was implemented is quite unfortunate because it makes it nearly impossible to use it for lots of useful things like finding other programs that might be on your PATH. (After all, it is called FindBin and $0 is a script, not a binary.)

      But when this question came up in chatterbox, I did some checking and it appears to me that the correct answer for the full path to the current script is simply:

      rel2abs( $0 )
      as another answer mentions.

      For those that don't know, the (perverse) FindBin module goes through some effort to search $ENV{PATH} trying to find your script. However, I can't come up with any situation in which the script would have been found on the $ENV{PATH} but that $0 would not contain the (full) path, so the module appears to be pretty useless.

      Now, the closely related problem of finding the full path to the current executable of, for example, a C program, is solved (as best as is possible) via a method very similar to what the FindBin module does. However, this isn't the same problem.

      Now, a C program's value of argv[0] can be whatever the invoking program wanted to pass to exec(), including something completely unrelated to the name of the executable file. For the common case where argv[0] is set to the base name of the executable, searching $ENV{PATH} makes sense. For other cases, it may be impossible to determine the path to the executable. But, for a Perl script, the value of $0 is set by Perl and Perl doesn't set it to strange values (so we don't have these cases where we can't determine the full path) and often sets it to the full path for you.

      Okay, I can think of two cases where $ENV{PATH} is searched in order to find a Perl script. First, the kernel (or any other program such as a shell or a web server) might search the path to find "script" and then, reading the "#!/usr/bin/perl" line of "script", it would then invoke "perl", telling it to read the script.

      So, the kernel (or other program) will run "perl", passing the name of the script to be executed. Will it expect "perl" to search the $ENV{PATH} in order to find the script? No. So it will pass the full path to the script (or a path relative to the current working directory if there is one and the program feels like doing that work -- which some of them do). "perl" will just set $0 to whatever it was given so we'll either get 1) the full path to the script or 2) a path relative to the current directory. We don't need to search $ENV{PATH}.

      Second, you can do "perl -S script" to tell "perl" to search $ENV{PATH}. What does $0 get set to in this case? Well, my testing on multiple platforms shows $0 gets set to the full path.

      So throw in the third case of the user doing "perl script" or "perl subdir/script" and you see that $0 is always set to either 1) the full path or 2) a path relative to the current directory. So FindBin can give you the wrong answer and goes to lots of work (in a perverse way) in order to do it.

      Did I mention that I think FindBin is perverse? (:

              - tye (but my friends call me "Tye")
        This reply is very late, but I think needs adding. rel2abs($0) will probably break if you are running setuid on a modern Unix. To avoid a race condition, these kernels do not pass the filename of a setuid script to its interpreter but rather a "virtual" filename (somewhere in /proc on Linux if I'm not mistaken) which is rigidly associated to the file the kernel opened. The filename Perl sees in that case and consequently the $0 value would bear no relation to the actual location of the script. (Of course, FindBin will not work any better in that case.)

        Makeshifts last the longest.

        Ahh. Suppose you call chdir before you call FindBin. I believe it will still work. But your rel2abs($0) will no longer be accurate.

        So, with the caveat that you cannot call chdir before rel2abs, you can use it in place of FindBin.

        -- Randal L. Schwartz, Perl hacker

        Looking around for a solution to the above problem I found:
        use Cwd 'abs_path'; my $scriptpath = abs_path($0);
        ..but then of course I explicitly wanted to follow any symlinks, so that I could place script & data in the same separate directory, put a symlink to the script in my $PATH, and have the script dig figure out where to read its data by following that link.

        See also:

        Has the perversion gone away with the new version 1.51 of FindBin? In perldelta for 5.16.0, it says:
        It no longer returns a wrong result if a script of the same name as the current one exists in the path and is executable.
Re: How do I get the full path to the script executing?
by clash (Novice) on Dec 03, 2000 at 10:34 UTC
    I think that that is overworked. When I used $ENV{'SCRIPT_NAME'} it gave me the full path (not URL) to the script being executed. But maybe thats just me...
Re: How do I get the full path to the script executing?
by clemburg (Curate) on Nov 08, 2000 at 21:19 UTC

    Do it like that:

    #!/usr/bin/perl -w use strict; use File::Spec::Functions qw(rel2abs); print rel2abs($0);

    Originally posted as a Categorized Answer.

      If you want only the directory part, use File::Basename, like this:

      #!/usr/bin/perl -w use strict; use File::Spec::Functions qw(rel2abs); use File::Basename; print dirname(rel2abs($0));

      Using a regex here is calling for trouble.

      Christian Lemburg
      Brainbench MVP for Perl
      http://www.brainbench.com

Re: How do I get the full path to the script executing?
by clemburg (Curate) on Nov 08, 2000 at 21:15 UTC

    Do it like this:

    #!/usr/bin/perl -w use strict; use File::Spec::Functions qw(rel2abs); print rel2abs($0);

    Originally posted as a Categorized Answer.

Re: How do I get the full path to the script executing?
by ccn (Vicar) on Oct 01, 2004 at 15:16 UTC

    Moduleless approach:

    use vars qw($Bin $Script); BEGIN { ($Bin, $Script) = split /([^\/\\]+)$/, $0 }

    Originally posted as a Categorized Answer.

Re: How do I get the full path to the script executing?
by tye (Sage) on Nov 13, 2000 at 00:18 UTC
Re: How do I get the full path to the script executing?
by clemburg (Curate) on Nov 08, 2000 at 21:28 UTC

    Re: How do I get the full path to the script executing?

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.