Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am having a code shown below. this procedure is called as
Infile($filename)
where $filname contains a name of file. i am getting folowing error
Modification of a read-only value attempted at testscript1.pm at line line #(some number).
see the code i had written a comment where error comes. this is first place i am using $status scaler. Ca any one tell what is reason for this. when i change name of $status to $statu , no error comes.
sub InFile { local($outfile) = @_; if( -e $outfile) { if( open(SRC_FILE, $outfile)) { open(DEST_FILE, ">TEMPFILE_TOPLOG"); local($date1) = scalar localtime(); print DEST_FILE (@header); print DEST_FILE ("*** Log generated on $date1\n"); $line = <SRC_FILE>; while($line ne "") { print DEST_FILE ($line); $line = <SRC_FILE>; } close(DEST_FILE); close(SRC_FILE); local($filename1) = "TEMPFILE_TOPLOG"; local($newfile) = $outfile; $status = system("mv $filename1 $newfile");# HERE ERROR COMES if ($status != 0) { print "FAILED WHILE MOVING mv $filename1$newfile\n"; return 1; } } else { print "FAILED WHILE OPENING $outfile\n"; return 1; } } return 0; }

20050301 Janitored by Corion: Added code tags

Replies are listed 'Best First'.
Re: help related to modifiaction of read only value attempted error
by chromatic (Archbishop) on Mar 01, 2005 at 07:57 UTC

    It's not obvious from the code you posted. However, I will guess that if you used my instead of local on your variables as appropriate, you'd clear up the error. Try this line instead:

    my $status = system("mv $filename1 $newfile");

    Of course, I'd write that code as:

    unless (system("mv $filename1 $newfile") == 0) { print "FAILED WHILE MOVING mv $filename1$newfile\n"; return 1; }

    That avoids the variable altogether.

    Another suggestion is to look into the module File::Copy and the operators unlink and rename, so as to avoid unnecessary system calls.

Re: help related to modifiaction of read only value attempted error
by tphyahoo (Vicar) on Mar 01, 2005 at 09:57 UTC
    You should also put
    use strict; use warnings;
    at the top of your code.