Gunnar Hjalmarsson wrote:
Dr.Ruud:
Gunnar Hjalmarsson:
Dr.Ruud:
Gunnar Hjalmarsson:
eval { $ssh2->connect($_) };
if ($@) {
warn "Unable to connect host $_: $@" and next;
}
That is the "old fashioned" way. You really need to use the return
value of eval to make sure.
I don't see anything about that in "perldoc -f eval". Instead it says:
"If there is a syntax error or runtime error, or a die statement is
executed, an undefined value is returned by eval, and $@ is set to
the error message. If there was no error, $@ is guaranteed to be a
null string."
This has been discussed many times before.
In that case, why haven't the docs been changed accordingly? (rhetorical
question)
Well, it depends on how you read "If there was no error". :)
The error can happen in a place that you don't immediately know about.
The "path to disaster":
- using eval inside a DESTROY is maybe stupid but not forbidden.
- $@ is a global variable
The best known example is a DESTROY with a die-ing eval, happening at
leaving the eval bock scope. Since you don't have control over that,
you should always check the result of the eval itself.
Ok, thanks.
For more pointers, google: perlmonks eval destroy
There is a hint in perlobj: "Since DESTROY methods can be called at
unpredictable times, it is important that you localise any global
variables that the method may update. In particular, localise $@ if you
use eval {}", but that way doesn't solve all cases. For example "use"
and "BEGIN{}" also involve eval.
--
Ruud
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/