On Wed, Jun 10, 2009 at 3:28 PM, Amir E. Aharoni<[email protected]> wrote:
> 2009/6/10 Berler Chanan <[email protected]>:
>> In the real world (in my way of thinking)
>> Foreach should have interpreted into a 'for' loop
>>
>> Means:
>> foreach $elm (@elm_arr) ==> should have been: for ($index = 0; $index >=
>> $#elm_arr; $index++) { $elm = $elm_arr[$index]; ..... }
>
> ...But then it would be written in C.
>
> If you want to write it in Perl, use strict.
In general it is a good idea to use strict but in this case it would
have not helped.
You can write
use strict;
my $color;
my @colors = qw(red blue green);
foreach $color (@colors) {
print $color;
}
It would be better to write
use strict;
my @colors = qw(red blue green);
foreach my $color (@colors) {
print $color;
}
as that would make it clearer to the reader that $color does not have
a meaning outside
of the foreach loop.
For this reason IMHO use strict should have allowed this code as well:
use strict;
my @colors = qw(red blue green);
foreach $color (@colors) {
print $color;
}
but it does not work that away.
Anyway, $color is not a copy but an alias to the elements so if
someone changes $color within the
foreach loop it will actually change the values of @colors.
use strict;
my @colors = qw(red blue green);
foreach my $color (@colors) {
$color = "dark$color";
}
print "@colors";
darkred darkblue darkgreen
Gabor
http://szabgab.com/
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl