Glenn Linderman wrote:
>
> Tony Olekshy wrote:
>
> > Traditionally Perl has had both the "do" and the "eval" block
> > forms, the latter which traps, the former which doesn't.
>
> In the perl 5 pocket reference 3rd edition page 63, it claims that
> $@ is set to the result of an eval or do. How does this impact
> exception handling tests on $@ to determine if an exception was
> thrown, if $@ can be set by a do ? OR is that an error in the
> pocket guide?
The following program
$@ = 0; eval { 1 }; print '$@: ', $@, "\n";
$@ = 0; eval { die 2 }; print '$@: ', $@;
$@ = 0; do { 3 }; print '$@: ', $@, "\n";
$@ = 0; do { die 4 }; print '$@: ', $@;
which produces the following results
$@:
$@: 2 at ... line 2.
$@: 0
4 at ... line 4.
would suggest that
- eval{} always touches $@, setting it to undef or some
die's argument, and unwinding (if any) is terminated.
It does not set $@ to the result of the eval.
- do{} doesn't touch $@ unless the block throws, in which
case $@ is some die's argument, and unwinding begins.
It does not set $@ to the result of the do.
If so, it would appear that the pocket reference has it wrong,
and that the traditional operation of "do" is not affected by the
details of any try/throw/except/always/catch/finally mechanism
that specifies a particular behaviour for $@. From an exception
handling perspective, "do" is transparent.
Yours, &c, Tony Olekshy