Ok, folks: I don't understand this. It must have something to do with anonymous arrays in Perl (no, it doesn't, I realize now), but I don't grok the connection. I ran into this in attempting a seemingly simple change in some customer's code; it wasn't hard to fix, but I simply do not understand why this happened.
Well, that's not precisely true: it happened because I went ahead in a hurry and added something "quick". It was just a new loop around some existing code. Ordinarily I would have written it like this:
@foo=qw(foo ba);
foreach $loopvar (@foo) {
..
But for some reason I did this instead:
foreach("foo","ba") {
$loopvar=$_;
..
Why? I don't know. Why did I grab brown pants this morning instead of blue? Who knows.
Anyway, that caused a strange error in a subroutine. I can duplicate it with simple 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? Dude, werent you listening? I do not comprehend why. Some person with more brains than I currently have will have to explain that one to both of us.
Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)
| Views for this page | ||||
|---|---|---|---|---|
| Today | This Week | This Month | This Year | Overall |
| 1 | 1 | 1 | 254 | 3,060 |
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Fri Sep 15 18:53:34 2006: Subject: TonyLawrence
I got some answers at http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/a8837e91f0314ed0/9030124c236e8ddf#9030124c236e8ddf
but honestly they make no sense to me. That is, I understand *what* they are saying, but it isn't clear to me why it should work that way.
Sat Sep 16 15:15:45 2006: Subject: TonyLawrence
With a little more help from the newsgroups, I get it. I simply had misunderstood $_, not realizing that it is a package variable. That confusion came from it being localized in loops.
The Camel book actually tells you that the default variable for angle bracket input is the global $_ and not the local $_ (p.81 of the 3rd edition, otherwise look for the section on Line Input (Angle) Operator).
So that's what bit me.
Sat Dec 29 13:28:50 2007: Subject: TonyLawrence
In the just released 5.10 Perl , $_ can be made local:
http://search.cpan.org/dist/perl-5.10.0/pod/perl5100delta.pod#Lexical_$_
If you have done "my $_", you can still get to the global $_ with "$::_," or "our $_".
Add your comments