http://qs321.pair.com?node_id=752512

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

Attention: Question Resolved by Reading the Fine Manual (More closely)

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:

Hello. Goodbye. Maybe.
then it becomes:
Hello. Goodbye. Maybe. Hello. Goodbye. Maybe. Hello. Goodbye. Maybe.
in the original file, and two repetitions in the second file.

Why?

for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";