The following chunk facilitates default values for command-line arguments. It works fine, but I suspect there's a less redundant approach than just repeating "
if defined($opt_foo){$foo=$opt_foo} else {$foo=def_val}" for seven different values of "foo". I'd use hash keys and values, but there are three unique parameters for each iteration.
Suggestions, o wise brethren and sistren?
#!/usr/bin/perl -w
use strict;
use Getopt::Long
my (
$targ, $opt_targ,
$firstport, $opt_firstport,
$lastport, $opt_lastport,
$proto, $opt_proto,
$udptimeout, $opt_udptimeout,
$contimeout, $opt_contimeout,
$help,
);
GetOptions(
'targ=s' => \$opt_targ,
'firstport=s' => \$opt_firstport,
'lastport=s' => \$opt_lastport,
'proto=s' => \$opt_proto,
'udptimeout=s' => \$opt_udptimeout,
'contimeout=s' => \$opt_contimeout,
'help!' => \$opt_help,
);
if (defined $opt_targ){ $targ = $opt_targ }
else {$targ='localhost';}
if (defined $opt_firstport){ $firstport = $opt_firstport }
else {$firstport=1;}
if (defined $opt_lastport){ $lastport = $opt_lastport }
else {$lastport=1024;}
if (defined $opt_proto){ $proto = $opt_proto}
else {$proto='tcp';}
if (defined $opt_udptimeout){ $udptimeout = $opt_udptimeout }
else {$udptimeout=3;}
if (defined $opt_contimeout){ $contimeout = $opt_contimeout }
else {$contimeout=3;}
if (defined $opt_help){
Usage('You rang, sir?');
exit;
}
cheers,
Don
striving toward Perl Adept
(it's pronounced "why-bick")
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.