Beyond the precedence problem, you are mixing two pieces of syntax that should not go together. When dealing with modifiers, if you are in doubt, try to read the statement outloud. If it doesn't make sense to you, then it is likely to be incorrect, not syntactically, but semantically.
(BTW, the statement with "if" was giving back a correct result.
hopes modification makes both results the same, but
both wrong!)
Now, let's point out a semantic problem that you might face using code like this.
Perl statement modifiers were designed to simplify reading, not to complicate it.
If your idea was to print "Get Lost!" when $nice is false, and "Hi" otherwise, then you should use a syntax that allows for alternatives.
Statements like this one (especially with dubious semantics) can confuse both the programmer and the reviewer.
My choice would be:
sub hello3 {
if ($nice) {
return "Hi"
}
else {
return "Get Lost!"
}
}
Now, in this snippet it's easy to understand what I want. I would even use the ternary operator, because (once you get used to it) it is clear what it means.
sub hello3 {
return $nice ? "Hi" : "Get Lost!"
}
I use "unless" in cases where the alternative is not needed.
print "you were asking for it!" unless $nice;
# or a return statement,
# when you just need to check a requirement
# in the middle of a sub
sub hello {
return "get lost" unless $nice;
# more statements
return "hi"
}
"or" should be used after an assertive statement ("You behave or I teach you a lesson").
$nice > 0 or print "Now I am goin to teach you ...";
_ _ _ _
(_|| | |(_|><
_|
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.