develooper Front page | perl.perl5.porters | Postings from November 2011

Explanation of "for" and "foreach" in perldoc

Thread Next
From:
Ævar Arnfjörð Bjarmason
Date:
November 26, 2011 15:19
Subject:
Explanation of "for" and "foreach" in perldoc
Message ID:
CACBZZX4NSOfB09x4L89Ax7ewKYECcj4Za7-fUM_BdYudoMxcOA@mail.gmail.com
The Perl documentation sometimes refers to "for" as C-style only and
"foreach" as the Perl-style only construct. When actually they're
synonyms for the same thing.

I've pushed the two following patches to avar/foreach-docs that fix a
small amount of this discrepancy, but I wonder why it the
documentation was written like this. Were the two subtly different at
some point in the past, or is it just that the authors of the
documentation wanted people to write "for" for the C-style ones and
"foreach" for the Perl-style ones, even though they're synonyms?

    commit 32d89c44211e3342412d169c036fcb498fba708b
    Author: Ævar Arnfjörð Bjarmason <avar@cpan.org>
    Date:   Sat Nov 26 22:58:22 2011 +0000

        pod/perlsyn: for is equivalent to "foreach'

        Change the Compound Statements section to not like by omission. Both
        for and foreach can be used as C-style and Perl-style for-loops, but
        the documentation pretended that "for" was always C-style and
        "foreach" was always Perl-style.

    diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod
    index 3d4825a..fd4c745 100644
    --- a/pod/perlsyn.pod
    +++ b/pod/perlsyn.pod
    @@ -236,6 +236,9 @@ The following compound statements may be used
to control flow:
         LABEL until (EXPR) BLOCK
         LABEL until (EXPR) BLOCK continue BLOCK
         LABEL for (EXPR; EXPR; EXPR) BLOCK
    +    LABEL for VAR (LIST) BLOCK
    +    LABEL for VAR (LIST) BLOCK continue BLOCK
    +    LABEL foreach (EXPR; EXPR; EXPR) BLOCK
         LABEL foreach VAR (LIST) BLOCK
         LABEL foreach VAR (LIST) BLOCK continue BLOCK
         LABEL BLOCK continue BLOCK
    commit 2fb5ef2fef013ac580eb6e7fd1fc90bde4f30a7a
    Author: Ævar Arnfjörð Bjarmason <avar@cpan.org>
    Date:   Sat Nov 26 23:07:15 2011 +0000

        pod/perlintro: mention that "foreach" is really a synonym for "for"

        The perlintro section on for/foreach would have you believe that "for"
        is always C-style and "foreach" is always Perl-style. This isn't
        actually the case, so link to the perlsyn section that covers that.

    diff --git a/pod/perlintro.pod b/pod/perlintro.pod
    index 5e2fe5c..5a84f32 100644
    --- a/pod/perlintro.pod
    +++ b/pod/perlintro.pod
    @@ -407,6 +407,9 @@ the more friendly list scanning C<foreach> loop.
             print "The value of $key is $hash{$key}\n";
         }

    +The C<foreach> keyword is actually a synonym for the C<for>
    +keyword. See C<L<perlsyn/"Foreach Loops">.
    +
     =back

     For more detail on looping constructs (and some that weren't mentioned in

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About