Another similarity between perl and shell (common to C as well), is that the second expression will not be evaluated at all if the first expression "answers the question". This matters when the second expression has side effects. Some trivial examples:
sub f0 { return 0 }
sub f1 { return 1 }
if (( my $x = f1() ) && ( my $y = f1() ))
# true, and both variables are set to 1
if (( my $x = f0() ) && ( my $y = f1() ))
# false, $x is set to 0, $y is undef
if (( my $x = f0() ) || ( my $y = f1() ))
# true, $x is set to 0, $y is set to 1
if (( my $x = f1() ) || ( my $y = f1() ))
# true, $x is set to 1, $y is undef
As in shell usage, this can be used to good effect, to perform (or skip) an action based on the outcome of previous action. (It can also be a trap for the unwary.)
In reply to Re: AND OR
by graff
in thread AND OR
by hasimir44
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.