in reply to how to make -h optional in GetOpt::Long

What you have pairs up 'h=s' with a reference to the result of evaluating error_detect("h"). Try

'h=s' => \&error_detect("h"),
(note the ampersand).

the lowliest monk

Replies are listed 'Best First'.
Re^2: how to make -h optional in GetOpt::Long
by ikegami (Patriarch) on Jun 02, 2005 at 04:16 UTC

    Adding the ampersand does nothing if you keep the parameter. I don't believe a sub is appropriate here anyway -- they're meant to help parsing -- and it shouldn't be h=s either.

    Getopt::Long::GetOptions( 'u=s' => \$user, 'p=s' => \$passwd, 's=s' => sub { local *_ = \$_[1]; /^([^:]+):(\d+)$/ or die("Invalid format for option s.\n"); $host = $1; $port = $2; }, 'help' => \$help, # Will also handle -h ) or error_detect("3 Invalid commmand line options."); if ($help) { die("usage: ...\n"); }

    We've already answered this question for him.

      but keeping every thing aside. how do u make a switch optional in GetOpt::Long? in STD you use a semi colon to denote that the switch is optional what do u do in Long? what if i have more than 2 switches which are optional? how do i express them as optional in Long?
        All switches are optional. (On that subject, the docs say "That's why they're called 'options'.") If you don't want a switch to be optional, you have to explicitely check to make sure a value was supplied, after calling GetOptions.