Anyway, try it, and you will see it is quite stable, but if you have any suggestion for improvement, I would very much welcome it.
Yes, sorry, I wasn't doubting the quality of the implementation of Nice::Try. Though PPI is certainly excellent, like all static Perl parsers it can't actually parse all of Perl. Sadly, all source filters suffer from the fact that they can be broken by things outside of their control, namely, the source code that they are inserted into.
With Perl 5.34:
use warnings;
use strict;
use experimental 'try';
print foo(); # prints "Caught an error: Argh..."
sub foo {
try {
die( "Argh...\n" );
}
catch( $wow ) {
return( "Caught an error: $wow" );
}
}
| [reply] [d/l] |
Yes, sorry, I wasn't doubting the quality of the implementation of Nice::Try.
I did work hard to develop it thinking it would benefit many, but there is always room for improvement, and I enjoy constructive criticism and perl monks is always a great place to get excellent feedbacks, so thank you.
Though PPI is certainly excellent, like all static Perl parsers it can't actually parse all of Perl. Sadly, all source filters suffer from the fact that they can be broken by things outside of their control, namely, the source code that they are inserted into.
Yes, it's true that although PPI is really good, it has its limitations. There will definitely be edge cases when PPI will fail, unfortunately. Right now, as of perl v5.34, there is an experimental implementation of try-catch and hopefully in the future, they will fully implement it. However, because it is a feature, the use experimental 'try'; must be added in each block where one wants to use try-catch.
In the snippet you provided, Nice::Try would render the same result by the way.
| [reply] [d/l] |
PPI is in no way robust enough for this use case. Source filters will never be appropriate for production use.
use strict;
use warnings;
use experimental 'signatures';
use Nice::Try;
sub foo { 1 }
try {
my $k = sub ($f = foo()) {};
}
catch ($e) {
warn "caught: $e";
}
__END__
Global symbol "$e" requires explicit package name (did you forget to d
+eclare "my $e"?) at bad-syntax.pl line 10.
syntax error at bad-syntax.pl line 10, near ") {"
Execution of bad-syntax.pl aborted due to compilation errors.
The module has other serious issues:
use strict;
use warnings;
use Nice::Try;
{
package MyException;
use overload '""' => 'message';
sub message { $_[0]->{message} }
sub new { my $class = shift; bless { @_ }, $class }
sub throw { my $class = shift; die $class->new(message => shift) }
}
try {
MyException->throw("hi\n");
}
catch (MyException $e) {
warn "working catch: $e";
}
catch ($e) {
warn "broken catch: $e";
}
__END__
$ perl bad-exception.pl
broken catch: hi at bad-exception.pl line 21.
| [reply] [d/l] [select] |
Thank you for taking the time and test it, much appreciate it.
use experimental 'signatures';
Yes, this (sub ($f = foo())) messes it up. I will add a disclaimer. Hopefully there won't be much frequency of that happening.
The module has other serious issues:
Yes, this mishandling of overloaded exception was a bug that I just resolved in version 1.1.2 now available on CPAN.
Thank you again for helping me make it better!
| [reply] [d/l] |