summaryrefslogtreecommitdiff
path: root/pod/perlsyn.pod
diff options
context:
space:
mode:
authorJames E Keenan <jkeenan@cpan.org>2020-11-04 21:11:53 -0500
committerJames E Keenan <jkeenan@cpan.org>2020-12-26 20:17:34 -0500
commitdba7cf2eab9433e2b03074d49e3473dfbf0d85b1 (patch)
tree80dfbbbaae2a07bac5581daa2eee21c4255a84d9 /pod/perlsyn.pod
parent345560c9fb124eb9b3b2788d5bb036f15ec8b64a (diff)
downloadperl-dba7cf2eab9433e2b03074d49e3473dfbf0d85b1.tar.gz
Provide code example for 'my' declared in initialization of 'for' loop
For: https://github.com/Perl/perl5/issues/18260
Diffstat (limited to 'pod/perlsyn.pod')
-rw-r--r--pod/perlsyn.pod16
1 files changed, 15 insertions, 1 deletions
diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod
index 81270f13f6..a96331fe2e 100644
--- a/pod/perlsyn.pod
+++ b/pod/perlsyn.pod
@@ -455,9 +455,23 @@ is the same as this:
There is one minor difference: if variables are declared with C<my>
in the initialization section of the C<for>, the lexical scope of
those variables is exactly the C<for> loop (the body of the loop
-and the control sections).
+and the control sections). To illustrate:
X<my>
+ my $i = 'samba';
+ for (my $i = 1; $i <= 4; $i++) {
+ print "$i\n";
+ }
+ print "$i\n";
+
+when executed, gives:
+
+ 1
+ 2
+ 3
+ 4
+ samba
+
As a special case, if the test in the C<for> loop (or the corresponding
C<while> loop) is empty, it is treated as true. That is, both