Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

why not listed foreach and if?

by vincentaxhe (Scribe)
on Jun 17, 2024 at 17:31 UTC ( [id://11160035]=perlquestion: print w/replies, xml ) Need Help??

vincentaxhe has asked for the wisdom of the Perl Monks concerning the following question:

why can't I write something like
print foreach @a if $a;
or
foreach(@b){ print } if $a;
have to use do{...}, but foreach is a code block, why perl donot deduce from right to left.

Replies are listed 'Best First'.
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;
      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.

        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:

        1. ...
        2. Cut the blue wire
        3. But first cut the red wire
        4. ...

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        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

        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.

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.

Re: why not listed foreach and if?
by eyepopslikeamosquito (Archbishop) on Jun 18, 2024 at 00:59 UTC
Re: why not listed foreach and if?
by The_Dj (Scribe) on Jun 19, 2024 at 00:48 UTC

    I suspect there is some useful info about this Here: https://perldoc.perl.org/perlop


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]'

      map is for transformation of a list of items, not iteration. Using it in void context like this (for the side effect throwing away the return values) is obfuscated code.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        perldoc for map starts by saying that map "Evaluates the BLOCK or EXPR for each element of LIST ...". So map is primarily an iterator.

      no, just curious about the restriction, looking for some reasons backed by compile theory.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2025-06-14 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.