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

While I was optimizing some code, I bumped into a bug. I narrowed it down to the bit below. This basically creates a hash reference, containing 1 plain key/value pair and 2 CGI.pm parameter based pairs. Now none of the two params return something (parameter is optional). The result is however, that the value of the text key is the next key (being session). I produced a Data::Dumper dump of the reference to be sure.
#!/usr/bin/perl use strict; use CGI qw(param header); use Data::Dumper; my @params = qw(main); my $hash = {"file"=>shift(@params),"text"=>param("lite"),"session"=>pa +ram("session")}; print header; print Dumper $hash;
Output is: $VAR1 = { 'text' => 'session', 'file' => 'main' };
COBOL has been messing with my mind, but I checked and double-checked and I can't find it :(
BTW: I am using Perl 5.6.1

Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.

Replies are listed 'Best First'.
Re: Referenced Hash
by Joost (Canon) on May 14, 2002 at 13:26 UTC
    The problem is that you call the param() subs in list context, so they return an empty list (not undef) which then get flattened into "no element".

    Assuming you expect max 1 argument per param(), you might try:

    my $hash = { "file"=>shift(@params), "text"=>scalar param("lite"), "session"=> scalar param("session") };

    if you really want to allow more than 1 argument per param(), you could put them in an anonymous array:

    my $hash = { "file"=>shift(@params), "text"=>[ param("lite") ], "session"=> [ param("session") ] };

    See also:

    perldoc perldata perldoc CGI

    -- Joost downtime n. The period during which a system is error-free and immune from user input.