in reply to Re^6: Perl try { } catch(e) { }
in thread Perl try { } catch(e) { }

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 line 10. syntax error at line 10, near ") {" Execution of 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 broken catch: hi at line 21.

Replies are listed 'Best First'.
Re^8: Perl try { } catch(e) { }
by jdeguest (Sexton) on Jun 18, 2021 at 11:31 UTC
    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!