Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Type Globs

by saucepan (Scribe)
on Jan 04, 2001 at 07:49 UTC ( #49693=note: print w/replies, xml ) Need Help??

in reply to Type Globs

The other answers are better, but here's a unixy "no modules" solution just for the heck of it:
# Usage: $teed_fh = tee @output_handles; # Returns a new filehandle that, when written, copies to all @output_h +andles sub tee { use vars qw(*__TEE_FH); my $tfh = do { \local *__TEE_FH }; # XXX Should use IO::Handle ins +tead defined( my $pid = open($tfh, "|-") ) or die "fork: $!"; return select((select($tfh), $| = 1)[0]) if $pid; for (@_) { select($_); $| = 1 } while( sysread(STDIN, my $block, 8192) ) { print $_ $block for @_ +} kill 9,$$ or exit; # XXX Should use POSIX::_exit instead }
and an example using it to do what you wanted:
# Tee STDOUT to a log file use vars qw(*LOG); open LOG, ">>log.txt" or die "log.txt: $!"; *STDOUT = tee(*STDOUT, *LOG); END { close STDOUT and wait } # Now do something that writes to STDOUT print "Hello, world #$_!\n" for (1..20); # XXX But, should have just used IO::Tee in first place :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://49693]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-03-29 03:21 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (70 votes). Check out past polls.