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

Hi all, so I need to implement a script that somebody else wrote, but I can't figure out what they're doing with getopts.pl. Here's what's confusing me:

&Getopts('afg:s:n:vb:cd');

What does this mean? I tried to read about it and found something about ':' separating options that expect arguments or not, but the info I found wasn't very detailed. I can tell from the code which variables are which, but I would like to understand this line, and if it affects how I should call the script. Thanks in advance :)

EDIT: Okay, so I read Getopt::Std as suggested. I didn't realize the colon applied only to the letter before it, I thought they separated groups or something weird... This makes sense, however in the script they are assigning a variable to $opt_f, but isn't that option supposed to be a Boolean?

Replies are listed 'Best First'.
Re: getopts help
by jethro (Monsignor) on Aug 11, 2011 at 15:04 UTC

    See Getopt::Std which uses (AFAIK) the same syntax. In your case it means that $opt_g will be set to whatever argument g had. Same for s,n and b.

Re: getopts help
by Perlbotics (Archbishop) on Aug 11, 2011 at 15:10 UTC

    It means, that I feel sorry for you, because there's a Perl 4 smell ;-)
    Seriously, here's the header of my /usr/lib/perl5/5.10.0/getopts.pl:

    ;# getopts.pl - a better getopt.pl # # This library is no longer being maintained, and is included for back +ward # compatibility with Perl 4 programs which may require it. # # In particular, this should not be used as an example of modern Perl # programming techniques. # # Suggested alternatives: Getopt::Long or Getopt::Std # ;# Usage: ;# do Getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* a +s a ;# # side effect. sub Getopts { ...
    It is probably a good idea to port the program to use Getopt::Std or Getopt::Long (... there's probably a require "getopts.pl"; somewhere in your program that needs to be replaced) .

    Search for occurrences of $opt_* in your program and initialise them with a proper invocation of Getopt::Std::getopts() or Getopt::Long::GetOptions().

    Update:   &Getopts('afg:s:n:vb:cd'); means:

    flags:
    -a, -f, -v, -c, -d
    with argument:
    -g xxx, -s xxx, -n xxx, -b xxx

    Update2: Getopts::Long also allows you to use short flags, so you can keep the interface to your program backward compatible while beeing able to use long options at the same time (I guessed, what the switches might do!):

    $result = GetOptions ( "all" => \$opt_a, "force" => \$opt_f, "verbose" => \$opt_v, "check" => \$opt_c, "debug" => \$opt_d, "get=s" => \$opt_g, "source=s" => \$opt_s, "number=i" => \$opt_n, "backup=s" => \$opt_b );

Re: getopts help
by TomDLux (Vicar) on Aug 11, 2011 at 15:10 UTC

    Ooops, missed the mention of getopts.pl. The methods below still work ... the file begins with the following text. Note that Perl5 has been around since 1994 or thereabouts. Using Perl4 code is kinda like using a telephone with a crank which rings a bell to get the operator's attention.

    # This library is no longer being maintained, and is included for back +ward # compatibility with Perl 4 programs which may require it.

    Your program has a use XXX line for some options module which provides that Getopts(). You can use perldoc Module::Name in a terminal window, or browse to CPAN.org and search for the module name, or you can google the name, or perhaps your IDE will show you the documentation.

    Read it.

    Come back here if there's anything it doesn't explain.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: getopts help
by toolic (Bishop) on Aug 11, 2011 at 15:25 UTC
    however in the script they are assigning a variable to $opt_f, but isn't that option supposed to be a Boolean?
    $opt_f is a scalar variable. It can be assigned any legal scalar value: 0, 1, 734, 'abc', etc. Your Getopts function is probably setting $opt_f to 1 if -f appears on the command line. I agree that assigning it in your script is strange, but it is legal.