Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: (elbie) command line options

by elbie (Curate)
on Aug 12, 2001 at 07:43 UTC ( [id://104208]=note: print w/replies, xml ) Need Help??


in reply to command line options

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (None)
    As of 2024-04-25 00:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found