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... |
| [reply] [d/l] [select] |
|
Unfornately, $ENV{_} doesn't work on Windows. Thanks though.
| [reply] |
|
| [reply] |
|
|
|
neat trick, but i get undef!
| [reply] [d/l] |
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
|
| [reply] |
|
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.
| [reply] |
|
$ perl -le'print $^X'
/usr/bin/perl
$ cp /usr/bin/perl ~/tmp/
$ ./tmp/perl -le'print $^X'
/home/rafl/tmp/perl
Cheers, Flo | [reply] [d/l] |
|
|
|
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.
| [reply] [d/l] [select] |
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
|
| [reply] |
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
|
I don't know if it works on windows, but on anything linuxy you could use:
print `whereis perl` . "\n";
Spidy
| [reply] [d/l] |
|
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!).
| [reply] [d/l] |
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... | [reply] [d/l] |
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. | [reply] |