in reply to Puzzling CGI behavior

The quick (and possibly dirty, depending on your POV) is to ensure that your hash has a default value the first-time through.

my %webobj = ( 'template' => param('template')||'', 'minscale' => param('minscale')||'0' );

A quick test shows this seems to work ok for your example.


Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Replies are listed 'Best First'.
Re: Re: Puzzling CGI behavior
by punkish (Priest) on Jan 30, 2003 at 03:13 UTC
    thanks for the suggestion. I will take it. However, here's my question. Why does this happen? I mean, I can also do something like
    my $template = param('template'); my $minscale = param('minscale'); my %webobj = ( 'template' => $template, 'minscale' => $minscale );
    and then I get acceptable behavior. But, this shouldn't be necessary. In fact, even if I get something strange, why the heck does 'template' => param('template') end up grabbing 'minscale' as its value... that comma after param('template') should tell it to lay off of 'minscale'. Would love to find out why? Many thanks
      that comma after param('template') should tell it to lay off of 'minscale'.

      It doesn't, though. merlyn has the right of it, as usual. There are no parameters. Calling param() in list context (as happens in the hash assignment) produces an empty list in this case. Since there has to be a value, Perl grabs the next one in a list.

      Your snippet is equivalent to:

      my %webobj = ( 'template', param( 'template' ), 'minscale', param( 'minscale' ), );

      Consider what happens if you assign an empty list:

      use strict; use Data::Dumper; my %hash = ( foo => ,,, bar => 1 => 'baz' ); print Dumper \%hash;