By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,278 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,278 IT Pros & Developers. It's quick & easy.

Use of uninitialised values error when values have been initialised :(

P: 2
Hi, as a bit of background (and seeing as it is my first post :)) i am a complete newbie at perl. I literally picked it up a week ago to do this project as it seemed like the best choice for a language to connect to a SQL database in a Unix envirment as well as script parsing so there is more than likely a ton of things i could have done a better way.
I have been at this for 3 days now and cant for the life of me work out why the code is piping out errors

Th input into the code for sake of simplicity as i think the rest of my errors are essentially the same thing is:
Expand|Select|Wrap|Line Numbers
  1. Dec 16 07:50:22 xxxx-vpn-xxx 1765156 12/16/2007 07:50:21.110 SEV=6 IKEDBG/64 RPT=183939 10.100.250.100  IKE Peer included IKE fragmentation capability flags: Main Mode:        True Aggressive Mode:  False
The code works but the errors i am getting are:
Use of uninitialized value in concatenation (.) or string at parser.pl line 62.
at parser.pl line 62
Use of uninitialized value in concatenation (.) or string at parser.pl line 64.
at parser.pl line 64

Now i know this means that a value wasn't declared or explicitly stated but i cant see how this is possible as i give them all the value 'null' when i declare them and then these null values are changed where necessary,
Before in the
Expand|Select|Wrap|Line Numbers
  1. if ($currentIP eq $sessionIP){ }
statements, even though essentially the code wouldnt get that far because it would be caught by
Expand|Select|Wrap|Line Numbers
  1. if($currentCode=~ m/(IKEDBG\/64)|(IKE\/52)|.....|(IKE\/49)|(IKE\/75)/){
As being an invalid line adding a check to see if sessionIP contained something got rid of the error message

This is just a snippet so it probably wont compile if you would like more please dont hesitate to ask

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl-w
  2.                   #-w use warnings
  3.                   #-T use Taint to ensure program is secure
  4.  
  5. use strict;                     #Force variable declarations
  6. # Functions to locate errors
  7. use Carp ();
  8. local $SIG{__WARN__} = \&Carp::cluck;
  9.  
  10.  
  11. #=== Delcare Varibles ===#
  12. my $line = undef;               #Line to be processed
  13. my $file = 'testLog.txt';       #Path to log file
  14. my @log = undef;              #Stores lines from log file
  15.  
  16. # Session Data Array as global (to avoid un-neccesary processing when passed into function)
  17. our @sessions = (
  18.     # Session Data Holding Variables
  19.     {'clientIPAddress' => undef,    #Address assigned from IP pool
  20.      'currentCode' => 'Initialiser',#Current IKE code being processed
  21.      'connectDate' => 'null',        #Date of connection to access point
  22.      'connectTime' => 'null',        #Time of connection to access point
  23.      'userID' => 'null',             #Username unique to user
  24.      'authenticationTime' => 'null', #Time user's has been authenticated
  25.      'groupID' => 'null',            #VPN group user belongs to (usually Roamnet)
  26.      'platformType' => 'null',       #Platform user is running Windows, Unix etc..
  27.      'clientVersion' => 'null',      #Version of VPN client
  28.      'vpnPoolAddress' => 'null',     #IP address assigned from VPN pool
  29.      'disconnectTime' => 'null',     #Time session was disconnected
  30.      'duration' => 'null',           #Duration of session
  31.      'bytesTx' => 'null',            #Bytes Transmitted
  32.      'bytesRx' => 'null',            #Bytes Recieved
  33.      'reasonForDisconnect' => 'null',#Reason for connection termination
  34.      'errorMessages' => undef,      #Additional error messages
  35.      'state' => 'Disconnected'      #State of session
  36.    }
  37. );
  38.  
  39.  
  40. #=== Get data from file ===
  41. open(LOG, $file) or die "Cannot open $!";    #Open file under the handle LOG
  42.  
  43. for $line (<LOG>){      #Read file into @log
  44.   if($line){
  45.     chomp($line);       #Removes new line character from entry
  46.     process_log($line); #Call function to precess log line
  47.   } else {              #Check to see if line is empty
  48.     print 'Warning Line is Empty <BR>\n';
  49.   }
  50.  
  51. }
  52. close(LOG);             #Close the log file
  53.  
  54.  
  55.  ### =================== TESTING - REMOVE ME = =============================
  56.  my $totalSessions = scalar (@sessions);
  57.  print ':::';
  58.  for (my $n=0; $n < $totalSessions; $n++){
  59.        print 'currentcode = '.$sessions[$n]{"currentCode"}.'<br>';
  60.        print 'connectdate = '.$sessions[$n]{"connectDate"}.'<br>';
  61.        print 'connecttime = '.$sessions[$n]{"connectTime"}.'<br>';
  62.        print 'clientip = '.$sessions[$n]{"clientIPAddress"}.'<br>';
  63.        print 'state = '.$sessions[$n]{"state"}.'<br>';
  64.        print 'authentication date = '.$sessions[$n]{"authenticationDate"}.'<br>';
  65.        print 'authentication time = '.$sessions[$n]{"authenticationTime"}.'<br>';
  66.        print 'userID = '.$sessions[$n]{"userID"}.'<br>';
  67.        print 'groupID = '.$sessions[$n]{"groupID"}.'<br>';
  68.        print 'platformType = '.$sessions[$n]{"platformType"}.'<br>';
  69.        print 'clientVersion = '.$sessions[$n]{"clientVersion"}.'<br>';
  70.        print 'vpnPoolAddress = '.$sessions[$n]{"vpnPoolAddress"}.'<br>';
  71.        print 'disconnectTime = '.$sessions[$n]{"disconnectTime"}.'<br>';
  72.        print 'duration = '.$sessions[$n]{"duration"}.'<br>';
  73.        print 'bytesTx = '.$sessions[$n]{"bytesTx"}.'<br>';
  74.        print 'bytesRx = '.$sessions[$n]{"bytesRx"}.'<br>';
  75.        print 'reasonForDisconnect = '.$sessions[$n]{"reasonForDisconnect"}.'<br>';
  76.        print 'errorMessages = '.$sessions[$n]{"errorMessages"}.'<br>';
  77.        print '<br>';
  78.    }
  79.  ### =========== END TESTING ==============================================
  80.  
  81.  
  82.  #=== Sub Routines ===
  83.  
  84.  sub process_log { #Script to parse logs
  85.  
  86.  #== Parameters passed into function ==
  87.  #$line (@_) line being processed
  88.  
  89.  #== Declare Variables ==
  90.  my $currentCode = undef;                          #Code being processed
  91.  my $currentIP = undef;                            #IP of line being processed
  92.  my $currentUserID = undef;                        #UserID of line being processed
  93.  my $numberOfSessions = undef;                     #Number of sessions in the array
  94.  my $data = undef;                                 #Stores result of regex expressions
  95.  my $currentLine = undef;                          #Whole Line currently being processed
  96.  my $errors = undef;                               #Stores erorrs in log parsing
  97.  my $sessionIP = undef;                            #Used to store IP for session during search
  98.  my $sessionUserID = undef;                        #Used to store userID for session during search
  99.  #=== Store elements from line ===
  100.  
  101.  $currentLine = shift;                              #Get line passed into function
  102.  my @seperatedLine = split(/\s/, $currentLine);     #Split line based on empty space delimitation
  103.  $currentIP = $seperatedLine[10];                   #Get IP from line
  104.  $numberOfSessions = scalar (@sessions);            #Get number of elements sessions array
  105.  $currentLine =~ m/SEV=\d (.*) RPT/;                #Extract current code from line
  106.  $currentCode = $1;                                 #Get current code
  107.  
  108.  if($currentCode){                                  #Check if line contains a code
  109.   }else{
  110.    #no code found
  111.    next;
  112.  }
  113.  
  114.  #Check for an unknown error code and corrupted lines
  115.  if($currentCode=~ m/(IKEDBG\/64)|(IKE\/52)|(AUTH\/5)|(IKE\/22)|(IKE\/100)|(IKE\/98)|(AUTH\/9)|(IKE\/167)|(IKE\/184)|(AUTH\/22)|(IKE\/194)|(IKE\/119)|(IKE\/136)|
  116. (IKE\/25)|(IKE\/34)|(IKE\/50)|(AUTH\/28)|(IKE\/123)|(IKE\/66)|(IKE\/120)|(IKE\/49)|(IKE\/75)/){
  117.     #code found
  118. }else{
  119.   $errors = 'Log Error: \''.$currentLine.'\'<BR>';
  120.   #print $errors; #========================== Save error to log file/database =========================
  121.   next;
  122. }
  123.  
  124.  # IKE/DBG64
  125.  if ($currentCode eq 'IKEDBG/64'){
  126.       for (my $i=1; $i <= $numberOfSessions; $i++){                 #Check to see if session already exists
  127.         $sessionIP = $sessions[$i]{"clientIPAddress"};              #Get IP for selected session
  128.         if($sessionIP){                                             #Check if line contains a IP
  129.           if ($currentIP eq $sessionIP){                            #Compare IP
  130.             #-Session Exists-
  131.             #Check for illegal connections (session not terminated correctly)
  132.             my $sessionState = $sessions[$i]{"state"};              #Get current state of session
  133.             if ($sessionState eq 'Disconnected'){
  134.               #--Legal Session--
  135.               $sessions[$i]{"state"} = 'Connecting';                #Progress state to connecting
  136.               last;                                                 #Break out of loop
  137.               }else{
  138.               #--Illegal Session--
  139.               #Error Message
  140.               $sessions[$i]{"reasonForDisconnect"} = 'Session was not teminated correctly and has been termintated by parser';
  141.               save_session($i);                                     #Call save_session function
  142.               splice(@sessions,$i,1);                               #Delete the current session
  143.               #Create new session
  144.               create_new_session($i);
  145.               $sessions[$i]{"clientIPAddress"} = $seperatedLine[10];#Used as key for session
  146.               $sessions[$i]{'currentCode'} = $currentCode;          #Store current code for debugging
  147.               $sessions[$i]{"connectDate"} = $seperatedLine[5];
  148.               $sessions[$i]{"connectTime"} = $seperatedLine[6];
  149.               last;                                                 #Break out of loop
  150.             }
  151.           }
  152.         }else{
  153.           #-Session doesn't exist-
  154.           #Create new session
  155.           create_new_session($numberOfSessions);                                 #As the array starts from 0, $numberOfSessions will
  156.                                                                                  #always be greater than the index for the last session.
  157.           $sessions[$numberOfSessions]{"clientIPAddress"} =  $seperatedLine[10]; #Used as key for session
  158.           $sessions[$numberOfSessions]{'currentCode'} = $currentCode;            #Store current code
  159.           $sessions[$numberOfSessions]{"connectDate"} = $seperatedLine[5];
  160.           $sessions[$numberOfSessions]{"connectTime"} = $seperatedLine[6];
  161.         }
  162.       }
  163.  }
  164.  
Any help would be much appreciated

Thanks
Jan 12 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
It appears that variables, like 'clientIPAddress' => undef, are not initialized before you try printing a little bit later:

print 'clientip = '.$sessions[$n]{"clientIPAddress"}.'<br>';

same with

print 'authentication date = '.$sessions[$n]{"authenticationDate"}.'<br>';

there is no "authenticationDate" in the initial data set @sessions. Why are you using an array that contains an annonymous hash? Are there more annonymous hashes in the array?
Jan 12 '08 #2

P: 2
It appears that variables, like 'clientIPAddress' => undef, are not initialized before you try printing a little bit later:

print 'clientip = '.$sessions[$n]{"clientIPAddress"}.'<br>';

same with

print 'authentication date = '.$sessions[$n]{"authenticationDate"}.'<br>';

there is no "authenticationDate" in the initial data set @sessions.
Maybe it's me being dim I dont quite understand :s, surely the set is initialised when i make the template entry and put 'null' values into them from the get go

i use the same method to create a new "session" later on in the function create_session.(see below)
If anything shouldn't it just print 'null'?
I used undef for the client IP and other things so that i could do a check to see if there was anything in the variable, if there wasnt the loop would break and if there was it would continue to try avoid the uninitialised problems


Expand|Select|Wrap|Line Numbers
  1. sub create_new_session {            #Script to create a blank session
  2.  
  3.  my $sessionID = shift;                                  #Session ID
  4.  # Create empty session
  5.  $sessions[$sessionID]{'clientIPAddress'} = undef;    #Address assigned from IP pool
  6.  $sessions[$sessionID]{'currentCode'} = 'Initialiser';        #Current IKE code being processed
  7.  $sessions[$sessionID]{'connectDate'} = 'null';        #Date of connection to access point
  8.  $sessions[$sessionID]{'connectTime'} = 'null';        #Time of connection to access point
  9.  $sessions[$sessionID]{'userID'} = 'null';             #Username unique to user
  10.  $sessions[$sessionID]{'authenticationDate'} = 'null'; #Date user's has been authenticated
  11.  $sessions[$sessionID]{'authenticationTime'} = 'null'; #Time user's has been authenticated
  12.  $sessions[$sessionID]{'groupID'} = 'null';            #VPN group user belongs to (usually Roamnet)
  13.  $sessions[$sessionID]{'platformType'} = 'null';       #Platform user is running Windows, Unix etc..
  14.  $sessions[$sessionID]{'clientVersion'} = 'null';      #Version of VPN client
  15.  $sessions[$sessionID]{'vpnPoolAddress'} = 'null';     #IP address assigned from VPN pool
  16.  $sessions[$sessionID]{'disconnectTime'} = 'null';     #Time session was disconnected
  17.  $sessions[$sessionID]{'duration'} = 'null';           #Duration of session
  18.  $sessions[$sessionID]{'bytesTx'} = 'null';            #Bytes Transmitted
  19.  $sessions[$sessionID]{'bytesRx'} = 'null';            #Bytes Recieved
  20.  $sessions[$sessionID]{'reasonForDisconnect'} = undef;#Reason for connection termination
  21.  $sessions[$sessionID]{'errorMessages'} = undef;      #Additional error messages
  22.  $sessions[$sessionID]{'state'} = 'Disconnected';      #State of session
  23. } #End Create empty session
  24.  
Why are you using an array that contains an annonymous hash? Are there more annonymous hashes in the array?
It's because more than one session needs to be tracked at any one time as the script is intended to parse live logs, therefore needs to be done on a line by line basis rather than pull a session out the logs save it and move on.

I created the empty session at the top to make the code easier to read and so i could delcare and initialise all the variables to try avoid the error i am getting
Jan 12 '08 #3

eWish
Expert 100+
P: 971
If anything shouldn't it just print 'null'?
If you want insert a null value into the db you have to use 'undef'. Otherwise, you are just printing the word 'Null' which is not the same as a null/undefined value.

Here is an example where if $value is not defined, then it will use undef.
Expand|Select|Wrap|Line Numbers
  1. my $value = $some_other_value || undef;
Jan 13 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
the value of 'null' is a string, so those variables are defined, undef means undefined, so any value assigned the value of undef is undefined.

use warnings;
$foo = undef;
print $foo;

The above returns a warnings about "uninitialized value in print" which is essentially the same warning you are getting.
Jan 14 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.