Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: read from a file and write into the same file

by goibhniu (Hermit)
on Mar 03, 2008 at 19:49 UTC ( [id://671704]=note: print w/replies, xml ) Need Help??


in reply to read from a file and write into the same file

I think OP's problems were sufficiently pointed out. I was wondering if lexical file handles (open my $fh vs. open FILE) might be useful. What I found was that I successfully separated the input filehandle pointer from the output filehandle pointer, but the input filehandle stopped at the original eof instead of moving on through the new file contents. Could some wiser monks explain that to me?

C:\chas_sandbox>for %f in (a b c d) Do echo %f>>alpha.txt C:\chas_sandbox>echo a 1>>alpha.txt C:\chas_sandbox>echo b 1>>alpha.txt C:\chas_sandbox>echo c 1>>alpha.txt C:\chas_sandbox>echo d 1>>alpha.txt C:\chas_sandbox>perl -le "open my $fh_in, '<', 'alpha.txt'; open my $f +h_out, '>> ', 'alpha.txt';while (<$fh_in>) {chomp;print $fh_out chr(ord($_)+4); l +ast if $_ eq 'v'}" C:\chas_sandbox>type alpha.txt a b c d e f g h


#my sig used to say 'I humbly seek wisdom. '. Now it says:
use strict;
use warnings;
I humbly seek wisdom.

Replies are listed 'Best First'.
Re^2: read from a file and write into the same file
by almut (Canon) on Mar 03, 2008 at 21:13 UTC
    ...but the input filehandle stopped at the original eof

    Presumably just the usual Suffering from Buffering. Adding $fh_out->autoflush(1); (after having opened the output file) should make things behave as you expect (fill the file with letters up to 'z').  You also need to load IO::Handle for the autoflush method to be available — i.e. -MIO::Handle if you want to stick with your one-liner :)

    BTW, there is nothing special with lexical filehandles in this regard, the piece of code would work just as well with normal filehandles like IN, OUT.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found