jpepersack has asked for the wisdom of the Perl Monks concerning the following question:
I have a question about eval.
Given the general error handling construct:
perl -e '$foo = "foo"; eval { $bar = $foo; } or do { die "Error: $@"; };'
This works as expected (IE does not die) for any value of $foo except undef or empty string. If $foo is undef or empty string, $@ gets set to empty string and the or do {...} block is executed. This seems like the Wrong Thing to me -- assigning either undef or an empty string to a scalar is a valid statement, therefore $@ should not get set
It's easy enough to work around this (mis-)behavior by putting a no-op at the end of the eval block:
perl -e '$foo = undef; eval { $bar = $foo; 1; } or do { die "Error: $@"; };'
Still, I'm curious as to why it works this way. It doesn't seem to be consistent.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Eval, assignments, and empty string/undef
by ikegami (Patriarch) on Mar 25, 2010 at 16:24 UTC | |
Re: Eval, assignments, and empty string/undef
by BrowserUk (Patriarch) on Mar 25, 2010 at 16:33 UTC | |
by Anonymous Monk on Mar 26, 2010 at 16:44 UTC | |
Re: Eval, assignments, and empty string/undef
by meredith (Friar) on Mar 26, 2010 at 13:57 UTC |