Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Error.pm vs closure issue fixed in Perl > 5.8.4 ?

by npf (Initiate)
on Sep 01, 2008 at 16:45 UTC ( [id://708258]=perlquestion: print w/replies, xml ) Need Help??

npf has asked for the wisdom of the Perl Monks concerning the following question:

Hello !

I recently began to play with Perl exception mechanisms, and gave a try to the Error module with the "try/catch" syntax.
However, reading docs ( Matt Sergeant presentation), I discovered that it presents a serious issue with regard to closures, and creates memory leaks.
But I also read in the DDJ article from Dave Rolsky that Perl >= 5.8.4 might fix the closure issue ?
Trying the test sample provided in the presentation:
#!/usr/bin/perl -w use strict; use warnings; use Error qw(:try); for (1..3) { my $x = Foo->new(); try { try { print "$_: $x\n" }; }; } END { warn("interpreter exit\n") } package Foo; sub new { return bless {}, shift } sub DESTROY { warn("Foo::DESTROY\n") }
I indeed notice that the output result changes between perl 5.6.1:
1: Foo=HASH(0x80f6084) 2: Foo=HASH(0x813b750) 3: Foo=HASH(0x815a0f0) interpreter exit Foo::DESTROY Foo::DESTROY Foo::DESTROY
and Perl 5.10.0:
1: Foo=HASH(0x81981f8) Foo::DESTROY 2: Foo=HASH(0x81981f8) Foo::DESTROY 3: Foo=HASH(0x81981f8) Foo::DESTROY interpreter exit
The object seems indeed destroyed when leaving the loop's scope.

Can we then consider that this closure issue when using the "try/catch" syntax of the Error package is over ? Is it now advisable to use the Error module with the "try/catch" syntax ?

Nothing appears indeed in the perl584delta.pod about closures.

Regards,
Pierre

Replies are listed 'Best First'.
Re: Error.pm vs closure issue fixed in Perl > 5.8.4 ?
by perrin (Chancellor) on Sep 02, 2008 at 01:37 UTC
      but returning from inside a try block is still a problem (and not fixable as long as try blocks are implemented as sub refs),

      returning from a 'try' block is not fixable at all. Because 'try' has to be implemented via eval and return in an eval just leaves the eval, it doesn't return from the enclosing subroutine. So this particular problem is a problem with Perl(5)'s implementation of exceptions and not at all a problem specific to Error.pm.

      For the problem with Error.pm, s/try/catch/g on your statement:

      but returning from inside a catch block is still a problem (and not fixable as long as catch blocks are implemented as sub refs),

      - tye        

        Interesting, I thought that eval would allow a return, but you're right, it doesn't. So there are two problems that prevent this from working intuitively with return statements and neither of them have any known fix in Perl 5.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://708258]
Approved by moritz
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found