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

Hi, Is there a definative way of extracting the perl prog name ($0 can be reset/redefined)?

$^X produces the perl exe, not the calling perl prog (as does `pmap $$`).

This is on Solaris 8 or 10, perl5.8.8

Replies are listed 'Best First'.
Re: Perl progname
by cdarke (Prior) on Aug 06, 2008 at 13:03 UTC
    Not really. If someone went to the trouble of screwing $0, then they might also delete or mv the script as well. rename or unlink on $0 are both possible.
Re: Perl progname
by zentara (Cardinal) on Aug 06, 2008 at 14:41 UTC
    If you have the /proc filesystem, the most you can do is get the pid with $$, then check /proc/$pid/cmdline to get the full path to the executable.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      then check /proc/$pid/cmdline ...

      cmdline doesn't exist in Solaris /proc. Proc-ProcessTable does the trick on several platforms:

      use strict; use warnings; use Proc::ProcessTable; my $proc_table = Proc::ProcessTable->new->table; my ($process) = grep { $_->pid == $$ } @$proc_table; print $process->cmndline, "\n";
      /proc/$$/cmdline will reflect $0's modification, e.g.:
      $ perl -le '$0="foo"; print qx "cat /proc/$$/cmdline"' foo