Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Perl command line switches

by Irakro1997 (Novice)
on Mar 27, 2020 at 02:27 UTC ( [id://11114699]=perlquestion: print w/replies, xml ) Need Help??

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

Hi I recently started using Perl scripting again after a very long time so I inherited a scile file that I am playing with to get to work while trying to understand it I just recalled a useful command but I can’t remember the name nor the exact usage so thought I would ask ...if you have inherited a Perl script that you quickly want to understand which arguments to pass on command line to generate out put files there is a command that you can use on the Perl script to get help on how to run that file or which arguments to pass to the script while running .... if that makes sense would someone here know these until commands ? Thanks

Replies are listed 'Best First'.
Re: Perl command line switches
by AnomalousMonk (Archbishop) on Mar 27, 2020 at 04:42 UTC
    ... I inherited a scile file ...

    I don't know what a "scile" file is.

    ... a command that you can use on the Perl script to get help on how to run that file or which arguments to pass to the script while running ...

    There is no such general utility of which I am aware (update: but see perldoc as noted by haukex). However, any well-designed, non-trivial script will | should IMHO have a "usage" or "help" message of some kind that might look something like

    use constant USAGE => <<"EOT"; usage: perl $0 -yz input_file_name output_file_name where: -y optional: command line switch to enable Y -z optional: command line switch to enable Z input_file_name required: input file name output_file_name required: output file name etc... EOT ... die USAGE unless @ARGV >= 2; ...
    (where  $0 is (more or less) the script name; see perlvar). If you can't find such code, then after examining the script to try to determine that it will do nothing irreversible, maybe try invoking the script with no command line arguments; this may trigger a usage/help message. There may also be a command line flag or switch like  -h --help -? /h /? or similar supported by the script. Maybe also look for the presence of modules Getopt::Std or Getopt::Long used to manage command line options and arguments and see what they're doing in the script.


    Give a man a fish:  <%-{-{-{-<

      I don't now what a "scile" file is.
      Neither do I, but I assume it's an accidental contraction of "script file" :-)
        [scile] ... an accidental contraction of "script file" ...

        Very likely, although Google did cough up slice file :)


        Give a man a fish:  <%-{-{-{-<

      Hi All thanks for trying to respond there is a typo instead of "scile file" i meant to say "script file" perl I have a piece of the code below I would like to know when running this code if the script name is convert.pl i tried to run like this below:
      example : perl convert.pl .?.?.? . how do i display the arguments list that are supposed to be passed to this script when running it to generate the correct output ?
      ###################################################################### ###################################################################### sub main { my $gen_public = 0; my $version = "0.1"; while ($arg = shift @ARGV) { $gen_public = 1 if ($arg eq "-p"); if ($arg eq "-v") { $version = shift @ARGV; $XLFILE =~ s/v0\.1/v$version/g; } } &print_tagec_xml_hdr(); require "../spr_uc_events.v$version.pl"; foreach my $box (@SPRBoxes) { printf(" <Uncore>\n <ProcessorArea name=\"%s\">\n", $box); &create_tagec_recs(\%SPR_UCEventList, $gen_public, $box); printf(" </ProcessorArea>\n </$area>\n"); } my $coreevents = "core_events.xml"; if (-f $coreevents) { open(COREXML, $coreevents ) || die("Cannot Open $coreevents"); printf while <COREXML> ; } &print_tagec_xml_tail(); }

        It seems that you want to find out how your (unformatted) snippet parses the command line:

        ###################################################################### ###################################################################### sub main { my $gen_public = 0; my $version = "0.1"; while ($arg = shift @ARGV) { $gen_public = 1 if ($arg eq "-p"); if ($arg eq "-v") { $version = shift @ARGV; $XLFILE =~ s/v0\.1/v$version/g; } } # .... rest elided }

        You run Perl scripts for example by using perl -w path/to/that/script.pl. Looking at the code, it looks for a -p switch or a -vswitch and sets some variables. All the values from the command line get stuffed into the @ARGV array, see perlvar on that.

Re: Perl command line switches
by haukex (Archbishop) on Mar 27, 2020 at 06:35 UTC

    In addition to AnomalousMonk's excellent post, note that some Perl scripts have embedded documentation (POD). You can access it via perldoc scriptname.pl, or simply by looking at the source code of the script.

Re: Perl command line switches
by davido (Cardinal) on Mar 27, 2020 at 19:56 UTC

    One of the things you may want to do as you take over ownership of this script could be to convert it to use Getopt::Long and Pod::Usage, and to write the POD so that it is compatible with pod2usage. You've got some work to do on understanding the script. But you can leave it in a better state when you're done with it.


    Dave

Re: Perl command line switches
by camtauxe (Sexton) on Mar 27, 2020 at 15:13 UTC

    Perl has a syntax for embedded documentation called POD (perlpod). It's possible that that's what you're thinking of. If the author of the original file was kind enough to include any of this documentation in the file, then you can read it on the command-line with the pod2text command. Just enter:

    pod2text <myscript.pl>

    And that will print out any POD that exists in the file.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-24 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found