The whole point here is to use
$tee->format_write() instead of write. Then you don't have to fool around with
$^ et al. (it all happens automagically). A better way for me to have shown this might have been with more calls to
$tee->format_write(), as in the following modified version of what I posted earlier.
use strict;
use warnings;
use IO::Tee;
my %hash = map { ("key$_", "val$_") } ( 1 .. 5 );
my $key;
format TEE_TOP =
==== Top of Output ====
.
format TEE =
@<<<<< @<<<<<
$key, $hash{$key}
.
# with lexical file handle; can also use bareword handle
open my $outfile, '>', 'junk.out' or die "error opening junk.out: ($!
+)";
my $tee = IO::Tee->new( \*STDOUT, $outfile );
# see docs for IO::Tee and IO::Handle for explanation of these methods
# and others available for proper format use/control
$tee->format_top_name( q/main::TEE_TOP/ );
for $key ( sort keys %hash ) {
$tee->format_write( q/main::TEE/ );
}
from the shell:
-> ./tst-tee-write
==== Top of Output ====
key1 val1
key2 val2
key3 val3
key4 val4
key5 val5
-> cat junk.out
==== Top of Output ====
key1 val1
key2 val2
key3 val3
key4 val4
key5 val5
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.