Re: why not listed foreach and if?
by ikegami (Patriarch) on Jun 17, 2024 at 17:58 UTC
|
It's quite dangerous to readability to use flow control structure that are tail-loaded. As you said, they would require reading from right-to-left (or bottom-to-top), but Perl is otherwise arranged in a top-to-bottom, left-to-right order. Tail-loaded control structures are only acceptable, if ever, when the body is minimal. Your second snippet is a prime example of horrible code.
So flow control modifiers can't be tacked onto other flow control structures, including other flow control modifiers.
Correct bracketing:
if ( $a ) {
print for @b;
}
The following might be an option:
next if !$a; # Or `return`
print for @b;
If you don't set $\ and $,, the first snippet above is equivalent to:
print @b if $a;
| [reply] [d/l] [select] |
|
why the restriction? perl feed subs from right to left, why cannot listed foreach and if or any other control structure. Perl do checks if tail-loaded, and why not keep on? as long as there is no ambiguity.
| [reply] |
|
Because for readability and thus understandability and maintainability the important stuff should come first. A trailing if that determines if anything is performed at all makes the code much harder to understand because when you reach the if you have to revise your entire mental model of the statement to that point.
Somewhat like the infamous instructions for disarming a bomb:
- ...
- Cut the blue wire
- But first cut the red wire
- ...
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
| [reply] |
|
|
|
|
why the restriction?
It's just that perl's parsing does not handle these types of constructs.
It interprets:
foreach @a if $a
as
foreach(@a if $a)
foreach takes a list as its argument, and I guess perl just doesn't see (@a if $a) as being a valid list.
You know what you mean, and I know what you mean, but perl doesn't.
I don't know exactly why perl doesn't understand these constructs ... but I suspect there are good reasons.
Cheers, Rob
| [reply] [d/l] [select] |
|
|
|
|
perl feed subs from right to left,
I don't know what that means? Both sub definitions and sub calls read from left to right.
Perl do checks if tail-loaded,
No, that's not the case for either the do function or the do block. Both start with do. They read from left to right.
Are you talking about do ... if ...;, do ... for ...; and , do ... while ...;? Those are just statement modifiers on a expression. No different than next if ...;, etc.
And again, you keep giving examples that defy your point. do ... while ...; is rarely used, I've never seen the others used, and I actively avoid them all.
| [reply] [d/l] [select] |
|
|
|
|
Re: why not listed foreach and if?
by dave_the_m (Monsignor) on Feb 02, 2025 at 00:11 UTC
|
Larry Wall had a linguistic background, so perl tends to allow English-like syntax. For example in English we might say:
Let's have a picnic tomorrow, unless it rains.
Lets have a picnic every day next week.
which correspond in perl to:
have_picnic($tomorrow) unless $it_rains;
have_picnic($_) for @days;
But in English, such suffix modifiers don't stack well:
Lets have a picnic every day next week unless it rains.
If it rains on the first day, do we cancel just that day or the whole week?
So Larry, for linguistic reasons, limited perl to a single statement modifier.
Dave. | [reply] [d/l] |
Re: why not listed foreach and if?
by eyepopslikeamosquito (Archbishop) on Jun 18, 2024 at 00:59 UTC
|
| [reply] [d/l] [select] |
Re: why not listed foreach and if?
by The_Dj (Scribe) on Jun 19, 2024 at 00:48 UTC
|
| [reply] |
Re: why not listed foreach and if?
by Danny (Chaplain) on Jun 17, 2024 at 17:44 UTC
|
Perhaps you are looking for map?
perl -wE 'my @a = (0,2,3); map say, @a if $a[1]'
| [reply] [d/l] |
|
| [reply] [d/l] |
|
| [reply] |
|
|
no, just curious about the restriction, looking for some reasons backed by compile theory.
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |