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

I'm curious how all of you CGI whizpopmonks like to handle instances where you must check against a parameter that may not be defined, I find that a lot of my programming seems to run into the fact that when using strict, I get a lot of "[Fri Oct 12 18:15:49 2001] form.cgi: Use of uninitialized value at /www/mako/dev/public_html/bin/form.cgi line 92." type of warnings. The following snippet builds a select for a form and I have defaults that I'd like to use but if the form has been called with parameters, use them instead. Note: $base_vars{'day'} is equal to todays date and $q->param('arrday') can be null (doesn't exist) or a day in the future.
my @days = (1..31); my $days_select = '<select name="arrday"><option></option>'; for (@days) { if ($q->param('arrday') && $q->param('arrday') == $_) { $days_select .= "<option value=\"$_\" selected>$_</option>"; } elsif ($base_vars{'day'} == $_ && $base_vars{'day'} > $q->param('arr +day')) { $days_select .= "<option value=\"$_\" selected>$_</option>"; } else { $days_select .= "<option value=\"$_\">$_</option>"; } } $days_select .= '</select>';

Could I get some suggestions on this? I'm tired of having error logs like 200mb's long {grin} kinda fine for development, not so fine for final code.

BlackJudas

Replies are listed 'Best First'.
Re: CGI Programming Style With Empty Params
by merlyn (Sage) on Oct 13, 2001 at 05:53 UTC
    First, I hate typing $q->param..., so I follow the style that Lincoln Stein himself uses of bringing in the subroutines. My time typing and reading is worth more than the CPU I'll ever save.

    So, having said that, I find I do this a lot:

    if (defined(my $foo = param('foo')) { .. do something with $foo } else { .. foo isn't present, now what? }
    And that seems to be a fairly reusable pattern.

    -- Randal L. Schwartz, Perl hacker

Re: CGI Programming Style With Empty Params
by tstock (Curate) on Oct 13, 2001 at 06:00 UTC
    I normally create a hash with default values, then map param('name') to hash{'name'}. The default may even be '' but this way I get no warnings.

    Another way to handle this is to assign to the param, like
    $q->param('subject') || $q->param(-name=>'subject', -value=>'no subject');

    warning: 0 or '' is still false, so the default will step in
    Tiago
      Thanks tstock, I can use your suggestion in the above code example, though I was trying to step away from that technique (using the hash map), I've used it before on occasion, but I thought there'd be a better way.
Re: CGI Programming Style With Empty Params
by tstock (Curate) on Oct 13, 2001 at 06:40 UTC
    OK, I'll have a stab at how I would do your code-
    some shortcuts for simplicity sakes, no error checking
    my $day = ($base_vars{'day'} > $q->param('arrday')) ? $base_vars{'day'} : $q->param('arrday'); my $days_select = '<select><option></option>'; for (1..31) { my $s = ($_ == $day) ? ' selected' : ''; $days_select .= qq{<option$s></option>\n}; }

    Untested. I hate calling a function (method) inside the loop if I can avoid it.

    Tiago