Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
P is for Practical
 
PerlMonks  

Clarification on the write read mode of filehandling

by I_love_perl (Novice)
on Oct 19, 2013 at 13:16 UTC ( #1058915=perlquestion: print w/ replies, xml ) Need Help??
I_love_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I was trying to understand the write read mode of file handling in perl (+>). For the same I've written the below program.

1 #!/usr/bin/perl 2 use strict; 3 use warnings; 4 5 open my $FH , "+>", "input"; 6 7 my @contents = <$FH>; 8 print "@contents\n"; 9 10 print $FH "4\n"; 11 12 @contents = <$FH>; 13 print "@contents\n"; 14 close($FH); Before executing the perl script: Initial file ananda@ananda-desktop:~/Documents/perl_programs/try$ cat input 1 2 3 ananda@ananda-desktop:~/Documents/perl_programs/try$ perl test.pl ananda@ananda-desktop:~/Documents/perl_programs/try$ cat input 4 ananda@ananda-desktop:~/Documents/perl_programs/try$ After executing the perl script: Updated file

What I was expecting is print statement at line 13 in the code will produce an output of 4. But it did not happen that way. The list was empty and hence the output was blank.

My guess is the write operation into the file has written the data into primary memory and not yet into the secondary storage (Hard disk). The data will be written into secondary storage only when we close the file handle.

If the above assumption is right, then is there any way to store the modified data back to the secondary storage by any other means other than closing the filehandle.

If the above assumption is wrong, can someone explain the reason for the above behaviour

Thanks,

Ananda

Comment on Clarification on the write read mode of filehandling
Download Code
Re: Clarification on the write read mode of filehandling
by Corion (Pope) on Oct 19, 2013 at 13:32 UTC

    A file only has one "position", which is used for reading and writing. If you write to a file, the position advances. If you try to read from the end of a file, you will get nothing. See seek for how to manipulate the read/write position, and tell to find out the current position.

      Hi Corion,

      Thanks a lot. Yes, it was happening because the filehandle was advancing as we write and hence any read operation on the file handle was returning a blank value. I moved the filehandle back to the previous line using seek and now it is working perfectly fine.

Re: Clarification on the write read mode of filehandling
by roboticus (Canon) on Oct 19, 2013 at 13:32 UTC

    I_love_perl:

    From the docs (perldoc -f open:):

    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.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Clarification on the write read mode of filehandling
by Laurent_R (Priest) on Oct 19, 2013 at 16:45 UTC

    The '+>' mode will clobber the file before writing anything to it, it is only useful if you may want to reread what you just wrote. If you want to read and then write (anew), use the '+<' mode.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-04-19 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls