New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
foreach loop and local-to-file my variable #9785
Comments
From vincent-perl@vinc17.netCreated by vincent@vinc17.orgNote: the following can be seen either as a perl bug or as Consider the following cases: $ perl -e 'foreach $i (1,2) { &foo } sub foo { print "!$i\n" }' The result of the second test looks incorrect. The perlsyn(1) man page says under "Foreach Loops": The "foreach" loop iterates over a normal list value and sets the For the second test, we are in the 3rd case: If the variable was previously declared with "my", it uses that The variable is *not* lexically scoped, so that the $i of the Now concerning its value, the variable is localized, but this is just Perl Info
|
From @davidnicolInline Patchdiff --git a/pod/perlsyn.pod b/pod/perlsyn.pod
index 2ba30d8..e466ea8 100644
--- a/pod/perlsyn.pod
+++ b/pod/perlsyn.pod
@@ -405,14 +405,12 @@ X<readline> X<< <> >>
X<for> X<foreach>
The C<foreach> loop iterates over a normal list value and sets the
-variable VAR to be each element of the list in turn. If the variable
-is preceded with the keyword C<my>, then it is lexically scoped, and
-is therefore visible only within the loop. Otherwise, the variable is
-implicitly local to the loop and regains its former value upon exiting
-the loop. If the variable was previously declared with C<my>, it uses
-that variable instead of the global one, but it's still localized to
-the loop. This implicit localisation occurs I<only> in a C<foreach>
-loop.
+variable VAR to be each element of the list in turn. If the variable is
+preceded by or was previously declared with the keyword C<my>, then it
+is lexically scoped, and is visible only within the loop. Otherwise,
+the variable is implicitly local to the loop and regains its former
+value upon exiting the loop. This implicit localisation occurs I<only>
+in a C<foreach> loop.
X<my> X<local>
The C<foreach> keyword is actually a synonym for the C<for> keyword, so
@@ -424,7 +422,7 @@ X<$_>
If any element of LIST is an lvalue, you can modify it by modifying
VAR inside the loop. Conversely, if any element of LIST is NOT an
lvalue, any attempt to modify that element will fail. In other words,
-the C<foreach> loop index variable is an implicit alias for each item
+the C<foreach> loop index variable gets aliased to each item
in the list that you're looping over.
X<alias> |
The RT System itself - Status changed from 'new' to 'open' |
Migrated from rt.perl.org#67162 (status was 'open')
Searchable as RT67162$
The text was updated successfully, but these errors were encountered: