Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

backwards compatible 'die $obj'

by LanX (Saint)
on Sep 24, 2021 at 15:52 UTC ( [id://11136994]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

we have a large infrastructure based on catching die "string" errors and catching them with eval I want to introduce die $err_obj with Try::Tiny for a cleaner handling.

But for introduction we need to be backwards compatible, i.e. $err_obj should stringify via overload to a classical error string. Before reinventing the wheel I was skimming thru CPAN but couldn't quite find a ready to use solution.

Any recommendations?

update

talking about Throw looks promising ... :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: backwards compatible 'die $obj'
by choroba (Cardinal) on Sep 24, 2021 at 16:01 UTC
Re: backwards compatible 'die $obj'
by Corion (Patriarch) on Sep 27, 2021 at 15:18 UTC
      > s/autoload/overload/ I think

      oops, of course, corrected! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: backwards compatible 'die $obj'
by perlancar (Hermit) on Sep 28, 2021 at 02:00 UTC
    Maybe something like Object::Util::Stringify can help? It can be used with other exception framework or just without.
      Thanks, but how to implement stringification via overload is not the real problem here.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: backwards compatible 'die $obj' (grep.metacpan.org)
by LanX (Saint) on Sep 30, 2021 at 22:42 UTC
    FWIW:

    this query on https://grep.metacpan.org

    https://grep.metacpan.org/search?q=use%5Cs%2Boverload&qd=%5Cb%28Carp%7CThrow%7CDie%7CTry%7CCatch%7CException%7CError%29&qft=&qci=on

    lists over 30 potential distributions named \b(Carp|Throw|Die|Try|Catch|Exception|Error) and including use\s+overload

    I didn't link directly to the query to avoid unnecessary traffic.

    grep.metacpan still seems to be work in progress, it didn't refresh automatically for me and I had to reload.

    It's also listing dozens of empty result pages.

    Not all hits are valid, I couldn't narrow it down yet to only those overloading '""' explicitly, because a) perl has many q("") expressions and b) they need to be on the same line for the query.

    I guess at most half of those hits are valid, but refining the search with the API gives a powerful tool to find valid modules. :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found