in reply to Eval, assignments, and empty string/undef
Building on what the fellows above said, eval works like a sub and will return the result of the last expression if not given an explicit return value. This means that when used with an 'or' operator, your eval will be false for any value of $foo that is false. It's not returning success or failure of the eval operation - this allows you to squeeze an eval into just about any code you want and you don't have to jump through hoops rearranging that code in order to catch an error.
Instead, eval sets $@ to either undef or an error each time it's called, so you can test if the eval failed using 'if ($@) {...}' on the next line.
perl -e '$foo = undef; eval { $bar = $foo; }; die "Error: $@" if $@;'
|
---|