APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > Unix Scripts > Perl anonymous arrays cause strange read-only error
Printer Friendly Version




Perl loop causes strange read-only error




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.


Technorati tags:
If this page was useful to you, please click to help others find it:  
Your +1's can help friends, contacts, and others on the web find the best stuff when they search.


3 comments




More Articles by Anthony Lawrence - Find me on Google+



Click here to add your comments





Fri Sep 15 18:53:34 2006:   TonyLawrence

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

gravatar
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 $_".




Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



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.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




Buy Kerio from a dealer who knows tech: I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals
pavatar.jpg

This post tagged:

       - Perl
       - Programming




Unix/Linux Consultants

Skills Tests

Guest Post Here