Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

IO::Tee problem (Well, problem with my brain, anyway)

by pobocks (Chaplain)
on Mar 23, 2009 at 05:48 UTC ( #752512=perlquestion: print w/replies, xml ) Need Help??

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";

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
    Can you explain more about what you are trying to do?

    I don't know about IO:Tee, but "tee" is a very standard and commonly used program. Tee is available on all platforms including Windows.

    The basic job of tee is to take STDIN and send a line to STDOUT and also to a file. Tee.pl is simple.

    #!/usr/bin/perl -w use strict; # tee program $| =1; #sometimes important, flushes I/O for each write. sub usage () { print "TEE USAGE:\n tees stdout to a file and to stdout\n". " program | tee outfile\n"; exit(0); } my $filename = shift @ARGV; usage unless $filename; open (OUTFILE, ">$filename") or (die "Can't open OUTFILE: $!"); while (<>) { print; print OUTFILE; }
Re: IO::Tee problem (Well, problem with my brain, anyway)
by apl (Monsignor) on Mar 23, 2009 at 11:11 UTC
    Your code contains:
    open my $read, '<', 'Fanggame.txt'; open my $write, '>>', 'Fanggame.txt';

    That is, you open for input and appending for output the same file.

    Every time you run the program, you should double the size of your input file. Run it again; how much longer does the file get?

      The problem is that the input file triples, while the output file doubles.

      As far as I can tell, I'm slurping the entire file (previous to any additions), and then foreaching over a simple array and printing the file once. I should get a doubled copy in the input file, and a single copy in the output file.

      I've checked (with a print statement) and @lines contains only a single copy of the input file.

      for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
        Why do you want to echo your input back to your input file?
Re: IO::Tee problem (Well, problem with my brain, anyway)
by zentara (Archbishop) on Mar 23, 2009 at 11:53 UTC

      (IO::)Tee hee!

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2022-09-25 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?