in reply to Re: A cleaner way of scoping variables
in thread A cleaner way of scoping variables
This code has a problem: if the condition is false, the condition is the last expression evaluated in the do block, so whatever the condition evaluates to becomes the result of the do block.
$ perl -le'print do { "x" if 1 }' x $ perl -le'print do { "x" if 0 }' 0
In your case, the value of $foo is assigned to $bar if $foo is false. This could be particularly nasty because you'll never notice if you don't run tests with $foo being 0 or the empty string rather than undefined.
You must make sure that your do blocks always evaluate to an intended result. In this case, you have to add an else { undef } clause.
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: A cleaner way of scoping variables
by ambrus (Abbot) on Aug 09, 2004 at 13:59 UTC | |
by Aristotle (Chancellor) on Aug 09, 2004 at 14:24 UTC | |
by ikegami (Patriarch) on Oct 21, 2005 at 15:09 UTC | |
by ambrus (Abbot) on Nov 03, 2007 at 17:01 UTC |