Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the CGI CPAN distribution.

Report information
The Basics
Id: 37908
Status: resolved
Priority: 0/
Queue: CGI

People
Owner: Nobody in particular
Requestors: hkhwu [...] yahoo.com
pcronin [...] loyola.edu
paul [...] schillingconsulting.com
Cc:
AdminCc:

Bug Information
Severity: Critical
Broken in: (no value)
Fixed in: (no value)



Subject: Default of 0 is no longer selected
3.38 adds the logic to popup_menu: CGI.pm:2454 } elsif ($default) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default; } However, if $default is 0 it will not be selected like the old logic in 3.37: CGI.pm:2444 } else { $selected = $default; }
Subject: popup_menu -default => 0 no longer works.
I know this was working in 3.15 but is no longer working in 3.40. The code in "sub popup_menu" changed from: 3.15: if (!$override && defined($self->param($name))) { $selected = $self->param($name); } else { $selected = $default; } To 3.40 line 2452: if (!$override && defined($self->param($name))) { $selected{$self->param($name)}++; } elsif ($default) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default; } The elsif is causing the problem. It could be "} elsif (defined($default)) {" That way a value of 0 or "" will allow the code to enter the elsif. An example which no longer works: $cgi->popup_menu('-name'=>'foo', '-values'=>[1, 0], '-labels'=>{1=>'True', 0=>'False'}, '-override' => 1, '-default'=>0); Nothing will be set to selected in the output and the browser will default to True. Thanks for CGI.pm. I have been using it for a *very* long time.
Subject: CGI.pm popup_menu default value of 0
Date: Fri, 29 Aug 2008 15:17:01 -0400
To: bug-CGI.pm [...] rt.cpan.org
From: Patrick Cronin <pcronin [...] loyola.edu>
I'd like to report a bug in CGI.pm-3.41 that occurs when using popup_menu to create a popup menu. Perl version: "This is perl, v5.8.8 built for i686-linux" OS vender and version: Linux hostname.com 2.6.9-55.0.12.ELsmp #1 SMP Fri Nov 2 11:19:08 EDT 2007 i686 i686 i386 GNU/Linux Consider the code: use CGI; my $q = new CGI; print $q->popup_menu( -name => 'active', -value => [ 1, 0 ], -default => 0, -labels => { '1' => 'Yes', '0' => 'No' } ) I would expect this to create a popup menu with two options, "Yes" and "No," and that "No" would be the default selected value. What happens is that the menu is created, but the default value is not selected, and since "No" is the second value, the "Yes" shows up as the default value. When you wrap the 0 in an array ref, which the documentation says is for providing multiple values, the 0 value works. The following code produces the menu as expected: use CGI; my $q = new CGI; print $q->popup_menu( -name => 'active', -value => [ 1, 0 ], -default => [ 0 ], -labels => { '1' => 'Yes', '0' => 'No' } ) Thanks, Patrick Cronin
I guess this is a duplicate of bug #37908, and I prefer the following solution in function popup_menu (~ line# 2454) -------- if (!$override && defined($self->param($name))) { $selected{$self->param($name)}++; ! } elsif ($default) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default; } -------- if (!$override && defined($self->param($name))) { $selected{$self->param($name)}++; ! } elsif (defined($default)) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default; } -------- Heiko
Some day I learn how to format some code here ... please see attached patch. Heiko {{code}} *** CGI.pm.orig Mon Sep 22 10:14:15 2008 --- /usr/local/lib/perl5/site_perl/5.8.8/CGI.pm Mon Sep 22 09:50:42 2008 *************** *** 2450,2458 **** my($result,%selected); if (!$override && defined($self->param($name))) { $selected{$self->param($name)}++; ! } elsif ($default) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default; } --- 2450,2458 ---- my($result,%selected); if (!$override && defined($self->param($name))) { $selected{$self->param($name)}++; ! } elsif (defined($default)) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default; }
*** CGI.pm.orig Mon Sep 22 10:14:15 2008 --- /usr/local/lib/perl5/site_perl/5.8.8/CGI.pm Mon Sep 22 09:50:42 2008 *************** *** 2450,2458 **** my($result,%selected); if (!$override && defined($self->param($name))) { $selected{$self->param($name)}++; ! } elsif ($default) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default; } --- 2450,2458 ---- my($result,%selected); if (!$override && defined($self->param($name))) { $selected{$self->param($name)}++; ! } elsif (defined($default)) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default; }
From: paul [...] schillingconsulting.com
The attached patch file corrects the problem (patch -p0 < popup-default.patch). I tracked this down to a change in between release 3.37 and 3.38: Show quoted text
> perl -e 'use lib "CGI.pm-3.37"; use CGI; print &CGI::popup_menu(-name=>"foo", -
values=>[-2..2], -default=>0), "\n\n";' <select name="foo" > <option value="-2">-2</option> <option value="-1">-1</option> <option selected="selected" value="0">0</option> <option value="1">1</option> <option value="2">2</option> </select> Show quoted text
> perl -e 'use lib "CGI.pm-3.38"; use CGI; print &CGI::popup_menu(-name=>"foo", -
values=>[-2..2], -default=>0), "\n\n";' <select name="foo" > <option value="-2">-2</option> <option value="-1">-1</option> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> </select>
--- CGI.pm-orig 2008-11-09 10:08:04.000000000 -0600 +++ CGI.pm 2008-11-09 10:07:44.000000000 -0600 @@ -2451,7 +2451,7 @@ if (!$override && defined($self->param($name))) { $selected{$self->param($name)}++; - } elsif ($default) { + } elsif (defined($default)) { %selected = map {$_=>1} ref($default) eq 'ARRAY' ? @$default : $default;
On Mon Sep 22 04:10:49 2008, http://wecos.pip.verisignlabs.com/ wrote: Show quoted text
> I guess this is a duplicate of bug #37908, and I prefer the following > solution in function popup_menu (~ line# 2454) > > -------- > if (!$override && defined($self->param($name))) { > $selected{$self->param($name)}++; > ! } elsif ($default) { > %selected = map {$_=>1} ref($default) eq 'ARRAY' > ? @$default > : $default; > } > -------- > if (!$override && defined($self->param($name))) { > $selected{$self->param($name)}++; > ! } elsif (defined($default)) { > %selected = map {$_=>1} ref($default) eq 'ARRAY' > ? @$default > : $default; > } > -------- > > Heiko
Thanks for this patch. I agree it's a bug and consider it a high priority to get it patched in and released. Could you also submit an automated test which illustrates the case? That will help prevent future regressions. Mark
Thanks, this patch has now been applied in my git repo and should appear in the next release.
Subject: released, thanks.
I believe this change was released today as part of CGI.pm 3.45. Thanks for the contribution.