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

Greetings,

I was studying someone else's code and came across this line:

GetOptions( "refresh=i" => \ (my $REFRESH = 10), ...other option variables... );

I'm confused by the syntax of this line. It is nowhere to be found in the module documentation. According to the module, the "=" sign in "refresh=i" makes the option mandatory in the command line. So if that's the case, why does the argument above appear to be assigning a value to "$REFRESH"?

Respectfully Yours,

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot";
$nysus = $PM . $MCF;

Replies are listed 'Best First'.
Re: "Getoptions'" argument syntax
by wog (Curate) on Jun 15, 2001 at 23:36 UTC
    That code is another way of saying:

    my $REFRESH = 10; GetOptions( "refresh=i" => \$REFRESH, ...... );

    You can think of the my as both representing and declaring the lexical variable(s) in question.

    update: The reason it's a =i and not a :i is that the =i makes the number mandatory after the option, but not the option.

      OK, thanks. But still, if $REFRESH is required to be given a value from command line, why bother setting it to 10? Why not make the command optional with a ":". This is merlyn's code so I'm assuming a guru like that would have some logic behind this but maybe I'm wrong.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot";
      $nysus = $PM . $MCF;

        '=' means that: *If* you give the option you *must*(mandatory) give an argument. To give the option although is *optional*. ':' means you can give the option with or without an argument.
Re: "Getoptions'" argument syntax
by btrott (Parson) on Jun 16, 2001 at 00:15 UTC
    The '=' in 'refresh=i' doesn't mean that 'refresh' is a mandatory option to the program. It means that, if 'refresh' is supplied, the *integer argument* is mandatory. So you can't just have
    $ foo.pl --refresh
    An argument to 'refresh' is mandatory.

    I don't think there *is* a way to indicate that an option is mandatory (ie. that 'refresh' must be provided as an option).

    And since 'refresh' may not be provided, the

    $REFRESH = 10
    sets the default value for 'refresh' to 10. And it can be overriden on the command line with '--refresh'.
Re: "Getoptions'" argument syntax
by mikfire (Deacon) on Jun 16, 2001 at 00:08 UTC
    borris)cat getopt.pl #!usr/bin/perl -w use Getopt::Long; use strict; GetOptions( "refresh=i", \ (my $refresh = 10 ) ); print "refresh = $refresh\n"; borris)perl getopt.pl refresh = 10 borris)perl getopt.pl -refresh 5 refresh = 5
    Quick experimentation suggests that it provides a default value for an optional parameter. Which is really quite neat, and seems to be completely undocumented.

    mikfire

      ... seems to be completely undocumented.

      Getopt::Long's documentation does document it, though not directly (emphasis added):

      The call to GetOptions() parses the command line arguments that are present in "@ARGV" and sets the option variable to the value "1" if the option did occur on the command line. Otherwise, the option variable is not touched.
      It's just a little leap from that that you can take advantage of having an option's variable set before its passed to GetOptions, which is what the odd code given does.