Hi syedtoah
There are a couple of probs with the given code
- There is a " missing from the help printage
- Getopt eats up the values in @ARGV so it is alway empty
- the exits test is always true as Getopt creates the hash keys
here is a fixed version that should work as expected.
#!/use/your/bin/perl -w
use strict;
unless (@ARGV) {&usage}
use Getopt::Long;
my %options;
my $ret = GetOptions(
"i" => \$options{i},
"o=s" => \$options{o},
"p=s" => \$options{p},
"h" => \$options{h}
);
if ($ret eq "" || $options{'h'}) {&usage}
if ($options{i}) {
print "i is enabled\n";
}
if ($options{o}) {
print "o is enabled file is $options{o}\n";
}
if ($options{p}) {
print "p is enabled value is $options{p}\n";
}
sub usage {
print " \nCommand line options:
-i interactive
-o file give a file
-h help
-p value personal\n";
exit;
}
Update
As Nkuvu points out Getopts only eats up the valid options from @ARGV, anything else will remain so the test for @ARGV will pass if there are any non-option values in the command line.
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.