Thank you folks. This is more than what I wanted. Here is my version of the
same :  ;)


use strict;
use warnings;
use Getopt::Long;

my ( $name, $passion, $age);

my $result = GetOptions(
               'name|n=s' => \$name,
               'passion|p=s' => \$passion,
               'age|a=i' => \$age,
       );

if ($name) {
    print "My name is $name.\n";
}

if ($age){
    print "I am $age years old.\n";
}

if ($passion){
    print "I have passion for $passion.\n";
}


perl Getopt.pl -n Parag -a 24 -p Perl
My name is Parag.
I am 24 years old.
I have passion for Perl.


Bingo :)

Everytime I post something and I get to learn so much. :)

Cheers,
Parag




On Mon, Jan 11, 2010 at 7:41 PM, Steve Bertrand <st...@ibctech.ca> wrote:

> 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
>
>
>

Reply via email to