Today I wrote a script, in which I preferred some logic expressed as 2nd fragment rather than the 1st:

for ( $x .. $y ) { # some loop # ... skipped ... # fragment #1: next unless exists $gs_val-> { HT }; my $ht = $doc-> getValue( $gs_val-> { HT }); next unless exists $ht-> { Default }; my $df = $doc-> getValue( $ht-> { Default }); next unless exists $df-> { TransferFunction }; my $tf = $doc-> getValue( $df-> { TransferFunction }); next unless $tf eq 'Default; # ... skipped ... # fragment #2: next unless $doc-> getValue( $doc-> getValue( $doc-> getValue( $gs_val-> { HT } || next ) -> { Default } || next ) -> { TransferFunction } || next ) eq 'Default'; # ... skipped ... }

As you see, I tried to improve the #1 with a few blank lines, but, to me, the #2 is more readable -- now, and I hope it will remain so in a year, when I have to try hard to remember what it all was about. Perhaps not everyone will agree.

However, while experimenting and testing to see if such constructs are OK, I was surprised to find that Perl prefers even more unexpected form:

>perl -we "for(\0,0){print${$_||next}}" 0 >perl -MO=Deparse -we "for(\0,0){print${$_||next}}" BEGIN { $^W = 1; } foreach $_ (\0, 0) { print ${next unless $_;}; } -e syntax OK >perl -we "for(\0,0){print${next unless $_}}" 0 >

Looks like if "or next" is part of block that is supposed to return value, then Perl replaces it with "next unless" syntax. What's totally unexpected is that this "next unless" thing is actually valid and does return value.

I can only guess that everything works the other way, in reality: "next unless" is optimized to "or next", therefore it returns value, and that for some strange reason B::Deparse shows "||next" as "next unless". That is only way this can be sane... Or is it not so? :)


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.