Bug#719342: perlsyn: add for my $i; ; example

jidanni at jidanni.org jidanni at jidanni.org
Sun Aug 11 04:58:37 UTC 2013


Package: perl-doc
Version: 5.14.2-21
Severity: wishlist
File: /usr/share/man/man1/perlsyn.1.gz

perlsyn says

   For Loops
       Perl's C-style "for" loop works like the corresponding "while" loop;
       that means that this:

           for ($i = 1; $i < 10; $i++) { #BUT HOW DOES ONE USE my HERE the reader wonders
               ...
           }

       is the same as this:

           $i = 1;
           while ($i < 10) {
               ...
           } continue {
               $i++;
           }

       There is one minor difference: if variables are declared with "my" in
       the initialization section of the "for", the lexical scope of those
       variables is exactly the "for" loop (the body of the loop and the
       control sections).

However the reader may not be exactly sure where to put the "my".

He looks down further and finds

           for my $elem (@elements) {

and

           OUTER: for my $wid (@ary1) {
           INNER:   for my $jet (@ary2) {

But that is for a foreach style for loop.


He indeed finds

       Here's how a C programmer might code up a particular algorithm in Perl:

           for (my $i = 0; $i < @ary1; $i++) {

But that is "how a C programmer might code up a particular algorithm in
Perl", so that is to be avoided...




More information about the Perl-maintainers mailing list