in reply to Re2: Learning how to use the Error module by example
in thread Learning how to use the Error module by example
I wouldn't just go spreading FUD abour Error.pm. It has very serious problems, and the only reason I didn't describe them is that I thought they were well-known. The try/catch syntax is implemented using sub ref prototypes. Take a look at this code:
What value do you think this sub will return if it catches an error? It will always return 1, because the return inside of the catch block just returns from the implicit subroutine that the catch block creates. Nasty.use Error qw( :try ); sub important_function { try { something_dangerous(); } catch Error::Simple with { return 0; }; return 1; }
Possibly worse, the nested closure problem can cause memory leaks in a long-running process. The problem is partially described by Matts in this presentation. Here's an example of a sub that will leak memory every time you call it:
I hope I've got this code right; it's been a while since I used it.use Error qw( :try ); sub leaky_function { my $foo = 7; try { # do something here try { $foo++; } catch Error::Simple with { # whatever }; } catch Error::Simple with { # whatever }; }
UPDATE: This leak seems to have been fixed in recent versions of Perl. That's a nice thing to see!
We used Error in the system we built at eToys, and after having both of the problems I descibe here I have decided never to use the try/catch syntax again. It's dangerous and the problems are hard to see.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re4: Learning how to use the Error module by example
by dragonchild (Archbishop) on Jul 29, 2003 at 17:51 UTC | |
by adrianh (Chancellor) on Jul 29, 2003 at 20:40 UTC | |
by dragonchild (Archbishop) on Jul 30, 2003 at 13:30 UTC | |
by adrianh (Chancellor) on Jul 30, 2003 at 13:50 UTC | |
by dragonchild (Archbishop) on Jul 30, 2003 at 14:15 UTC | |
| |
by perrin (Chancellor) on Jul 29, 2003 at 18:05 UTC |
In Section
Seekers of Perl Wisdom