Most esteemed monks,
I've long been a fan of Perl's Fatal module, which allows one to replace both user-defined and built-in functions with equivalents that throw an exception on failure. However due to the limitations of Perl before 5.10, Fatal had a package-wide scope, and this would keep me awake at night. What if someone introduced Fatal to their legacy code and introduced/revealed new bugs, due to Fatal's wide-reaching consequences?
However with the powers of Perl 5.10 and the %^H lexical hints hash, we have the power to create Fatal-style changes with only lexical scope. I've put together a proof-of-concept pragma to do this, which currently works as follows:
use exceptions qw(open close); open(my $fh, '<', 'some_file'); # Throws exception on failure { no exceptions qw(open); open(my $other_fh, '<', 'some_other_file'); # fails silent close($fh); # This still throws an exception. no exceptions; # Turns off exceptions entirely. close($other_fh); # Fails silent. } close($yet_another_fh); # Throws exception on failure again.
The code as it stands has a few adjustments that I need to make, and obviously a lot of tests to ensure that code this cool works correctly. I'm primarily looking for interface and naming suggestions, although I'm happy for this to start a broard discussion.
Some matters I'm still considering:
- What should the module be called? My first two picks, fatal and exceptions (lower-case, as it's a pragma) conflict in name with the existing Fatal (core) and Exceptions (CPAN) modules. Sure, Perl cares about case, but Windows filesystems don't, so we can't simply use the lower-cased name of an existing module.
- no exceptions with no arguments should clearly turn off all exceptions generated by this pragma, but what should use exceptions do? Should it be a no-op? Should it be an error? Should it try to turn them on for everything? (I think this last one would be a bad idea.)
- Should this module even exist? Am I duplicating existing work? Note that I'm using Fatal to do all the heavy lifting internally. To the best of my knowledge there's no existing way to use Fatal with lexical scope.
- Anything else you'd like to comment/suggest/ask.
Those who are curious should also know that this module is currently being discussed on moduless@perl.org. If you really want you can also download my proof of concept, but you should be aware that's it's unfinished, buggy, emits warnings, ugly, is poorly documented and should not be used for production code. Using the code, or even looking at it, may cause the Earth to fall into the sun and your hair to fall out. It also only works under 5.10. It's a proof of concept, not a finished piece of art. I am seeking feedback on the interface, not the code, at this time.
Thanks in advance for all your thoughts,
Paul Fenwick
Perl Training Australia
Update: After many discussions on p5p it looks like we'll be getting a new Fatal.pm module with 5.10.1. The new pragma version will be called autodie.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: RFC: Lexical Fatal.pm for Perl 5.10
by BrowserUk (Patriarch) on Mar 09, 2008 at 08:25 UTC | |
by pjf (Curate) on Mar 09, 2008 at 08:47 UTC | |
by BrowserUk (Patriarch) on Mar 09, 2008 at 09:40 UTC | |
Re: RFC: Lexical Fatal.pm for Perl 5.10
by hossman (Prior) on Mar 09, 2008 at 07:58 UTC | |
by pjf (Curate) on Mar 09, 2008 at 08:26 UTC | |
by Smylers (Pilgrim) on Mar 10, 2008 at 11:15 UTC | |
Re: RFC: Lexical Fatal.pm for Perl 5.10
by diotalevi (Canon) on Mar 09, 2008 at 18:10 UTC | |
by pjf (Curate) on Mar 10, 2008 at 00:43 UTC |