develooper Front page | perl.perl5.porters | Postings from December 2012

Re: [perl #116098] Error in Getopt::Std documentation examples

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
December 16, 2012 21:24
Subject:
Re: [perl #116098] Error in Getopt::Std documentation examples
Message ID:
20121216212437.GO1842@iabyn.com
On Sat, Dec 15, 2012 at 06:44:45AM -0800, rrt@sc3d.org wrote:
> # New Ticket Created by  rrt@sc3d.org 
> # Please include the string:  [perl #116098]
> # in the subject line of all future correspondence about this issue. 
> # <URL: https://rt.perl.org:443/rt3/Ticket/Display.html?id=116098 >
> 
> 
> 
> This is a bug report for perl from rrt@sc3d.org,
> generated with the help of perlbug 1.39 running under perl 5.14.2.
> 
> 
> -----------------------------------------------------------------
> [Please describe your issue here]
> 
> The first two examples in the documentation appear to have incorrect
> comments:
> 
>            getopt('oDI');    # -o, -D & -I take arg.  Sets $opt_* as a side effect.
>            getopt('oDI', \%opts);    # -o, -D & -I take arg.  Values in %opts
>  
> According to my tests (and by comparison with C & shell getopt), the
> above should say "do not take arg", not "take arg". If you want an
> argument, you need to follow the option with a colon. (As far as I can
> tell, optional arguments are not possible in the Perl implementation;
> in shell you use a double colon for that case).

I think you have confused the optopt() function with the getopts()
function. The former doesn't recognise ':', and assumes that every switch
has an argument; so the documentation is correct:


    $ cat /tmp/p
    #!/usr/bin/perl

    use Getopt::Std;
    use Data::Dumper;

    getopt('a:bcd', \%opts);
    print Dumper \%opts;
    __END__

    $ /tmp/p -a -b -c -d -: arg1 arg2 arg3 arg4
    $VAR1 = {
	      'c' => '-d',
	      'a' => '-b',
	      ':' => 'arg1'
	    };
    $

Changing 'getopt' to 'getopts' in the code above gives this output:

    $VAR1 = {
	      'c' => 1,
	      'a' => '-b',
	      ':' => 1,
	      'd' => 1
	    };


(Interestingly enough, getopts() seems to treat the ':' both as an argument
placeholder and an option!)



-- 
"There's something wrong with our bloody ships today, Chatfield."
    -- Admiral Beatty at the Battle of Jutland, 31st May 1916.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About