Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Possible precedence issue with control flow operator

by LanX (Saint)
on Jan 13, 2020 at 17:11 UTC ( [id://11111361]=note: print w/replies, xml ) Need Help??


in reply to Possible precedence issue with control flow operator

It's not clear to me what you really wanted.

When in trouble with perlop precedence always use parentheses.

The 1. variant

> return something() or croak "something didn't work";

equals

( ( return something() ) or ( croak "something didn't work" ) );

But doesn't make much sense, because the or-branch is never reached.

A "normal" return almost never fails, and if return fails, the program dies right away:

> perl return __END__ Can't return outside a subroutine at - line 2.

(update: one might argue that the former shouldn't even compile, but there are probably use-cases in combination with do FILE )

The 2. variant

> return something() || croak "something didn't work";

equals

return ( ( something() ) or ( croak "something didn't work" ) );

which looks a bit strange but is somehow possible.

still Carp::croak() means to die

# die of errors (from perspective of caller) croak "We're outta here!";

so returning the value of croak doesn't really make sense either.

update

The latter means mixing two very different control flows in one line ... irk.

In the spirit of PBP : "make control flows immediately clear!", mixing two is quite confusing for the reader.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found