Tony Lawrence
2006-09-15 16:37:09 UTC
Can someone explain this to me?
(full text at http://aplawrence.com/Unix/perl_readonly.html ,
abbreviated version here)
Test code:
#!/usr/bin/perl -w
# no problem here
caroomba("first");
@dayval=qw(foo ba);
foreach $dayval (@dayval) {
# no problem here
caroomba($dayval);
}
foreach $dayval ("foo2","ba2") {
# no problem here either
caroomba($dayval);
}
foreach ("foo3","ba3") {
# doesn't like this
$dayval=$_;
caroomba($dayval);
}
sub caroomba {
my $p=shift;
print "Caroomba called $p\n";
open(I,"./t");
while (<I>) {
# stuff..
}
close I;
}
When run, that produces:
Caroomba called first
Caroomba called foo
Caroomba called ba
Caroomba called foo2
Caroomba called ba2
Caroomba called foo3 Modification of a read-only value attempted at
./t.pl line 23, <I> line 23.
Why?
Something to do with anonymous arrays, but I don't grok it.
(full text at http://aplawrence.com/Unix/perl_readonly.html ,
abbreviated version here)
Test code:
#!/usr/bin/perl -w
# no problem here
caroomba("first");
@dayval=qw(foo ba);
foreach $dayval (@dayval) {
# no problem here
caroomba($dayval);
}
foreach $dayval ("foo2","ba2") {
# no problem here either
caroomba($dayval);
}
foreach ("foo3","ba3") {
# doesn't like this
$dayval=$_;
caroomba($dayval);
}
sub caroomba {
my $p=shift;
print "Caroomba called $p\n";
open(I,"./t");
while (<I>) {
# stuff..
}
close I;
}
When run, that produces:
Caroomba called first
Caroomba called foo
Caroomba called ba
Caroomba called foo2
Caroomba called ba2
Caroomba called foo3 Modification of a read-only value attempted at
./t.pl line 23, <I> line 23.
Why?
Something to do with anonymous arrays, but I don't grok it.