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

Ok today I ran into an unexpected behavior. I have a script which generates an input string for a JavaScript menu/submenu thing. The function that generates the string builds this string based on items in the database. However if the form was just submitted I want the current categories to stick ala CGI.pm's default behavior. So I pass the current value of those params as the first two arguments to my subroutine. I have an actual third argument which if true adds an All option to each of the menus. My problem was on the initial load I wasn't getting the all menu which led me to discover a surprising behavior in Perl I wasn't aware of.
sub makeMenuInput{ my ($category,$subcategory,$allIsOn)=@_; } makeMenuInput($query->param('category'),$query->param('subcategory')," +allon");
The surprising thing that happened when 'category' and 'subcategory' weren't defined was that the "allon" option got passed in the first slot.

What I ended up having to do to get around this was:

makeMenuInput($query->param('category')||"",$query->param('subcategory +')||"","allon");
Can anyone explain why this occurs?

Replies are listed 'Best First'.
Re: Unexpected behavior after passing $query-param('bar') as subroutine parameter
by merlyn (Sage) on Sep 18, 2001 at 22:53 UTC
    Yes. param is context sensitive. In a scalar context, it returns a single scalar value of the first (arbitrary order) param with that name, or undef showing that nothing has that name. In a list context, it returns a list of all params with that name, which could indeed be an empty list, as you saw.

    You are using a list context, and using the result in a list, so an empty list disappears. Had there been multiple elements, you'd be messed up the other way, in that your later parameters would have been pushed out of place the other way.

    I'd rewrite that code as:

    makeMenuInput(scalar $query->param('category'), scalar $query->param(' +subcategory'),"allon");

    -- Randal L. Schwartz, Perl hacker

Re: Unexpected behavior after passing $query-param('bar') as subroutine parameter
by blakem (Monsignor) on Sep 18, 2001 at 22:58 UTC
    This sounds remarkably similiar to a problem discussed at DBI Binding Inconsistancy. I bet the discussion there would shed some light on this for you.

    -Blake