APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Perl loop causes strange read-only error


2006/09/15

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.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Perl anonymous arrays cause strange read-only error


3 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Fri Sep 15 18:53:34 2006: 2461   TonyLawrence

gravatar
I got some answers at (link)

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: 2465   TonyLawrence

gravatar
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: 3355   TonyLawrence

gravatar
In the just released 5.10 Perl , $_ can be made local:
(link)

If you have done "my $_", you can still get to the global $_ with "$::_," or "our $_".






------------------------
Kerio Samepage


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.

Contact us





We're terrible animals. I think that the Earth's immune system is trying to get rid of us, as well it should. (Kurt Vonnegut)

He who hasn't hacked assembly language as a youth has no heart. He who does as an adult has no brain. (John Moore)












This post tagged: