Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: uninitialized values

by Laurent_R (Vicar)
on Aug 19, 2013 at 21:41 UTC ( #1050091=note: print w/ replies, xml ) Need Help??


in reply to uninitialized values

Use of uninitialized value $val in concatenation (.) or string at ./ping_linux.pl line 69, <CMD> line 2.

Your warning message is about an unitialized $val variable, but you are not using any such variable in the code you have posted (you're declaring one, but not using it).

The waning message also mentions line 69 of your code, the code you display is about 15 lines. How do you expect us to tell you what is wrong in line 69 if you don't show it to us?

Please give us line 69 of your code, or give us a correct warning message. You need to be very accurate on these types of things if you want us to be able to help. Please help us to help you.

In addition to other problems indicated by others (and using meaningful variable names is really not a luxury, we could understand much better what you are trying to do if your variables names would tell us what they are intended to contain; to me using meaningful names for variables, subroutines, etc. is the most important form of code comment), I also doubt quite a bit that the following lines (at least the two last ones) do what you want:

@values1 = split ; @val1 = split(/\(/,$values1[3]); $val1[1] =~ s/\)//;

Please supply a sample of the ping command output, so that we can figure out what these code lines are supposed to do. Also you don't use $val1[1] after having assigned it, what's the point?

BTW, the fact that you have this warning on line 2 possibly simply means that the second line of your input is empty.


Comment on Re: uninitialized values
Select or Download Code
Re^2: uninitialized values
by gaurav (Acolyte) on Aug 20, 2013 at 02:17 UTC

    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

      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

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-07-23 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (154 votes), past polls