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:


Hate these ads?

@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:

Comments


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

ad

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


Views for this page
Today This Week This Month This Year  Overall
111254 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.

Publishing your articles here

pavatar.jpg
More:
       - Programming
       - Perl




Unix/Linux Consultants


http://www.breakthru.com.au SCO (Openserver and Unixware), Unix, Solaris and Linux Consulting services including: Secure Networking Solutions; Linux based Firewalls; Backup Solutions; Secure Home to Office Network Setup; Phone, Remote and On-Site Support available - Satisfaction Guaranteed!


http://bcstechnology.net Full service Linux & UNIX systems integrator; Windows to UNIX/Linux Client-Server Specialist; Secure E-Mail & Website Hosting; Thoroughbred Software Developer; Custom Industrial Automation; Hardware & Electronics Experts; In Business Since 1985.


http://www.schewanick.com SCO Unix, Solaris, Linx (various), PHP, MySQL, Apache, uniBasic, dL4, Perl, System Administration and more....



Twitter
  • Nov 30 20:25
    I have 37,000 words of a 50,000 word project. I'd like to finish it this week..
  • Nov 30 20:05
    My wife made turkey sandwiches with stuffing and cranberry orange relish - I did not want to eat the last bite. Didn't want it to end!









Change Congress


Related Posts