Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^6: Perl try { } catch(e) { }

by jdeguest (Sexton)
on Jun 17, 2021 at 02:47 UTC ( [id://11133938]=note: print w/replies, xml ) Need Help??


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

they are very prone to making code extremely brittle
Yes, it is true, but it largely depends on what the change are and how they are made. In the case of Nice::Try, the changes are carefully implemented and minimal and the module relies on PPI, which is way much safer than using regular expression. 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.

Replies are listed 'Best First'.
Re^7: Perl try { } catch(e) { }
by haukex (Archbishop) on Jun 17, 2021 at 08:57 UTC
    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" ); } }
      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.

Re^7: Perl try { } catch(e) { }
by Haarg (Priest) on Jun 18, 2021 at 04:10 UTC
    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.
      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11133938]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-25 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found