Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Perl progname

by hammopau (Novice)
on Aug 06, 2008 at 12:30 UTC ( [id://702619]=perlquestion: print w/replies, xml ) Need Help??

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 (Archbishop) 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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://702619]
Approved by Corion
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: (3)
As of 2024-07-23 14:58 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.