perl - print to a log file and screen

as the title suggests, i need to print a user message to a log file and the screen using perl.

in unix i set up a function using 'tee' like so

function Display_Message
{
    echo "$*" | tee -ai $LOGFILE
}

the following command then obviously displays to the screen and prints to a log file.

Display_Message "Error: something gone wrong"

how can i do something similar in perl?

Just loop through the filehandles you want to print to.

$file='/tmp/logfile';
open LOGFILE ,">>$file" or die "can't open $file: $!";
@t=qw/STDOUT LOGFILE/;
for (@t){
print $_ `echo $$`;
}

You can do this in a oneliner aswell, but this should be easier to understand.
I'm sure there is simpler ways and maybe even some module that has the equivalent functionality tee.

thanks, i'll try your solution out and see if there are any modules.

If you meant to do some logging, consider using a more sophisticated logger mechanism, such as log4perl, described here:

perl.com: Retire your debugger, log smartly with Log::Log4perl!

Here are two Perl modules that you may find useful:

  1. Tee - Pure Perl emulation of GNU tee
    Tee - Pure Perl emulation of GNU tee - search.cpan.org

  2. IO::Tee - Multiplex output to multiple output handles
    IO::Tee - Multiplex output to multiple output handles - search.cpan.org

i decided to keep it simple.

open (STDOUT, "| tee -ai log.txt");
print "blah blah"
close (STDOUT);

thanks for replying everyone,

matt

nice solution