One of the beautiful switches in perl is the -s option.

At the top of your perl script, if you include the -s switch along with any other switches you need (like -w and possibly -T), i.e. #!/usr/bin/perl -sw then any command line options you use will be set as variables in your script.

The nice thing about this is that you no longer need to force users to put opt1 first, then opt2. Taken from the Camel book:

The following script prints "true" only when the script is invoked with a -foo switch.

#!/usr/bin/perl -s if ($foo) { print "true\n" }

If the switch is of the form -xxx=yyy, the $xxx variable is set to whatever follows the equals sign in that argument ("yyy" in this case). The following script prints "true" if and only if the script is invoked with a -foo=bar switch:

#!/usr/bin/perl -s if ($foo eq 'bar') { print "true\n" }

(Me again) So, you can now invoke your script with scriptname -opt2=value2 -opt1=value1 and the variables $opt1 and $opt2 would still be correctly set. If you left one of the values out, then that variable simply wouldn't be defined.

elbieelbieelbie


In reply to Re: (elbie) command line options by elbie
in thread command line options by Bladernr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.