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 |