Problems? Is your data what you think it is? | |
PerlMonks |
Re: Appending to a file.by tobyink (Canon) |
on May 07, 2012 at 16:02 UTC ( [id://969288]=note: print w/replies, xml ) | Need Help?? |
Before I say anything else, I'll warn you against that little ($$) thing. That's called a "prototype" and it doesn't do what you think it does. In most situations, prototypes are unhelpful. Anyway, your problem is that you read each line of the file into memory, do something with it, and then don't write it anywhere. What you want to do is be opening both an input filehandle and an output filehandle, and writing the line to the output filehandle when you're done with it. Something like this:
(Note to pedants: for cross-platform compatibility unlink 'filename' is better written as 1 while unlink 'filename' because some operating systems allows multiple versions of the same file to be stored.) There are various modules that make line-by-line file editing easier. Corion mentioned Tie::File. I happen to be a fan of File::Slurp. Here's an example using that...
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
In Section
Seekers of Perl Wisdom
|
|