Operator precedence
Operator precedence can be a tricky thing which often causes bugs which take hours to track. That’s why people often say that you should use parantheses. This just happened to me while reading the code of a project. The following line of perl code was responsible:
open PID, ">$pidfile" || die "Can't open pid file: $!\n";
One might expect that the return value of open() is checked and if it is undefined, the die() function is executed. But what actually happens is that the string “>$pidfile” is checked. And if it is undefined, the die() function is executed. Since this string will never be undefined (at least “>” will always be present), the die() statement also will never be executed. So if open() ever fails, you simply wouldn’t notice.
The solution is simple: parantheses!
open(PID, ">$pidfile") || die "Can't open pid file: $!\n";
About this entry
You’re currently reading “Operator precedence,” an entry on blog.7C0.org
- Published:
- November 25, 2009 / 12:42 am
- Category:
- development
- Tags:
- fail, perl, programming, software
1 Comment
Jump to comment form | comment rss [?] | trackback uri [?]