Modification of a read-only value attempted

J

Jerry Preston

I do not understand this at all:

local( *FI );
open( FI, $file ) || die "cannot open input file $file: ";
while(<FI>) { # I get "Modification of a read-only value attempted at
$file this line"

I have done this countless number of times. What is going on?

Thanks,

Jerry
 
J

Jerry Preston

I not sure what the deal is, but my code is working like it should for no
reason that I can see!

Regards,

Jerry
 
A

A. Sinan Unur

I do not understand this at all:

local( *FI );
open( FI, $file ) || die "cannot open input file $file: ";
while(<FI>) { # I get "Modification of a read-only value
attempted at
$file this line"

Funny ... the error message I get is different:

D:\Home\asu1\UseNet\clpmisc> t
Bareword found where operator expected at D:\Home\asu1\UseNet\clpmisc\t.pl
line 7, near "$file this"
(Missing operator before this?)
syntax error at D:\Home\asu1\UseNet\clpmisc\t.pl line 7, near "$file this
line" Can't find string terminator '"' anywhere before EOF at D:\Home\asu1
\UseNet\clpmisc\t.pl line 7.
I have done this countless number of times. What is going on?

I do not know. But please read the posting guidelines for this group
before your next post.

Sinan.
 
B

Brian McCauley

Jerry said:
I do not understand this at all:

local( *FI );
open( FI, $file ) || die "cannot open input file $file: ";
while(<FI>) { # I get "Modification of a read-only value attempted at
$file this line"

I have done this countless number of times. What is going on?

The while(<HANDLE>) construct does not localize $_ it stomps all over
whatever's happens to be in $_. Since $_ is typically an alais to
something else it also stomps all over that something else. If you are
lucky then the something else is a reaonly value and you get an error
rather than random corruption of another variable.

Some people will tell you to localize $_ beforehand using local($_) but
this is not safe. Thay are wrong, do not do that. If anyone ever tells
you to do that then tell them that they are wrong. local($_) does nasty
things when $_ happens to be an alias to an element of a tied agregate.

If you want to use this technique (other than at the top-level of very
small scripts, and particularly within subroutines) then you should
local(*_) beforehand. This, unfortunately will make @_ inaccessible so
make sure you've got everything you need from it first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top