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

Good morrow all.

I'd like to be able to determine what command line switches perl was run with. In the following, how could I print out -w and -MMath::Trig?

C:/>perl -w -MMath::Trig try.pl Name "main::old" used only once: possible typo at try.pl line 6. try.pl = [] Use of uninitialized value in print at try.pl line 6. C:/>cat try.pl local $" = q{], [}; local $| = 1; print "$0 = [@ARGV]\n"; print $old[1]; C:/>

Replies are listed 'Best First'.
Re: Reading command line switches
by BrowserUk (Patriarch) on Mar 16, 2011 at 02:21 UTC

    On windows, you can use an API to retrieve the command line used to start the current process. You can get at this API through either Inline::C or Win32::API:

    use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'junk', CLEAN_AFTER_BUILD => 0; SV *getCommandLine( ) { return newSVpv( GetCommandLine(), 0 ); } END_C print getCommandLine(); __END__ C:\test>getCmdLine.pl -x -P -Mfred /some=other "C:\Perl64\bin\perl.exe" "C:\test\getCmdLine.pl" -x -P -Mfred /some=o +ther

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Nice one! This works for me. Thank you very much.
Re: Reading command line switches
by ww (Archbishop) on Mar 16, 2011 at 01:58 UTC
    Please clarify: Do you mean print to <STDOUT>, a log, or dead trees? Is there some reason use of the mark-one eyeball (and perhaps, Win's command line editing capabilities) is inadequate?

    And what is the point of cating the pointless code in try.pl?

      I would like access to the switches, printing is irrespective. However, if it is of importance then STDOUT is a good place for it.

      cat was purely meant as an FYI to suggest that I did look in @ARGV before posting. In fact dare I say, it also suggests STDOUT, (negat|answer)ing your first question.

      The code will be executed by other people, and they might put any number of perl switches in on the command line which I would like to be able to determine.