Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Perl try { } catch(e) { }

by Superfox il Volpone (Sexton)
on Mar 03, 2015 at 14:16 UTC ( #1118605=perlquestion: print w/replies, xml ) Need Help??

Superfox il Volpone has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,
I am looking for a way to implement the construct:
try { die ("Hello world"); } catch(e) { print("Exception: " $e); }
I know this snippet from Programming Perl 4th ed,pp 329, and it works using locals:
sub try(&$) { my ($try, $catch) = @_; eval { # perl try &$try }; if($@){ # perl catch local $_ = $@; &$catch(); } } sub catch(&){ $_[0]; }
And this works as try { ... } catch { print("Exception caught: $_"); };
I'd like to refer the exception with something like catch(e){ ... }.

I would prefer to avoid using any external module such as TryCatch for portability reasons.


Replies are listed 'Best First'.
Re: Perl try { } catch(e) { }
by Corion (Pope) on Mar 03, 2015 at 14:27 UTC

    Have you looked at how Try::Tiny does its thing. Most likely, you can reuse that code for your implementation.

    Note that catch(e) is invalid syntax in Perl (unless you declare a parameterless function e()). Resorting to something like catch  my $e { is not allowed by Perl syntax either, because the & prototype works its syntactical magic only when it is the first prototype character.

    You could use something like

    catch { my $e= $@; ... }

    ... if you really want to rename $@.

      Aaaaahh I think I understood now how this is supposed to work:
      #!/usr/bin/env perl sub try(&$) { my ($try, $catch) = @_; eval { # perl try &$try }; if($@){ # perl catch &$catch($@); } } sub catch(&){ shift } try{ die("Frankie"); } catch { my $e = shift; print("Hello world $e\n"); }
      catch { ... } is invoked before the block in the try { ...}, and all it does is to return the block as a reference to the anonymous function defined by the catch block.
      Tricky mechanism.


        If you're going to use that code instead of Try::Tiny (which by the way is pure Perl and has no non-core dependencies), make sure you read the "Background" section of Try::Tiny's docs, as there are a number of issues with $@!

        "Aaaaahh I think I understood ..."

        I don't (for the moment) - but i don't care about and just do:

        #!/usr/bin/env perl use strict; use warnings; use Try::Tiny; try { die qq(Goodbye World!); } catch { warn $_; }; __END__

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: Perl try { } catch(e) { }
by Anonymous Monk on Mar 03, 2015 at 17:13 UTC
    "... for portability reasons."

    Welcome to the 21st Century, where that is no longer a valid excuse. Add the appropriate package to your deploy policy and let your configuration management solution do the rest.

    You do have a configuration management solution, don't you? ;)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1118605]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2021-10-19 20:57 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (77 votes). Check out past polls.