M
Mark Hobley
The iterator special variable can be used in a foreach loop as follows:
@fruit = ( "apples", "bananas", "cherries" );
foreach (@fruit) {
print "I like $_\n";
}
Is it accepted to use the special variable in a for loop as follows?
use strict;
use warnings;
for ($_=0; $_ <=9; $_++) {
print "$_\n";
}
(The above works and I get no errors.)
Would you expect nested loops using the iterator variable to work?
use strict;
use warnings;
for ($_=0; $_<=3; $_++) {
print "Loop $_\n";
for ($_=0; $_<=6; $_++) {
print "Value $_\n";
}
}
(The above does not work, but I get no warnings)
Could I fix these loops to work by changing variable scope?
Is it valid to change the scope of special variables?
Is the observed behaviour above undefined, or is there a document clarifying
this somewhere?
Mark.
--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE
Telephone: (0121) 247 1596
International: 0044 121 247 1596
Email: markhobley at hotpop dot donottypethisbit com
http://markhobley.yi.org/
@fruit = ( "apples", "bananas", "cherries" );
foreach (@fruit) {
print "I like $_\n";
}
Is it accepted to use the special variable in a for loop as follows?
use strict;
use warnings;
for ($_=0; $_ <=9; $_++) {
print "$_\n";
}
(The above works and I get no errors.)
Would you expect nested loops using the iterator variable to work?
use strict;
use warnings;
for ($_=0; $_<=3; $_++) {
print "Loop $_\n";
for ($_=0; $_<=6; $_++) {
print "Value $_\n";
}
}
(The above does not work, but I get no warnings)
Could I fix these loops to work by changing variable scope?
Is it valid to change the scope of special variables?
Is the observed behaviour above undefined, or is there a document clarifying
this somewhere?
Mark.
--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE
Telephone: (0121) 247 1596
International: 0044 121 247 1596
Email: markhobley at hotpop dot donottypethisbit com
http://markhobley.yi.org/