Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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?


Replies are listed 'Best First'.
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 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
      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 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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2022-01-27 14:09 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (70 votes). Check out past polls.