Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to determine the path to the Perl binary that executed your program?

by Anonymous Monk
on Sep 28, 2006 at 19:34 UTC ( [id://575408]=perlquestion: print w/replies, xml ) Need Help??

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

From within my Perl script, I want to determine the location of the Perl binary that executed the script.

My first thought is to use File::Which. But this approach would fail if there is more than one Perl binary installed, or if Perl is not in the path.

Then I thought I could use Config.pm. However this approach also fails if the Perl binary was moved around after compilation.

Finally I thought I could read the script itself and get the path from the shebang line at the top. But this approach also fails since shebang lines are optional!

So now I have run out of thoughts -- And I come to you.

  • Comment on How to determine the path to the Perl binary that executed your program?

Replies are listed 'Best First'.
Re: How to determine the path to the Perl binary that executed your program?
by grep (Monsignor) on Sep 28, 2006 at 19:52 UTC
    I just confirmed on my system:
    print $ENV{_};
    works correctly, it will even report the soft link if used.

    UPDATE:
    To confirm setup a softlink (in *nix) ln -s /usr/bin/perl /usr/local/bin/perl.
    then try: /usr/local/bin/perl -e 'print "$ENV{_}\n"; print "$^X\n";'
    Output:

    /usr/local/bin/perl /usr/bin/perl


    grep
    Mynd you, mønk bites Kan be pretti nasti...
      Unfornately, $ENV{_} doesn't work on Windows. Thanks though.

        If you can ensure that perl is called with an explicit interpreter, either by fully qualifying the path or by using the Windows extension mechanism then $^X should be reliable.

        ---
        $world=~s/war/peace/g

      neat trick, but i get undef!
Re: How to determine the path to the Perl binary that executed your program?
by rafl (Friar) on Sep 28, 2006 at 19:37 UTC
      That gets its value from the config file. So that would fail for the same reasons given above for using Config.pm. After compilation, the Perl binary could be moved or renamed.

        Actually, it doesn't.

        $ perl -le'print $^X' /usr/bin/perl $ cp /usr/bin/perl ~/tmp/ $ ./tmp/perl -le'print $^X' /home/rafl/tmp/perl

        Cheers, Flo

        No, $^X comes from C's argv[0]. If perl was in the path, then it may just be "perl", in which case File::Which should track down the correct version. Or it could be a relative path, which can be resolved to a full path.
Re: How to determine the path to the Perl binary that executed your program?
by xdg (Monsignor) on Sep 28, 2006 at 22:50 UTC

    I use Probe::Perl. It's based on code from Module::Build.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: How to determine the path to the Perl binary that executed your program?
by Spidy (Chaplain) on Sep 28, 2006 at 23:17 UTC

      Well, some of us have more than one perl binary on our system. e.g., a recent thread where the question was how to upgrade perl, my suggestion, echoed by wazoox, was to leave the system perl alone, and compile the desired perl into a new location. (You can read that thread to see why, but it's not relevant to this node.)

      So, if you want to know which perl binary that executed your program, it could be the one in /usr/bin (the system perl), or it could be the one in /usr/local/bin (the local version). The one you find is going to be based on the PATH - does /usr/local/bin come before /usr/bin? However, the one that is actually used might be the one in the PATH (if you're running it by running "perl myscript"), or it might be the one in the shebang line, which could be either perl.

      In fact, I have had multiple perls installed: /usr/bin/perl, /usr/bin/perl5.8, /usr/bin/perl5.8.7, /usr/bin/perl5.8.8, all at the same time. Your "whereis perl" would get the wrong one because I nearly always used a shebang line for #!/usr/bin/perl5.8.8. But $^X got it right. Which is good - it helped when compiling modules (perl5.8.8 Makefile.PL would result in a Makefile that used the right perl!).

Re: How to determine the path to the Perl binary that executed your program?
by Gilimanjaro (Hermit) on Sep 29, 2006 at 12:19 UTC
    On Linux the following should and indeed seems to work:
    print readlink("/proc/$$/exe"),"\n";
    Probably won't work on any other OS though...
Re: How to determine the path to the Perl binary that executed your program?
by Anonymous Monk on Sep 30, 2006 at 01:04 UTC
    Interesting. When run under mod_perl, $^X gives the path to httpd! That makes sense, I guess. But it's not what I wanted.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-26 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found