How to print to more than one output

B

Bern

Hello list,

Is it possible to print to two different output at once?

For example, I tried the following but it does not work. Nothing shows
up in the log file.

open(LOG, ">test.log") or die "Can't create log file: $!";
print { LOG && STDOUT } `ls -l`;

Thanks,
Bern
 
A

Anno Siegel

Jim Gibson said:
Not in a single print statement, no. There isn't the Perl equivalent of
the Unix tee command.


This will print to STDOUT only, because the block returns the value of
the STDOUT file handle, following the rules of the logical and
operator. The LOG file handle is evaluated first, and, because it is
true, the STDOUT is evaluated next and returned as the value of the
expression.

You will have to write your own subroutine to output to two or file
handles.

The IO::Tee module on CPAN also handles this.

Anno
 
B

Brian McCauley

Subject: Re: How to print to more than one output

Is it possible to print to two different output at once?

This is FAQ: How do I print to more than one file at once?

Please consult the FAQ _before_ posting a question.

Note the there are two answers to this (the ones you've been given
already) and oddly older versions of the FAQ give one and newer
versions the other.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top