Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to IO::Tee problem (Well, problem with my brain, anyway) by pobocks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-23 09:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found