Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: uninitialized values

by gaurav (Sexton)
on Aug 20, 2013 at 02:17 UTC ( [id://1050110]=note: print w/replies, xml ) Need Help??


in reply to Re: uninitialized values
in thread uninitialized values

Sorry Laurent.Here below I am given my full code

#!/usr/bin/perl -w =head1 NAME ping_linux.pl - This script works on Linux system pings a host and re +turns statistics data. =head1 VERSION Version 1.0 =head1 AUTHOR Gaurav Dubey (gaurav.d@ronankiinfotech.com =head1 SYNOPSIS ./ping_linux.pl [-c --> count (Number of echo requests to be sent)] +[-i --> interval (Interval in milliseconds between echo requests)] [ +-s --> packetsize (Size of icmp packet)] [-h --> destinationIP (Ip ad +dress of destination host)] [-w --> AllowableTime (Max time in second +s for sending receiving echo requests/reponse)]" =head1 DESCRIPTION This pings a host via the system ping command and returns RTA ,Tx pack +ets,Rx packets,TTL,Packet-loss =cut use strict; use Getopt::Long; use Pod::Usage; my ($host,$count,$interval,$packetsize,$allowableTime); GetOptions( "h|host=s", \$host, "c|count=i", \$count, "i|interval=i", \$interval, "s|packetsize=i", \$packetsize, "w|allowableTime=i",\$allowableTime, ); #pod2usage("$0: No host given!\n") unless($host); pod2usage("$0:No host given!\n") unless($host && $host =~ /^((([2][5][ +0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])\.){3})([2][5][0-5]|([2][0-4]|[1 +][0-9]|[0-9])?[0-9])$/); $count = 5 unless ($count); $interval = 1 unless ($interval); $packetsize = 56 unless ($packetsize); $allowableTime = 10 unless ($allowableTime); open CMD, "/bin/ping -c $count -i $interval -s $packetsize -w $allowab +leTime $host |" or die "Can't open ping: $!"; while (<CMD>) { my (@values1,@val1,@values2,@val2); if ( $_ =~ /PING/ ){ @values1 = split ; @val1 = split(/\(/,$values1[3]); $val1[1] =~ s/\)//; } print "$values1[1] \n"; print "$val1[0] \n"; if ($. == 2) { @values2 = split; @val2 = split(/\=/ , $values2[5]); } print "$val2[1] \n"; } close CMD

by this means I am running my perl script

./ping_linux.pl -h 74.125.235.7  -c 1

And this is my output

74.125.235.7 56 Use of uninitialized value $val2[1] in concatenation (.) or string at +./ping_linux.pl line 64, <CMD> line 1. Use of uninitialized value $values1[1] in concatenation (.) or string +at ./ping_linux.pl line 58, <CMD> line 2. Use of uninitialized value $val1[0] in concatenation (.) or string at +./ping_linux.pl line 59, <CMD> line 2. 54 Use of uninitialized value $values1[1] in concatenation (.) or string +at ./ping_linux.pl line 58, <CMD> line 3. Use of uninitialized value $val1[0] in concatenation (.) or string at +./ping_linux.pl line 59, <CMD> line 3. Use of uninitialized value $val2[1] in concatenation (.) or string at +./ping_linux.pl line 64, <CMD> line 3. Use of uninitialized value $values1[1] in concatenation (.) or string +at ./ping_linux.pl line 58, <CMD> line 4. Use of uninitialized value $val1[0] in concatenation (.) or string at +./ping_linux.pl line 59, <CMD> line 4. Use of uninitialized value $val2[1] in concatenation (.) or string at +./ping_linux.pl line 64, <CMD> line 4. Use of uninitialized value $values1[1] in concatenation (.) or string +at ./ping_linux.pl line 58, <CMD> line 5. Use of uninitialized value $val1[0] in concatenation (.) or string at +./ping_linux.pl line 59, <CMD> line 5. Use of uninitialized value $val2[1] in concatenation (.) or string at +./ping_linux.pl line 64, <CMD> line 5. Use of uninitialized value $values1[1] in concatenation (.) or string +at ./ping_linux.pl line 58, <CMD> line 6. Use of uninitialized value $val1[0] in concatenation (.) or string at +./ping_linux.pl line 59, <CMD> line 6. Use of uninitialized value $val2[1] in concatenation (.) or string at +./ping_linux.pl line 64, <CMD> line 6.

So now please enlighten me on this .Thanks

Replies are listed 'Best First'.
Re^3: uninitialized values
by farang (Chaplain) on Aug 20, 2013 at 04:21 UTC

    Here's a way to get rid of the warnings, but really you need to reconsider the whole approach used in the while loop. That code is a mess.

    By declaring lexical variables inside the loop, they get reset each time through. Move the declarations out of the loop for them to persist.

    my (@values1,@val1,@values2,@val2); while (<CMD>) { ...
    You will still get one warning from line 1, because the code after the if loop is executed for all lines and @val2 is not yet defined. Adding next can fix that.
    my (@values1,@val1,@values2,@val2); while (<CMD>) { if ( $_ =~ /PING/ ){ @values1 = split ; @val1 = split(/\(/,$values1[3]); $val1[1] =~ s/\)//; next; } ...

    Again, while these changes avoid the warnings and hopefully show you why they happened, the entire while loop needs a rethink.

      yes thanks for it,I got it.I am feeling dumb.Hope I won't committed this mistake again

Re^3: uninitialized values
by Random_Walk (Prior) on Aug 20, 2013 at 06:30 UTC

    If you have not looked at it already, you may want to take a look at Net::Ping. that way you avoid a lot of system dependencies. On the down side there are limits to the sort of ping you can perform on Unix, unless you are running as root

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      Hi R., I got struck now.I have one problem . My code has been working fine untill the host exist,But suppose if I have given some xyz ip which is not in that network, then its not working.So I want it to tell that Ip is unreachable or any other error msg.Give me some hint

      My code is

      open CMD, "/bin/ping -c $count -i $interval -s $packetsize -w $allowab +leTime $host |" or die "Can't open ping: $!"; #print "$success \n"; my (@values1,@val1,@values2,@val2,@values3,@values4,@values5); while (<CMD>) { if ( $_ =~ /PING/ ){ @values1 = split ; @val1 = split(/\(/,$values1[3]); $val1[1] =~ s/\)//; } if ($. == 2) { @values2 = split; @val2 = split(/\=/ , $values2[5]); } if ($_ =~ /packets/) { #print "$_ "; @values3 = split; } if ($_ =~ /^rtt/) { #print "$_ "; @values4 = split /\=/; @values5 = split(/\// ,$values4[1]); } }

      I hope you help me out of this.....Thanks

        Can you please add comments to your code with examples of what the ping returns in each case. I think you may be looking for several different failure modes:

      • Can not resolve name to IP
      • Can not reach that IP
      • That IP is not responding
      • As different OS/Ping combinations have different results it is hard for us to know what you are looking at in each case. If you add an example of each, in a comment before the related if statement, it will make debugging much easier. Not just for your fellow monks, but also for future maintainers of your code, including your future self.

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1050110]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found