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

Hi,
I am trying to open a file, and edit some chnages in it. For some reason I cannot open a file for both read and write at the same time, and update it. Please see what I am doing wrong in the following subroutine
sub reset ($@) { my $dir = shift @_; my @nums = @_; opendir (DIRHANDLE, $dir) or die "cannot open directory $dir \n"; my @files = readdir (DIRHANDLE); close DIRHANDLE; my $file; foreach $file (@files) { #print "$dir/$file \n"; if (-d "$dir/$file" && $file ne "." && $file ne "..") { &reset("$dir/$file",@nums); } elsif ($file eq "allstar_header.db") { print "$dir/$file \n"; open (FILEHANDLE, "+<$dir/$file") or die "Can't open $dir/ +$file \n"; while (<FILEHANDLE>) { if ($_ =~ /\s*solver_version_major_h.*/) { print $_; s/\d+/$nums[0]/; print "\t $_"; } if ($_ =~ /\s*solver_version_minor_h.*/) { print $_; s/\d+/$nums[1]/; print "\t $_"; } if ($_ =~ /\s*solver_version_fix_h.*/) { print $_; s/\d+/$nums[1]/; print "\t $_"; } } close FILEHANDLE; } } return 0; }

Replies are listed 'Best First'.
Re: update a file
by tachyon (Chancellor) on Sep 26, 2001 at 20:25 UTC

    Although I agree doesn't work is next to useless as an explanation it appears from your code that your presume that opening a file for Read/Write using the "+<" syntax will let you read a line into $_, modify that line using a regex s/this/that/ and then print that line back into the file using print $_. You can't do that using "+<". Each time you read a line you move the file position pointer 1 lines worth of bytes. If you then print to the file (via a print FILEHANDLE $stuff you will write at the current file postion pointer location and thus overwrite the data immediately following the line you just read rather than replace it.

    What you want to do is called inplace editing. Perl does this with ease. Do a Super Search for "inplace edit" in the text to find exactly the code I presume you want. Here is one link Re: Read from a file and replace

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: update a file
by davorg (Chancellor) on Sep 26, 2001 at 20:06 UTC

    Well, for one thing, you're not actually printing any data into the file anywhere in that code.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: update a file
by converter (Priest) on Sep 26, 2001 at 20:06 UTC

    "Doesn't work" isn't very helpful. You should include your error message or tell us what you're expecting the code to do and how it fails to meet your expectations.

    Also, reset is a Perl built-in function. The way you're calling it ensures that your user-defined subroutine will be called, but it's kind of "icky" to use built-ins' names for user-defined subroutines.

Re: update a file
by Anarion (Hermit) on Sep 26, 2001 at 20:12 UTC
    You have to select the HANDLER berfore printing, you just modify the variable in memory.

    $anarion=\$anarion;

    s==q^QBY_^=,$_^=$[x7,print

Re: update a file
by RayRay459 (Pilgrim) on Sep 26, 2001 at 20:19 UTC
    jalebie
    You're not printing anything out to your filehandle. what are you actually trying to print out? Also, to append to a file you have to use the >> in your open statement ...like
    open(OUT,">>filename") or die "$!";
    and when you want to print to that filehandle, use
    print OUT $stuff;
    Good luck.
    Ray