Saved has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use Getopt::Long ... perldoc.perl.org shows:
: number [ desttype ] Like :i , but if the value is omitted, the number will be assigned.
But my code fails. All help much appreciated.
GetOptions( "h|?"=>\$hflag, "Action=s"=>\$Action, "StartDate:0000000000"=>\$Start, "EndDate:9999999999"=>\$End ); print "Start = $Start and End = $End\n";
Thanx UPDATE: Sorry, the failure was the variables were not set. NetWallah's example showed me how to get it to work. I assumed just typing the script name would work, but when I added
ParseLdapAudLog.pl -s -e
The -s and -e , it worked fine Thanx

Replies are listed 'Best First'.
Re: Getopt::Long with default
by jethro (Monsignor) on Aug 27, 2012 at 14:09 UTC
    script --EndDate #outputs: Start = 0 and End = 9999999999

    If you just want a default value, just init your variable, i.e.

    my $End=999999999; GetOptions( ...

    PS: Error descriptions like "my code fails" are not very useful. Please give us a bit more info HOW it is not working next time

Re: Getopt::Long with default
by NetWallah (Canon) on Aug 27, 2012 at 14:09 UTC
    You have not specified how you think your code "fails".

    The problem may be semantics: When I run the code, i get:

    $ perl test-getopt.pl -Star=32323 -end Start = 32323 and End = 9999999999
    WITH the flag invoked, and no value given to the flag, it results in the default value specified in Getoptions.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: Getopt::Long with default
by daxim (Curate) on Aug 27, 2012 at 14:44 UTC
    Works for me.
    $ perl -MGetopt::Long=GetOptions 989989.pl --S --E Start = 0000000000 and End = 9999999999 $ perl -mGetopt::Long -E'say Getopt::Long->VERSION' 2.38