in reply to why not listed foreach and if?

Perhaps you are looking for map?
perl -wE 'my @a = (0,2,3); map say, @a if $a[1]'

Replies are listed 'Best First'.
Re^2: why not listed foreach and if?
by Fletch (Bishop) on Jun 17, 2024 at 22:55 UTC

    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.

        And the very next part of that sentence then reads "and composes a list of the results of each such evaluation." It's not for general purpose iteration.

        Also quoth Perl Best Practices (Chapter 6):

        This function [map] is specifically aimed at those situations when you want to process a list of values, to create some kind of related list.

        In fact for a while (until 5.8-ish? looks like) there actually was a performance hit for (bad) code misusing map because the return value wasn't optimized away in void context so you'd pay to make the list of transformed values even though they weren't used.. Use for (or foreach or while) for general iteration, use map for transformation, or grep for filtering; otherwise you're misleading readers about what code is doing.

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

Re^2: why not listed foreach and if?
by vincentaxhe (Scribe) on Jun 17, 2024 at 18:13 UTC
    no, just curious about the restriction, looking for some reasons backed by compile theory.