Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

abbreviate 'do { $@="blah blah"; return undef; };

by iaw4 (Monk)
on Jan 25, 2012 at 17:41 UTC ( [id://949935]=perlquestion: print w/replies, xml ) Need Help??

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

dear perl monks: I would like to mimick eval in its return scheme: $@ stores a message, and the routine returns undef; right now, I am writing
(cond1 && cond2) and do { $@="message"; return; };
looks ugly. what I would prefer is
(cond1 && cond2) and signalerror "message";
of course, the problem is that a sub signalerror returning undef will not return one level up. I could write something like
(cond1 && cond2) and return signalerror "message";
but at this point, my syntax looks about as painful. is there a way to coax perl to expand signalerror into $@=message and then return undef? /iaw

Replies are listed 'Best First'.
Re: abbreviate 'do { $@="blah blah"; return undef; };
by Corion (Patriarch) on Jan 25, 2012 at 17:49 UTC

    I would opt for your last version, but there also is Scope::Upper::unwind, which lets you return from the perspective of the caller. Not that I really advocate messing with the control flow outside of Perls primitives.

    What's your problem with calling die or croak?

Re: abbreviate 'do { $@="blah blah"; return undef; };
by stonecolddevin (Parson) on Jan 25, 2012 at 17:47 UTC

    Check out Try::Tiny

    Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past

Re: abbreviate 'do { $@="blah blah"; return undef; };
by JavaFan (Canon) on Jan 25, 2012 at 18:02 UTC
    return if cond1 and cond2 and $@ = "message";
Re: abbreviate 'do { $@="blah blah"; return undef; };
by tobyink (Canon) on Jan 25, 2012 at 23:03 UTC
    s/signalerror/die/

    OK, so it's not quite what you want, but it's more likely to be what people calling your code will expect.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found