olivierg
olivierg

Reputation: 792

Getopt::Long & subroutines in GetOptions

is there a possiblity to pass arguments to subroutine called via getopt::long ? e.g i have this code calling &Salt when a user specifies script.pl -pandora argument on the commandline

GetOptions (            "domain=s"    => \$domain,
                        "pandora=s"   => \&Salt,
                        "reverse=s"   => \$reverse,
                        "help"        => \&Usage)
       or die(&Usage);

how do i get the argument to be passed to Salt ? tried a couple of things such as :

GetOptions (            "domain=s"    => \$domain,
                        "pandora=s"   => \&Salt($pandora),
                        "reverse=s"   => \$reverse,
                        "help"        => \&Usage)
       or die(&Usage);

or even

    GetOptions (            "domain=s"    => \$domain,
                            "pandora=s"   => \&Salt($_[1]),
                            "reverse=s"   => \$reverse,
                            "help"        => \&Usage)
       or die(&Usage);

but it won't work

i know i can make it working by doing => $pandora, then using a condition in the code that says if ($pandora) { &Salt($pandora) } but i'll find it nicer to put the sub directly in getOptions if possible

thanks

Upvotes: 3

Views: 319

Answers (1)

jhnc
jhnc

Reputation: 16662

"pandora=s" => sub { my ($optname, $optval) = @_; Salt($optval) },

It can handle hashes too.

See the User-defined subroutines to handle options section in the documentation.

Upvotes: 6

Related Questions