perlquestion
pobocks
<h1>Attention: Question Resolved by Reading the Fine Manual (More closely)</h1>
<blockquote>
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, <em><strong>then writes any data
read to the output handles given as the remaining arguments to the
constructor.</strong></em>
</blockquote>
<p>Emphasis mine. I apologize very deeply; I don't know how I missed this on first read through the POD.</p>
<hr />
<p>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.</p>
<p>It works, for certain values of work; the issue is that the text is doubled in both the original and the second file.</p>
<code>
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} $_;
}
</code>
<p>Random Example: If the source file is:
<code>
Hello.
Goodbye.
Maybe.
</code>
then it becomes:
<code>
Hello.
Goodbye.
Maybe.
Hello.
Goodbye.
Maybe.
Hello.
Goodbye.
Maybe.
</code>
in the original file, and two repetitions in the second file.</p>
<p>Why?</p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-572803">
<code>for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";</code>
</div></div>