Owen wrote:
>> Hello All,
>>
>> This is surely a beginner's question and may even sound silly. :)
>>
>> How do you make a Perl script skip an input parameter if it is not
>> present.
>> Let me explain it through an example.
>>
>> EG:
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>> no warnings 'uninitialized';
>> print "1 - $ARGV[0]\n";
>> print "2 - $ARGV[1]\n";
>> print "3 - $ARGV[2]\n";
>>
>> When I execute this as - perl Input.pl one two three
>>
>> I get following O/P
>>
>> 1 - one
>> 2 - two
>> 3 - three
>>
>> Now suppose I don't want to give second parameter - perl Input.pl one
>> three
>>
>> I get following O/P:
>>
>> 1 - one
>> 2 - three
>> 3 -
>>
>> My requirement is -
>>
>> 1 - one
>> 2 -
>> 3 - three
> I know what you are asking, and I have seen the answer by Shlomi Fish
> but wonder if using Getopt::Std would not be another avenue to
> explore.
>
> Or for that matter, Getopt::Long
Yes.
use strict;
use warnings;
use Getopt::Long;
Getopt::Long::Configure qw( bundling );
use Pod::Usage;
my ( $month, $num_days, $help, $man );
my $result = GetOptions(
'month|m=s' => \$month,
'days|d=i' => \$num_days,
'help|h' => \$help,
'man|M' => \$man,
);
pod2usage({ -verbose => 1 }) if $help;
pod2usage({ -verbose => 2 }) if $man;
if ( $month ) {
do_something();
}
...etc
See perldoc Getopt::Long.
Steve
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/