Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

popup_menu default that isn't

by spork (Monk)
on May 07, 2002 at 20:19 UTC ( [id://164810]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to create a popup menu in a CGI script with a default but I'm having difficulty with setting the default value. Here is my code:
@values = sort numerically keys %ProgramAreas; $default = $ProgramAreas{$ComponentAreas{$ComponentID}}; print '<TD ALIGN="right"><B>Program Area:</B></TD><TD ALIGN= +"left">', popup_menu( -name=> 'ComponentProgramArea', -values=> \@values, -defaults=> \$default, -labels=> \%ProgramAreas), "</TD><BR><BR><BR>\n";
ProgramAreas is a hash that is used in a few other places and I know that it contains useful and legal data.

$default gets assigned a real, legal value based on a print statement I threw in as a diagnostic. However, when the menu is displayed the default value is the first item in @values and not $default.

What obvious piece am I missing?

Replies are listed 'Best First'.
Re: popup_menu default that isn't
by Zaxo (Archbishop) on May 07, 2002 at 20:53 UTC

    The attribute is 'default', not 'defaults', and its value shourd be a string, not a reference to scalar. I suspect that you want the values from %ProgramAreas as labels:

    -default => $default, # -labels => [@ProgramAreas{@values}] ),
    In your sort, do you mean @values = sort {$a <=> $b} keys %ProgramAreas; ? What you have looks like pseudocode, or possibly Perl6 ;-)

    Update: Was wrong about labels.

    After Compline,

Re: popup_menu default that isn't
by DigitalKitty (Parson) on May 07, 2002 at 20:53 UTC
    Hi Spork.

    Could you post some more of the source code? If not, you can set a default value with like this:
    print popup_menu(-name=>'food', -values=>['pasta','fish','chicken', 'vegetables','fruit','bread','dessert'], -default=>'dessert');

    I once had a problem with a cgi program I writing where the script wouldn't work until I changed the word 'values' to 'value' ( without the quotes ).

    If this reply doesn't provide a solution, just post again o.k?

    You can find some more perl/CGI tutorials at:


    I would try a supersearch here first though.

    Hope this helps,


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://164810]
Approved by boo_radley
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-06-12 22:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.