[Chicago-talk] Sharing my pain

Andy_Bach at wiwb.uscourts.gov Andy_Bach at wiwb.uscourts.gov
Fri Oct 13 13:44:29 PDT 2006

One thing - this syntax:
     my $sp;
     foreach $sp (@tmp) {

is subtly wrong - got this from Damian Conway at YAPC but - even 
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 
use strict;
use warnings;
my @tmp = qw(1 2 3);
my $sp = 'hi';
foreach $sp (@tmp) {
       print "sp: $sp\n";
print "sp: $sp\n";

sp: 1
sp: 2
sp: 3
sp: hi

Some folks try to use it to hold the last index value in a for loop.  I 
guess P6 (and maybe 5.real soon now) fixes this bug.  But:
foreach my $sp (@tmp) {

is what you want, or an explicit 'save' if you want to see the last value:
my $save_sp;
foreach my $sp (@tmp) {
    $save_sp = $sp
print "Last sp value: $save_sp\n";


Andy Bach, Sys. Mangler
Internet: andy_bach at wiwb.uscourts.gov 
VOICE: (608) 261-5738  FAX 264-5932

"The whole point of reserving these namespaces is not to prevent users 
misusing them, but to ensure that when we eventually get around to using a 

particular block name, and those same users start screaming about it, we 
mournfully point to the passage in the original spec and silently shake 
heads. ;-)"

Damian Conway, P6 list

More information about the Chicago-talk mailing list