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

Perl loop causes strange read-only error

© September 2006 Anthony Lawrence


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

@dayval=qw(foo ba);
foreach $dayval (@dayval) {
# no problem here

foreach $dayval ("foo2","ba2") {
# no problem here either

foreach ("foo3","ba3") {
# doesn't like this

sub caroomba {
my $p=shift;
print "Caroomba called $p\n";
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


Inexpensive and informative Apple related e-books:

Take Control of Numbers

Photos for Mac: A Take Control Crash Course

Take Control of Automating Your Mac

Take Control of IOS 11

Take Control of Apple Mail, Third Edition

More Articles by © Anthony Lawrence

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

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

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

In the just released 5.10 Perl , $_ can be made local:

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


Printer Friendly Version

Have you tried Searching this site?

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

Printer Friendly Version

That's the thing about people who think they hate computers. What they really hate is lousy programmers. (Larry Niven)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode