perl - print to a log file and screen | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

perl - print to a log file and screen

Shell Programming and Scripting


Tags
perl, tee

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-16-2007
mjays mjays is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 1 October 2008, 10:18 AM EDT
Location: london
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
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


Code:
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?
Sponsored Links
    #2  
Old 08-16-2007
rexf rexf is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 6 July 2010, 11:18 AM EDT
Location: 127.0.0.1
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Tmtowtdi

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.
Sponsored Links
    #3  
Old 08-17-2007
mjays mjays is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 1 October 2008, 10:18 AM EDT
Location: london
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
thanks, i'll try your solution out and see if there are any modules.
    #4  
Old 08-17-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
 
Join Date: Sep 2002
Last Activity: 4 May 2014, 6:22 AM EDT
Location: Hong Kong, China
Posts: 1,622
Thanks: 0
Thanked 9 Times in 8 Posts
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!
Sponsored Links
    #5  
Old 08-20-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 4 April 2014, 3:58 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 17 Times in 16 Posts
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
Sponsored Links
    #6  
Old 08-21-2007
mjays mjays is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 1 October 2008, 10:18 AM EDT
Location: london
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
i decided to keep it simple.


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

thanks for replying everyone,

matt
Sponsored Links
    #7  
Old 08-21-2007
bigearsbilly bigearsbilly is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 12 August 2014, 6:31 AM EDT
Location: Southern England
Posts: 245
Thanks: 0
Thanked 10 Times in 10 Posts
nice solution
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time. butterfly20 Shell Programming and Scripting 2 01-13-2011 07:35 AM
how to print script output to screen and file orahi001 UNIX for Dummies Questions & Answers 10 01-16-2009 12:47 AM
perl: howto print to screen & filehandle Alalush Shell Programming and Scripting 2 08-05-2008 01:32 AM
print to screen and to file using awk?! satnamx Shell Programming and Scripting 2 04-25-2006 01:33 PM
echo or print to screen and file Shakey21 UNIX for Dummies Questions & Answers 2 01-09-2002 06:56 PM



All times are GMT -4. The time now is 12:26 AM.