in reply to command line option with- or without argument

From Getopt::Long:
Using a colon ":" instead of the equals sign indicates that the option value is optional. In this case, if no suitable value is supplied, string valued options get an empty string '' assigned, while + numeric options are set to 0.

Update: Something like
use strict; use warnings; use Getopt::Long; my $foo; GetOptions ('foo:s', \$foo); print "foo: $foo\n" if defined($foo); print "Unprocessed by Getopt::Long\n" if $ARGV[0]; foreach (@ARGV) { print "$_\n"; }

Note that you should check for definedness rather than truth in $foo as the default values for optional parameters (0 and '') are false values.

Replies are listed 'Best First'.
Re^2: command line option with- or without argument
by Vasek (Acolyte) on Aug 29, 2009 at 14:01 UTC

    Dear james2vegas,

    Thanks a lot! i wouldn't thought that the keyword is so simple: "if defined($foo)"

    With the best