Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

open file for read/write

by Anonymous Monk
on Dec 08, 2005 at 12:57 UTC ( #515221=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on open file for read/write
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 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

      why didn't you just say RTFM ?
        Because responses like "RTFM" are rude, obscene, and often don't address the actual question being asked.

        One of the nice things about PerlMonks is that many of the monks seem to have patience to deal with newbies in spite of their failure to read and memorize all of the documentation. :)

        If questions like this irritate you, simply don't bother to respond -- others seem more patient and willing to address them.

        Update: I particularly liked davorg's response because it pointed the OP to the relevant documentation as well as (probably tongue-in-cheek) asking if the docs could be improved. This gentle kind of rebuke is much more palatable and is, in my opinion, more in-line with PerlMonks philosophy.

        Speaking as a low-ranking monk, I like the easy questions because it gives me a chance (if I'm fast) to answer from time to time rather than always coming to PM with questions. It may be illusory, but it makes me feel as though I'm giving something back to the community.


        No good deed goes unpunished. -- (attributed to) Oscar Wilde
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 ikegami (Pope) 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 GrandFather (Cardinal) 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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://515221]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-10-24 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (137 votes), past polls