Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Type Globs

by madhatter (Sexton)
on Jan 04, 2001 at 05:06 UTC ( #49663=perlquestion: print w/ replies, xml ) Need Help??
madhatter has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the infamous use constant DEBUG => 1; system. I want all the displayed information to also be dumped to a file. Of course, *STDOUT = *FILEHANDLE if DEBUG; merely redirects the displayed info into the file.

Is there a way to have STDOUT both displayed to the screen and dumped to a file?

Thanks,
madhatter

Comment on Type Globs
Re: Type Globs
by autark (Friar) on Jan 04, 2001 at 05:56 UTC
    You could try to define your own print function, sub print { ... } and there print to both STDOUT and files.

    However, if you are on a unix system you might find the tee utility program usefull

    ~ 1>whatis tee tee (1) - read from standard input and write to standard output and +files
    Autark.
      Says autark:
      You could try to define your own print function, sub print { ... }

      You can't override print.

        I don't see why it has to be called print (this was mentioned as a specific error reporting problem, I believe)...

        sub writeError { print STDERR @_; print FILE @_; # yes I know that should be Mail::Mailer, but I don't know its synta +x open MAIL, '|/usr/sbin/sendmail admin' or die; print MAIL @_; close MAIL; }
        Assuming FILE is already open and is avalible from there.

        This solution also has the advantage of allowing simple and understandable maintainability.

Re: Type Globs
by chromatic (Archbishop) on Jan 04, 2001 at 06:05 UTC
    You could use tie and write your own code to loop through an array of filehandles. I'd probably start out with IO::Tee or something similar, though.
Re: Type Globs
by saucepan (Scribe) on Jan 04, 2001 at 07:49 UTC
    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 :)
Re: Type Globs
by seeker (Curate) on Jan 04, 2001 at 20:15 UTC
    Re: Getting STDOUT displayed and dumped to a file: Here's what has worked for me, &someprog >somefile; tail -f somefile I'm sure that there's more than one way, -er nevermind.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-12-29 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls