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

A simple newbie question
How do I open a file for both read and write using one open statement ?
Do I have to open a file for read, close it then reopen it for write ?
I have a need to read a value from a file, then overwrite the value once it's been read

Replies are listed 'Best First'.
Re: open file for read/write
by davorg (Chancellor) on Dec 08, 2005 at 13:07 UTC

    Now that you have your answer, I wonder if you'd care to suggest how we could improve the documentation for open to make it more obvious how to solve your problem. Currently the docs say this:

    You can put a '+' in front of the '>' or '<' to indicate that you want both read and write access to the file; thus '+<' is almost always preferred for read/write updates--the '+>' mode would clobber the file first.
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: open file for read/write
by Perl Mouse (Chaplain) on Dec 08, 2005 at 12:59 UTC
    open my $handle, "+<", "/your/file/here" or die "Failed to open file: +$!";
    Don't forget to seek before you write.
    Perl --((8:>*
Re: open file for read/write
by gopalr (Priest) on Dec 08, 2005 at 13:12 UTC
      Excellent this is just what I was looking for Thanks
Re: open file for read/write
by l.frankline (Hermit) on Dec 08, 2005 at 15:01 UTC
    open(FH, "+<", "$filename") or die "Opening: $!"; @ARRAY = <FH>; # change ARRAY here seek(FH,0,0) or die "Seeking: $!"; for($i=0;$i<=$#ARRAY;$i++) { $line = $ARRAY[$i]; $line=~s#find#replace#; $ARRAY[$i] = $line; } print FH @ARRAY or die "Printing: $!"; truncate(FH,tell(FH)) or die "Truncating: $!"; close(FH);

    regards
    Franklin

    Don't put off till tomorrow, what you can do today.

Re: open file for read/write
by GrandFather (Saint) on Dec 09, 2005 at 03:58 UTC

    If you are simply passing through the file editing it then a common Perl idiom is to use inplace edit thus:

    local @ARGV = ('nameOfFileToEdit'); local $^I = '.bak'; # extension for backup version of file while (<>) { #line read is in $_ print "new state of line\n"; # write updated line }

    DWIM is Perl's answer to Gödel
Re: open file for read/write
by ikegami (Patriarch) on Dec 08, 2005 at 16:15 UTC
    You might be intersted in Tie::File, which treats a file as an array. Any changes to the array translates to changes in the file.
Re: open file for read/write
by vennirajan (Friar) on Dec 09, 2005 at 08:18 UTC

    you can put a ’+’ in front of the ’>’ or ’<’ to indicate that you want both read and write access to the file; thus ’+<’ is almost always preferred for read/write updates--the ’+>’ mode would clobber the file first. You can’t usually use either read-write mode for updating textfiles, since they have variable length records.

    The most important thing when you open file in both the modes is you have to use the "seek" functions effectively. Wrong read/write operation leads to data loss or it will create holes in the files.

    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                    -- BK Systems.