Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

if statement question

by dicty (Sexton)
on Dec 13, 2012 at 20:02 UTC ( #1008709=perlquestion: print w/ replies, xml ) Need Help??
dicty has asked for the wisdom of the Perl Monks concerning the following question:

I see if statement with only a variable in the condition (), such as if ($search) { ... }. What's the condition for $search? I feel very confused about this code. Thanks, Dicty

Comment on if statement question
Re: if statement question
by toolic (Chancellor) on Dec 13, 2012 at 20:23 UTC
    If $search has a true value, the code block inside the curlies will be executed. If not, the code inside the curlies won't be executed.

    Tip #2 from the Basic debugging checklist: display the contents of variables using print:

    print ">>>$search<<<"; if ($search) { }
Re: if statement question
by nubbel (Novice) on Dec 13, 2012 at 20:25 UTC
Re: if statement question
by Anonymous Monk on Dec 13, 2012 at 20:41 UTC
    Obviously a check for "truth value" is not a desirable practice. Better to use an explicit comparison, a function such as defined() and so on. But first you have to deduce what the code as-written actually does... what sort of values are in that variable.

      That seems to come from the Java school of thought. A check for a true value is perfectly fine and does not need to be explicit. Being explicit is just worthless clutter, doubly so in a strongly typed language like Java. (I feel like strangling someone every time I have to type if (obj == NULL))

      (It's good, though, to have a clue on what the variable contains. I generally reserve such shorthands for boolean 0/1 values and variables which I expect to carry either undef or a reference.)

      "Obviously a check for "truth value" is not a desirable practice."

      Ultimately all if conditions check truth values.

      if ($foo) and if (defined $foo) have two different meanings. One is not "better" than the other. Don't use the former when you mean the latter, but don't use the latter when you mean the former.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      There is nothing wrong with boolean variables in perl, it really helps the reader if they are given names such as "$is_found" instead of "$search".

Re: if statement question
by sundialsvc4 (Monsignor) on Dec 14, 2012 at 04:30 UTC

    Obviously, the question here .. if there is a question here .. is “to what extent is the computer’s actual response to this statement, as-written, likely to be a surprise?”     If you can say with confidence that the possible values of the variable in question fall into a particular scope, then you have no surprise, hence no problem.   If, on the other hand, there might be certainty, then a more stringent way of expressing what you actually want the computer to do, might be better at preventing “those damnable surprises.”   There really isn’t enough in the OP to say more.   But we do know that, in the end, the computer will do what you said, not what you meant.   Hence, the most-precise way of expressing “what you meant” is often a way to conserve your ever more precious hair-follicles.

Re: if statement question
by nithins (Sexton) on Dec 14, 2012 at 06:57 UTC
    use strict; ## scalar has not been initialized here the value in $result is undef my $result; print "hi" if ($result); ### scalar has been initialized and the value here is 12 $result = 12; print $result if($result);

    In 1st ,if condition looks for the value of a variable "$result" in this case its just declared its not been initialized hence if is evaluated as false and doesn't print "Hi"

    In second, if condition looks for the value of a variable, its present & value is 12 , then if is evaluated as true and it print's "hi".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008709]
Approved by Ratazong
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2014-07-28 07:25 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (193 votes), past polls