Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getopt::Long & subroutines in GetOptions

Tags:

arguments

perl

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

like image 520
olivierg Avatar asked Dec 03 '25 14:12

olivierg


1 Answers

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

like image 140
jhnc Avatar answered Dec 05 '25 14:12

jhnc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!