Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Abuse of "or next" in expressions and "next" that returns value

by RonW (Parson)
on Jul 18, 2018 at 21:00 UTC ( #1218787=note: print w/replies, xml ) Need Help??


in reply to Abuse of "or next" in expressions and "next" that returns value

next is not a function, so does not return a value. Nor does it need to return a value. It is either executed or not. If executed, the expression containing it never gets a chance to use anything next didn't provide. If not executed, then the other subexpression had a non-false value which became the value of the containing expression.

In this case, ${next unless $_;}, if $_ is non-false, the evaluator skips everything to the left of unless, so next isn't executed.

I do agree that ${next unless $_;} is surprising syntax. Still, it is valid and semantically equivalent to $_ or next

Replies are listed 'Best First'.
Re^2: Abuse of "or next" in expressions and "next" that returns value
by vr (Chaplain) on Jul 19, 2018 at 07:23 UTC

    Thank you everyone for answers, now it became more clear. As I understand, every statement in Perl "returns" a value -- rather, it can be evaluated if program flow requires it. The bare "next;" is a statement. What it evaluates to is never necessary to know. The "foo unless bar;" is a statement, too. If "bar" is false, it obviously evaluates to "foo". But if "bar" is true, this whole statement, unexpectedly to me as of yesterday, evaluates to "bar":

    >perl -wE "$x=33; say do{42 unless $x}" 33

    The (wrong) intuition was that result would be something like "undef". But, if I get RonW's explanation right, the "evaluator" is more straightforward and "primitive" -- it doesn't try to "understand" statement as a whole, but whatever subexpression was evaluated last is taken as result for a whole.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2019-04-25 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (127 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!