Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: printing to STDOUT and a Logfile (cross-platform sub)

by ybiC (Prior)
on Oct 29, 2002 at 01:22 UTC ( #208650=note: print w/replies, xml ) Need Help??

in reply to printing to STDOUT and a Logfile

Here's one cross-platform way to simultaneously print to STDOUT and a filehandle.

# print messages to both console and logfile sub PrintLogCon { print @_; print(LOG @_) or die "Error printing to $logfile: $!"; }

and you'd call it thusly:

use warnings; use strict; open(LOG, ">$logfile") or die "Error opening $logfile: $!"; ... ... ... do->stuff; do->it(again) or PrintLogCon("Error: $!"); do->stuff(more); ... ... ... close LOG or die "Error closing $logfile: $!"; __END__
Phat props to mad monk Petruchio for showing me this a while back.
    striving toward Perl Adept
    (it's pronounced "why-bick")

Update2: consistant-ized scalar for logfile
Update1: or use IO::Tee as shown by fine monk jsprat.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://208650]
choroba knows the situation. At work, unit tests run for 30 minutes, and integration and end-to-end tests take almost one hour.
[stevieb]: yeah. This is my build/test software that manipulates perlbrew/ berrybrew, and runs all unit tests of a project against all installed versions, including testing a dist against all of it's current revdeps. A lot of tests it runs for itself:)
[stevieb]: testing the test build system, essentially

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2017-03-26 22:08 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (315 votes). Check out past polls.