Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

if statement question

by dicty (Sexton)
on Dec 13, 2012 at 20:02 UTC ( [id://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

Replies are listed 'Best First'.
Re: if statement question
by toolic (Bishop) 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 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".

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found