in reply to Testing for filled-in box with CGI::param() ?

$var will be defined, but false. Try if ($var) instead.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Lexical variables question
by edan (Curate) on Aug 12, 2003 at 15:24 UTC

    In addition to davorg's valid criticism, your if ($var) test is less-than-optimal if zero is a valid value for the param, which it probably is...

    --
    3dan
Re: Re: Lexical variables question
by davorg (Chancellor) on Aug 12, 2003 at 14:34 UTC
    $var will be defined, but false. Try if ($var) instead.

    Why do you think that $var can't be "undef"? If param returns "undef" then $var will be undefined.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Because it won't be undef from a checkbox. That was the real question from the OP. It will be undef if the name is wrong. It will be defined if the name is there and is a checkbox. It will be ''.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        Because it won't be undef from a checkbox.

        Who said anything about a checkbox? In fact, who said anything about CGI? All we know for sure is that there is some function called param and it takes a string as a parameter.

        If I was going to make the huge assumption that we're talking about the param function from CGI.pm then my first guess would be that a control called "search_string" would be a textbox not a checkbox.

        And besides, a CGI program should never make assumptions about the form that calls it. The query might come from a faked up form that has completely different controls on it. Or it might be a query that was built from scratch by a Perl script using LWP and have half of the required parameters missing.

        You're making an awful lot of assumptions here.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg