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

Dear Perl monks, I am using cgi to pass some parameters to another script (through a text box). although i've searched a bit for it, I couldn't find a solution to the following problem. I want to check if these values have been passed into a scalar. I am using an if statement and I was thinking something along the lines:

if (defined($scalar)=true){ then .... elseif (defined($scalar)=false){ then... } }
But this doesn't seem to work. does anyone have any suggestions? Thanks in advance for your help

Replies are listed 'Best First'.
Re: scalar values assignment
by moritz (Cardinal) on Apr 09, 2010 at 12:32 UTC
    Please read perlintro. It shows you the syntax of the if statement.

    Also note that you don't have to compare boolean values to true and false - you can just use them in an if statement. (And you are using an assignment anyway, not a comparison. Again read perlintro)

    Perl 6 - links to (nearly) everything that is Perl 6.
      That's true in most languages, if you're comparing whether or not a value is true or false, you don't have to explicitly add ' = true'. If you're unfamiliar with this concept, and how conditionals work, I'd strong recommend looking into general programming concepts. Not necessarily Perl, although if you're programming in Perl it would be a good place to start.

      Learning these basic concepts will help you pick up any programming language in the future. The concepts don't change, but the syntax does, and that's just a matter of googling and forums.

      I've read perlintro, and i've tried something like

      if ($scalar1! && $scalar2!){ then.... }

      still this didn't work. I also don't understand what you mean that I don't have to compare booleans to true or false. I am sorry if my questions seem a bit stupid and thank you for your time to reply to me.

        perlintro doesn't mention a then belonging to an if, because Perl is not Visual Basic.

        Just write

        if (defined(param('foo'))) { print "yes, we have a foo\n"; } else { print "out of luck, pal\n"; }
        I also don't understand what you mean that I don't have to compare booleans to true or false.

        The question is $thing the same as true? is nearly the same as is $thing true?. When you write if ($thing) { ... } then perl already asks is $thing true? - no need to make it more complicated by explcitly comparing it to true (which doesn't exist in Perl 5 anyway).

        The actual error message should be telling you where to look.

        Were you trying to form a NAND operator? !&& isn't legit.
        Also, the not operator (!) needs something to negate at the end of your conditional.

Re: scalar values assignment
by AR (Friar) on Apr 09, 2010 at 13:55 UTC

    In addition to what the other monks have said, I want to mention that you used = in your example. = is for assignment, not comparison. If you're comparing integers, you'll want to use ==. If you're comparing stings, you'll want to use eq.

    Note: there are other problems in gogoglou's pseudo example, but I'm choosing to ignore them because they have mostly been addressed.

Re: scalar values assignment
by nvivek (Vicar) on Apr 09, 2010 at 13:05 UTC
    If you want to check whether that particular scalar is defined or not.You can simply use it in the if statement as follows.
    if($scalar) { statements } else { statements }
    If you want to check if it is undef,then you can use unless or simple else for the above if statement. For inverse of if,you can also use unless.
      if($scalar) ...

      Strictly speaking, that would test whether $scalar is true, not whether it's defined. 0 or "", for example, evaluate to false, but are considered "defined".  Test defined instead.