CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
perl write in file also display on screen!!!

 



zedex
New User

May 30, 2009, 10:11 PM

Post #1 of 5 (996 views)
perl write in file also display on screen!!! Can't Post

Hi all

This is simple Ping program i have writen which checks the status of connection and prints the message but i want it to print report in file as well as on screen how can i do it ??

if i print in file even after 2-3 minutes file is empty looks like there is some flushing issue.


Code
#!/usr/bin/perl -w 
use strict;
use Net::Ping ;
use Time::TimeStamp ;
my $t = Time::Timestamp->new(ts => time,time_zone => 'local');

$| = 1 ;

my $WaitIfOk = 30 ;
my $WaitIfNotOk = 10 ;
my $MaxOkCount = 25 ;

my $Count = 0 ;

sub main ()
{
my $ip = &GetDefGatewayIp ;
print <<"EOF" ;
==============================================================
Ping Response of Gateway : [$ip]
==============================================================
EOF
printf "%22s\t%7s\t%15s\n","TIME STAMP","REPLY","GATEWAY IP" ;
&PingDefGateway("$ip") ;
}
main ;

sub GetDefGatewayIp ()
{
my $_IP = "";
chomp ( my @GatewayIp = `ipconfig | findstr /i gateway` ) ;
foreach my $ip ( @GatewayIp )
{
if ( $ip =~ /^.*:\s*(\d+\.\d+\.\d+\.\d+)/ )
{ $_IP = $1 ; }
}
return $_IP ;
}

sub PingDefGateway($)
{
my $_gatewayIp = $_[0] ;
my $p = Net::Ping->new();
if ( $p->ping($_gatewayIp) )
{
&LogThis("Okay","$_gatewayIp","1") ;
$Count++ ;
sleep $WaitIfOk ;
} else
{
&LogThis("Not Ok","$_gatewayIp","1") ;
sleep $WaitIfNotOk ;
}
$p->close();

if ( $Count <= $MaxOkCount )
{
&PingDefGateway("$_gatewayIp") ;
} else
{
print GATEWAY "Exiting as server is giving steady resonse\n" ;
close GATEWAY ;
exit 0 ;
}
}
sub LogThis ($$)
{
my $_Msg = $_[0] ;
my $_IP = $_[1] ;
my $_TSFLAG = $_[2] ;
my $TimeStamp = $t->tsIso() ;

### HERE I WANT IT TO PRINT IT TO FILE like open file and just print
($_TSFLAG)? printf "%22s\t%7s\t%15s\n",$TimeStamp,$_Msg,$_IP:printf "%22s\t%7s\t%15s\n"," ",$_Msg,$_IP ;
return 0 ;
}



FishMonger
Veteran / Moderator

May 31, 2009, 7:30 AM

Post #2 of 5 (988 views)
Re: [zedex] perl write in file also display on screen!!! [In reply to] Can't Post

I'm not sure I understand the problem. If you want to the same output to the file and screen, then simply execute 2 print statements.

If you want something more complex, then use IO::Tee.
http://search.cpan.org/~kenshan/IO-Tee-0.64/Tee.pm


FishMonger
Veteran / Moderator

May 31, 2009, 7:40 AM

Post #3 of 5 (987 views)
Re: [zedex] perl write in file also display on screen!!! [In reply to] Can't Post

You really shouldn't use prototypes or the '&' on the subroutines unless you understand and want their side effects, which apparently you don't.

You should use the warnings pragma instead if the -w switch.

Move the calling of the "main" sub up (I almost missed that line), and add an exit statement just after it.

Don't directly use global vars in the subs. Instead, pass them by value or reference depending on what you need to do with them.


(This post was edited by FishMonger on May 31, 2009, 7:41 AM)


FishMonger
Veteran / Moderator

May 31, 2009, 8:00 AM

Post #4 of 5 (984 views)
Re: [zedex] perl write in file also display on screen!!! [In reply to] Can't Post


Quote
if i print in file even after 2-3 minutes file is empty looks like there is some flushing issue.


use IO::Handle;
http://search.cpan.org/~gbarr/IO-1.25/lib/IO/Handle.pm


zedex
New User

Jun 1, 2009, 10:16 AM

Post #5 of 5 (971 views)
Re: [FishMonger] perl write in file also display on screen!!! [In reply to] Can't Post

Hey fisheye

thanks for the tips i learned perl by looking at others code and that habit continued till now with little improvements every now & then

i would be happy if can tell me or at least give me link which will guide me about these stuffs ...

Any ways i will try to find out on my own

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives