Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Both read and write

by rpike (Scribe)
on Jan 08, 2008 at 19:50 UTC ( [id://661161]=perlquestion: print w/replies, xml ) Need Help??

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

Running V. 5.8.... and the +> doesn't seem to be working. Could someone post a simple example of writing to a file and then reading all the contents back in using +> or +<? Shouldn't opening, writing to it (print FILEHANDLE "Some text"; my @elems = <FILEHANDLE>;), and then reading in work like this? Thanks in advance. Rob

Replies are listed 'Best First'.
Re: Both read and write
by Errto (Vicar) on Jan 08, 2008 at 20:03 UTC
    You can't read text you've just written because the current position of the filehandle will be after the end of the text you've just written, so if you want to read it you have to seek back to the place you want to read from. At least, that's what I found in a quick test - I don't have much experience with this.
      Laziness, time to try it out, and low confidence (not in that particular order) caused me to post this then. I was thinking I might need to use seek or navigate back to the start of the file (possibly). Thanks for the info, now I'll definitely give it a try when I get back to it.
Re: Both read and write
by cdarke (Prior) on Jan 08, 2008 at 20:38 UTC
    A small tip when using seek: The POSITION argument can be negative. This is useful if you read a record then wish to overwrite it:
    seek HANDLE,-(length $record),SEEK_CUR;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-03-28 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found