Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Take reference and then dereference die() and warn()

by dmitri (Curate)
on Nov 08, 2013 at 15:46 UTC ( #1061727=note: print w/ replies, xml ) Need Help??


in reply to Re: Take reference and then dereference die() and warn()
in thread Take reference and then dereference die() and warn()

The reason is that I want to put this into the or:

do_something() or ($die_on_error ? \&die : \&warn)->("error");


Comment on Re^2: Take reference and then dereference die() and warn()
Download Code
Re^3: Take reference and then dereference die() and warn()
by Laurent_R (Parson) on Nov 08, 2013 at 17:28 UTC

    But you simply don't need that: you can just die or warn in the usual way within your ternary operator, as you can see in the following Perl one-liners:

    $ perl -e '$die_on_err=shift; 0 or ($die_on_err? die "failed\n" : warn + "warning\n")' 1 failed $ perl -e '$die_on_err=shift; 0 or ($die_on_err? die "failed\n" : warn + "warning\n")' 0 warning

      I realize that; my error message is much longer than "failed", and I do not want to copy-and-paste it.

        OK, I understand your motivations, now.

        You still have numerous simpler ways to do it, such as storing your long error message in a variable (you actually had a $error variable in your original post) or doing something like this:

        $ perl -e 'use diagnostics; $die_on_err=shift; 0 or print "error messa +ge" and ($die_on_err? die $! : warn "\n" ) ' 0 error message
        or, yet simpler, using a subroutine such as the one proposed by Jenda in this post: Re: Take reference and then dereference die() and warn().

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-09-24 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (244 votes), past polls