On 3/7/06, Chas Owens <[EMAIL PROTECTED]> wrote: > On 3/7/06, Jay Savage <[EMAIL PROTECTED]> wrote: > snip > > Since eval traps die, 'eval{ eval {} or die [EMAIL PROTECTED];};' is a > > convenient > > method for propagating $@ out of deeply nested evals. It's just a way > > to save writing lots of if blocks that do nothing but pass $@ along. > snip > > You had better be careful with the 'or' there. The return value of an > eval is that of the last statement executed. This means that > > eval { $a = 0 } or die $@ ; > > will fail even though the block did not have an error. The safe way > to handle blocks is to check the value of $@ and die if it is set: > > eval { $a = 0 }; > die $@ if defined $@; > > Although I believe you can always get away with shortening it to > > eval { $a = 0 }; > die $@ if $@; > > since die() always (I think) adds on "at X line Y.". >
That's a good point to keep in mind, but here OP clearly wants to return 1 for success. I said it was a shortcut; YMMV on corner cases. Another neat (sometimes) trick with eval is just to use a bare die as in 'die if $@'. That will append '\t...propagated at line $x\n' to the end of $@ every time the error is proagated by a nested eval. It can help you trace the execution and is either very useful or supremely annoying, depending on the situation. -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!