stevieb has asked for the wisdom of the Perl Monks concerning the following question:

There's been a bunch of talk about the if $@ after eval lately. Do the monks who have much experience with eval recommend Try::Tiny, or do they just use something like Corion suggested in Re: What does eval actually do?? Some of my modules and especially their unit tests are using the 'faulty' method, and it's blatantly clear I may have unknown issues.

-stevieb

Replies are listed 'Best First'.
Re: Do the Monks recommend Try::Tiny for eval work?
by Your Mother (Archbishop) on Oct 30, 2015 at 01:10 UTC

    Responding because I've linked the docs to the module twice lately. I don't use Try::Tiny (now, I did for awhile). I've just been linking the doc because it's a good overview of problems. I do similar to anonymonk. I didn't know Devel::EvalError either. tye usually knows what he's doing though :P so I'll check it out if I think regular eval{} isn't bringing enough to the table.

      Devel::EvalError isn't really needed with a modern version of Perl (where a DESTROY can't clobber $@).

      At work I mostly inherited the following pattern:

      eval { ... 1 } or do { my $e = $@ || 'Unknown error'; ... };

      And I've been pretty happy with it.

      We are still on a Perl version where $@ can be clobbered by a DESTROY, but we just exhaustively fix any DESTROY methods.

      If you have less control over your code base(s) and have to deal with less modern versions of Perl, then Devel::EvalError might be very useful (it was very useful to me at a prior job).

      - tye        

Re: Do the Monks recommend Try::Tiny for eval work?
by Anonymous Monk on Oct 29, 2015 at 23:31 UTC

    I use  eval { ...; 1 } or ...;

    When I think I need more than that I think about using Devel::EvalError -- Reliably detect if and why eval() failed

    Try::Tiny isn't even on my radar :)

      One I've never heard of. I'll check it out when I get home... thanks! I also like the use of  or there... very organic.
Re: Do the Monks recommend Try::Tiny for eval work?
by sam_bakki (Pilgrim) on Oct 30, 2015 at 04:57 UTC

    Hi

    At work, I extensilvely use Try::Tiny. I have many production code with try::Tiny, I highly recommend it.

    Here is how use it (Example from my Moose class production code)

    #Read the RAWFILE $self->loggerObj()->logDebug("Reading Data from RAW BIN File ".$in +RawFile); try { $rawBuffer = read_file( $inRawFile, { binmode => ':raw' } ) ; } catch { $self->loggerObj()->logError('Can not read RAW BIN File '.$inR +awFile.' '.$_); };

    Thanks & Regards,
    Bakkiaraj M
    My Perl Gtk2 technology demo project - http://code.google.com/p/saaral-soft-search-spider/ , contributions are welcome.

Re: Do the Monks recommend Try::Tiny for eval work?
by Athanasius (Bishop) on Oct 30, 2015 at 09:39 UTC
Re: Do the Monks recommend Try::Tiny for eval work?
by syphilis (Bishop) on Oct 30, 2015 at 02:06 UTC
    AFAICR, whenever I eval{...} my only interests are whether $@ has been set and, if so, the contents of $@.
    I therefore merely run the eval, then examine the contents of $@.
    I guess that if I needed to find out something else, then I'd do something else :-)

    Cheers,
    Rob
Re: Do the Monks recommend Try::Tiny for eval work?
by dsheroh (Monsignor) on Oct 30, 2015 at 09:17 UTC
    I tend to use Try::Tiny. It's simple, it works, and I don't have to think about it. There are definitely other good alternatives (as shown by previous replies), but this is the one that, for me, requires the least (effort|thought).

      I only started to use Try::Tiny around the time this thread was first posted. Now (over 3 years on) my experience matches dsheroh's. I would only add that the clarity of the syntax for me beats most if not all of the eval idioms. It seems there are several of those and each proponent has their favourite. Anyone else reading them will likely have to stop for a minute and work out what's going on. With try/catch it's clear, even to users of other such modules or even other languages with similar features. For these reasons, and that it is lightweight with no non-core dependencies, I'm very happy to continue to use Try::Tiny.

Re: Do the Monks recommend Try::Tiny for eval work?
by LanX (Cardinal) on Oct 30, 2015 at 00:39 UTC
    Well the monks certainly recommend staying in an active thread instead of starting a new one...

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Well the monks certainly recommend staying in an active thread instead of starting a new one...

      I think a straw poll deserves a thread of its own -- because it stands on its own

        the monastery has no straw poll feature ... unfortunately!

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!