Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: Need clean codeby davido (Cardinal) |
on Mar 25, 2017 at 19:54 UTC ( [id://1185951]=note: print w/replies, xml ) | Need Help?? |
You are using the wrong or there. It will work often, except where it causes hard to find bugs. Instead, use || for "OR" expressions. Use or for control of flow, and || for expressions that evaluate to a value, as a general rule of thumb. The issue is the low precedence of or, which will trick you sometimes. So the first code snippet above could be written as:
Your second code snippet is also probably broken, because undef $var will set $var to undef and will then evaluate the expression's value, which is now always going to be undef. You probably meant this:
Which might be written more elegantly like this:
For what it's worth, some of this applies equally in many languages such as C and C++. There's not really a concept of definedness in C/C++, but the concept of short circuiting in expressions using ||, and of ternary operators is nearly identical. Dave
In Section
Seekers of Perl Wisdom
|
|