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

Hi,
If I declair a variable using
my $var = param('search_string');

and then test to see if the box has been filled in
if( defined($var) )

will defined always return true, as this seems to be the case for me????

Title edit by tye

Replies are listed 'Best First'.
Re: Lexical variables question
by davorg (Chancellor) on Aug 12, 2003 at 14:13 UTC

    No. If it's possible for param to return "undef" then defined $var can be false.

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

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

      But only a return of "undef" will cause defined to be false, anything else and $var is still defined, even a blank.
Re: Lexical variables question
by Abigail-II (Bishop) on Aug 12, 2003 at 14:14 UTC
    No. $var has the value of whatever param('search_string') returns. If that function returns an undefined value, the condition in the if statement will be false.

    This has nothing at all to do whether $var is a lexical variable or not.

    Abigail

Re: Lexical variables question
by edan (Curate) on Aug 12, 2003 at 15:00 UTC

    perldoc CGI states:

    If a value is not given in the query string, as in the queries "name1=&name2=" or "name1&name2", it will be returned as an empty string. This feature is new in 2.63.

    So, assuming you're using version 2.63 or later, and assuming that the parameter is being passed in the query string, only with no value (as in 'search_string=&blah_blah_blah=bar'), you'll want this instead of what you're using:

    if ($var ne '')

    On a side note, as I was testing the above statement of CGI.pm, I found that if the parameter is the only thing in the query string, and has no value, then param() returns undef instead of the expected '' empty string. Anyone know of a reason for this? Is it a bug, or is it expected behavior? I tested with CGI 2.752

    echo "foo" | perl -MCGI=param,-debug -le'defined param("foo") and prin +t "defined!"' # doesn't print defined! ?? echo "foo=" | perl -MCGI=param,-debug -le'defined param("foo") and pri +nt "defined!"' echo "foo&" | perl -MCGI=param,-debug -le'defined param("foo") and pri +nt "defined!"' echo "&foo" | perl -MCGI=param,-debug -le'defined param("foo") and pri +nt "defined!"' # these all print defined!

    HTH

    --
    3dan
      if ($var ne '')
      That will emit a warning. You need to test definedness first.

      Makeshifts last the longest.

Re: Lexical variables question
by dragonchild (Archbishop) on Aug 12, 2003 at 14:19 UTC
    $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.

      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
      $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.