Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by jdeguest (Sexton)
on Mar 24, 2021 at 03:29 UTC ( [id://11130251]=note: print w/replies, xml ) Need Help??


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

There is a full implementation of try-catch block using perl filters: Nice::Try So you can do something like embedded try-catch block, return, variable assignments, catching class exception, etc. There is no need for semicolon on the lack brace.
use Nice::Try; try { # do something die( "Argh...\n" ); } catch( $wow ) { return( $self->error( "Caught an error: $wow" ) ); } finally { # do some cleanup }
Full disclosure: I am the author of this module that I created when Devel::Declare on which TryCatch was relying became obsolete.

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

Log In?
Username:
Password:

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

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

    No recent polls found