vr has asked for the wisdom of the Perl Monks concerning the following question:
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? :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Abuse of "or next" in expressions and "next" that returns value
by RonW (Parson) on Jul 18, 2018 at 21:00 UTC | |
by vr (Curate) on Jul 19, 2018 at 07:23 UTC | |
|
Re: Abuse of "or next" in expressions and "next" that returns value
by AnomalousMonk (Archbishop) on Jul 18, 2018 at 21:01 UTC | |
|
Re: Abuse of "or next" in expressions and "next" that returns value
by Veltro (Hermit) on Jul 18, 2018 at 21:08 UTC | |
|
Re: Abuse of "or next" in expressions and "next" that returns value
by haukex (Archbishop) on Jul 19, 2018 at 18:34 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |