Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Finding what args were passed to the perl interpretter ?

by dpuu (Chaplain)
on Nov 19, 2008 at 22:38 UTC ( [id://724752]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a way to figure out what command line args were passed to the perl interpreter (i.e. are not part of ARGV). On Linux I can do:
perl -le '$/ = "\0"; open my $fh, "/proc/$$/cmdline"; print for <$fh>'
which outputs:
/home/utils/perl-5.10/5.10.0-nothreads-64/bin/perl -le $/ = "\0"; open my $fh, "/proc/$$/cmdline"; print for <$fh>
However, this is not portable to systems that don't have the /proc file system. Is there a hook into perl itself to get this information? (specifically, I'm wanting any /-d:.*/ flags)
--Dave
Opinions my own; statements of fact may be in error.

Replies are listed 'Best First'.
Re: Finding what args were passed to the perl interpretter ?
by oko1 (Deacon) on Nov 19, 2008 at 23:05 UTC

    Well, you'd have to map the code that gets displayed back to the relevant commandline args, but you could use B::Deparse:

    ben@Tyr:~$ perl -MO=Deparse -wle# BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } -e syntax OK

    Note that the above 'BEGIN' statements differ from actual 'BEGIN' blocks: those are shown as 'sub BEGIN'.


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      Thanks for the reply. Unfortunately, that doesn't seem applicable to the problem I was trying to solve (which I guess I didn't state in my original post): the ultimate goal was for a script to detect that it was being run under -d:NYTProf, and to add that option (and other similar things) to various child perl-scripts that it runs.
      --Dave
      Opinions my own; statements of fact may be in error.
        I don't think there's a general way to get the arguments to perl from with Perl. However, an XS routine may be able to access argv and argc (although I've no idea whether perl itself leaves them as is).

        As for your specific request, detecting -d:NYTProf, it may be done. One of the effects of -d:foo is that it loads Devel::foo, and calls Devel::foo -> import. You may want to try to mask Devel::NYTProf::import.

Re: Finding what args were passed to the perl interpretter ?
by almut (Canon) on Nov 19, 2008 at 23:43 UTC

    (Update: I hadn't seen your follow-up note about the -d:NYTProf before posting this... If you need to do it from within the Perl code itself, this of course wouldn't work.)

    ___

    You could use the respective system call tracing tool, e.g. on Linux:

    $ strace -e execve -s 200 perl -le '$/ = "\0"; open my $fh, "/proc/$$/ +cmdline"; print for <$fh>' execve("/usr/local/bin/perl", ["perl", "-le", "$/ = \"\\0\"; open my $ +fh, \"/proc/$$/cmdline\"; print for <$fh>"], [/* 71 vars */]) = 0

    (add the option -f in a more complex setup, where perl is being called somewhere further down the fork-exec line...)

    Other systems have other system call tracers  (see this node for an attempt of mine to put together a list).

    This approach isn't really portable either... but you should at least be able to get it working somehow — which you otherwise couldn't if there's no /proc file system.

      How would a perl script attach an strace to itself in such a way that it can see its own invocation? If I simply do system "strace -p $$" then it's too late (the execve occurred in the past!)
      --Dave
      Opinions my own; statements of fact may be in error.

        Well, the idea would be to attach strace to whatever is calling perl... Why would you want to do it from within the script itself?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-03-29 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found