[Chicago-talk] Sharing my pain
Jonathan Rockway
jon at jrock.us
Sat Oct 14 23:35:23 PDT 2006
> strict/warnings won't help you; the $sp inside the foreach is *not* the
> 'my'-ed $sp from the line before. It gets silently localized w/i the
The real subtlety is that $sp is actually the list element that you're
iterating over:
@foo = (1, 2, 3);
print "@foo"; # 1 2 3
foreach my $sp (@foo){
$sp++;
}
print "@foo"; # 2 3 4
The same caveat applies to map and grep, of course. Don't let the "my" trick
you into thinking you made a copy, like you do here:
my $a = 0;
foo($a);
print "$a\n";
sub foo {
my $bar = shift; #
$bar++; # try $_[0]++ instead
}
(To achieve that in a for loop, do C<do {my $element = $_; ... } foreach
@elements> or something similar.)
Regards,
Jonathan Rockway
--
package JAPH;use Catalyst qw/-Debug/;($;=JAPH)->config(name => do {
$,.=reverse qw[Jonathan tsu rehton lre rekca Rockway][$_].[split //,
";$;"]->[$_].q; ;for 1..4;$,=~s;^.;;;$,});$;->setup;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 307 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/chicago-talk/attachments/20061015/0e21a20e/attachment.bin
More information about the Chicago-talk
mailing list