Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

perl open mode

by zer (Deacon)
on Oct 24, 2007 at 00:12 UTC ( #646788=perlquestion: print w/replies, xml ) Need Help??

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

Evening,

I was fiddling about with the open mode '+<'.

open (A, '+<', "somefile.html") or die $!; print A map{s/a/b/g;$_} <A>;
I know this is a bit of a mash up. But why is it that the files come out appended to as opposed to just over written. From what this feels like perl is acting like i called a '+>>'. Perl 5.8.8

Thanks

Replies are listed 'Best First'.
Re: perl open mode
by Sidhekin (Priest) on Oct 24, 2007 at 00:22 UTC

    As you are reading from the file handle in list context, you advance its position until eof.

    Then you print. At the former eof.

    (If you had opened with +>>, you would not have been able to read anything without seeking first.)

    Update: For what you want, and with a tiny bit more clarity:

    open (A, '+<', "somefile.html") or die $!; s/a/b/g for my @records = <A>; seek(A, 0, 0); print A @records;

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

      That works if the replacement is character for character. You may be surprised by the results if the replacements are shorter than the original value.

      open (A, '+<', "somefile.html") or die $!; s/a/b/g for my @records = <A>; seek(A, 0, 0); ### truncate (A, 0) or die "can not truncate file to zero length\n"; ### print A @records;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2020-12-02 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (28 votes). Check out past polls.

    Notices?