pobocks has asked for the wisdom of the Perl Monks concerning the following question:
The second way is to multiplex input from one input handle to zero or more output handles as it is being read. The IO::Tee constructor, given an input handle followed by a list of output handles, returns a tied handle that can be read from as well as written to. When written to, the IO::Tee object multiplexes the output to all handles passed to the constructor, as described in the previous paragraph. When read from, the IO::Tee object reads from the input handle given as the first argument to the IO::Tee constructor, then writes any data read to the output handles given as the remaining arguments to the constructor.
Emphasis mine. I apologize very deeply; I don't know how I missed this on first read through the POD.
So, this bit of nonsense is supposed to use IO::Tee to read from a filehandle, and then write back (append) to a filehandle attached to the same file, as well as another filehandle.
It works, for certain values of work; the issue is that the text is doubled in both the original and the second file.
use strict; use warnings; use IO::Tee; open my $read, '<', 'Fanggame.txt'; open my $write, '>>', 'Fanggame.txt'; open my $log, '>>', 'loggy.txt'; my $tee_fh = IO::Tee->new($read, $write, $log); my @lines = <$tee_fh>; foreach (@lines){ s/the/beeswax/g; print {$tee_fh} $_; }
Random Example: If the source file is:
then it becomes:Hello. Goodbye. Maybe.
in the original file, and two repetitions in the second file.Hello. Goodbye. Maybe. Hello. Goodbye. Maybe. Hello. Goodbye. Maybe.
Why?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: IO::Tee problem (Well, problem with my brain, anyway)
by Marshall (Canon) on Mar 23, 2009 at 08:49 UTC | |
Re: IO::Tee problem (Well, problem with my brain, anyway)
by apl (Monsignor) on Mar 23, 2009 at 11:11 UTC | |
by pobocks (Chaplain) on Mar 23, 2009 at 16:26 UTC | |
by apl (Monsignor) on Mar 23, 2009 at 17:15 UTC | |
by pobocks (Chaplain) on Mar 23, 2009 at 17:45 UTC | |
Re: IO::Tee problem (Well, problem with my brain, anyway)
by zentara (Cardinal) on Mar 23, 2009 at 11:53 UTC | |
by pobocks (Chaplain) on Mar 23, 2009 at 16:26 UTC | |
by zentara (Cardinal) on Mar 23, 2009 at 17:07 UTC |