develooper Front page | perl.perl5.porters | Postings from October 2011

Re: Are exceptions "still broken" in core Perl?

Thread Previous | Thread Next
October 25, 2011 07:39
Re: Are exceptions "still broken" in core Perl?
Message ID:
Jesse Luehrs wrote:
>Can it? I tried getting this to happen in 5.14, and couldn't

$ perl -lwe 'package Exception { use overload "\"\"" => sub { 0 }; } eval { die bless({}, "Exception") }; print ref($@); print $@ ? "true" : "false"'

It's not about clobbering; you can actually die with an exception object
that evaluates to false.  It's pretty much unfixable.  I think the best
available approach is to have a is_exception() predicate, which returns
false for a plain scalar empty string (the null value for $@, which can't
go through die unmodified) and returns true for anything else (including
for a blessed object that stringifies to the empty string).  Meanwhile,
purveyors of exception object classes should have the good taste to make
exception objects behave as true and stringify to something non-empty.

>                      have a destructor that called eval get called as
>the enclosing eval finished, and that doesn't happen anymore in 5.14,
>since destructors localize @$.

Destructors don't localise $@; in 5.14 it just doesn't matter what they
do to $@.  $@ is set to the exception value just before the eval exits,
*after* destructors for scopes inside the eval have run.

>Well, i suppose it can obviously happen if you do anything at all
>between the eval and checking $@,

That opportunity for clobbering still exists, of course, and is another
unfixable, within the constraints of the eval interface.  It's one of
the reasons why Try::Tiny is still useful.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About