Skip Menu |

This queue is for tickets about the Getopt-Long CPAN distribution.

Report information
The Basics
Id: 35759
Status: resolved
Priority: 0/
Queue: Getopt-Long

People
Owner: Nobody in particular
Requestors: KRYDE [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 2.37
Fixed in: 2.37_01



Subject: callback option arg overloaded to false
The attached foo.pl run as "perl foo.pl --foo" prints "no", where I hoped it would print "yes", which is what Getopt::Long version 2.35 did. I struck this when using a single func as the handler on multiple options, saving the option value as a mode like "$mode = $opt", with a check "if ($mode)" before overwriting, to allow just one --foomode or --barmode or --quuxmode, if you know what I mean. I guess the overload of "0+" in Getopt::Long::CallBack and/or the "fallback" setting there somehow makes such an object seem always false.
Subject: foo.pl
use strict; use warnings; use Getopt::Long; sub foo { my ($opt) = @_; if ($opt) { print "yes\n"; } else { print "no\n"; } } GetOptions (foo => \&foo);
From: jv [...] cpan.org
Yes, this is a known side-effect from introducing the option object that gets passed as the first parameter. Does it help to add an explicit overload for boolean context: use overload # Treat this object as an oridinary string for legacy API. '""' => \&name, '0+' => sub { 0 }, 'bool' => sub { 1 }, fallback => 1;
Subject: Re: [rt.cpan.org #35759] callback option arg overloaded to false
Date: Sun, 11 May 2008 11:04:33 +1000
To: bug-Getopt-Long [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
"Johan Vromans via RT" <bug-Getopt-Long@rt.cpan.org> writes: Show quoted text
> > Yes, this is a known side-effect from introducing the option object that > gets passed as the first parameter.
Actually I realized shortly after that debian "lintian" struck the same as me, and maybe "vcp" too, so I might have been only repeating ... Show quoted text
> Does it help to add an explicit overload for boolean context: > > 'bool' => sub { 1 },
Yep. I guess anyone using "--0" as an option might yet get an unpleasant surprise, if such an option like was likely, and was used as a number. (I suppose it's impossible to make an object be like the string used to be for all purposes.)
From: jv [...] cpan.org
On Sat May 10 21:05:40 2008, user42@zip.com.au wrote: Show quoted text
> Actually I realized shortly after that debian "lintian" struck the same > as me, and maybe "vcp" too,
What keeps bugging me is why anyone would use the first arg of the callback function in bool context. Until now, this was just plain true, so why test?
Subject: Re: [rt.cpan.org #35759] callback option arg overloaded to false
Date: Mon, 12 May 2008 08:43:37 +1000
To: bug-Getopt-Long [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
"Johan Vromans via RT" <bug-Getopt-Long@rt.cpan.org> writes: Show quoted text
> > What keeps bugging me is why anyone would use the first arg of the > callback function in bool context. Until now, this was just plain true, > so why test?
Oh, well, it's in replacing something else that starts false. I guess a style like "--mode=foo" can make it more obvious there's a single mode type choice, but is longer for human use. At any rate, for interest my bit (stringifying now) my $mode; { my $set_mode = sub { my ($opt) = @_; if ($mode) { print STDERR __x("chart: already got mode option --{mode}\n", mode => $mode); exit 1; } $mode = "$opt"; }; ... GetOptions (download => $set_mode, ticker => $set_mode, intraday => $set_mode, latest => $set_mode, ... } Or from lintian, sub record_action { if ($action) { die("too many actions specified: $_[0]"); } $action = "$_[0]"; } my %opthash = ( # ------------------ actions "setup-lab|S" => \&record_action, "remove-lab|R" => \&record_action, "check|c" => \&record_action, "check-part|C=s" => \&record_check_part, "dont-check-part|X=s" => \&record_dont_check_part, "unpack|u" => \&record_action, "remove|r" => \&record_action,
For all sensible purposes, the following should work: (Getopt/Long.pm, somewhere around line 1484): use overload # Treat this object as an ordinary string for legacy API. '""' => \&name, fallback => 1; Could you verify this?
Subject: Re: [rt.cpan.org #35759] callback option arg overloaded to false
Date: Wed, 14 May 2008 10:39:42 +1000
To: bug-Getopt-Long [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
"Johan Vromans via RT" <bug-Getopt-Long@rt.cpan.org> writes: Show quoted text
> > '""' => \&name, > fallback => 1;
That seems better.