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: Beginner:
Use of uninitialized value $line in pattern match (m//)

 



stokastic
New User

May 13, 2014, 6:57 AM

Post #1 of 3 (2366 views)
Use of uninitialized value $line in pattern match (m//) Can't Post

Hello, I am new to Perl, trying to pick up some code left by a previous coder. The problem is in this subroutine:


Code
       sub Get_Module_Size { 
#Read Inputs
my $source = shift;
my $path = shift;

#Initialize Variables
my $module_size = "";
my $empty = "";
my $XML_file = "";
my $line = "";

given ($source) {
when ("XML") {
$XML_file = "$path";
open (XMLSLAVE, '<', $XML_file) or die "Could not open $XML_file";

$line = <XMLSLAVE>;
until ($line =~ /^\s*<Module_Size>/) {
$line = <XMLSLAVE>;
}
($empty, $module_size) = split(/<Module_Size>|<\/Module_Size>/, $line);
close XMLSLAVE;
$module_size = Trim $module_size;
if ($module_size =~ /([0-9A-Fa-f]+)$/){
$module_size = "0x$1";
}
} default {
$module_size = "0x0";
}
}
return Trim ($module_size);
}


This is part of a much larger script. The entire script is used to parse an XML file. I have a batch file that runs this script 3 times, with 3 different XML files, and it runs fine the first 2 times, but on the 3rd time, I get the following syntax error:

Use of uninitialized value $line in pattern match (m//)

This occurs at the line that says ' $line = <XMLSLAVE>' within the 'until' loop. I'm wondering if there is an obvious fix for this, or an alternate way to accomplish the task.


FishMonger
Veteran / Moderator

May 13, 2014, 7:46 AM

Post #2 of 3 (2342 views)
Re: [stokastic] Use of uninitialized value $line in pattern match (m//) [In reply to] Can't Post

Since ' $line = <XMLSLAVE>' is assigning a value to $line, the uninitialized warning would not be coming from that line. It's coming from the line above it.

This section:

Code
$line = <XMLSLAVE>;	  
until ($line =~ /^\s*<Module_Size>/) {
$line = <XMLSLAVE>;
}


would be better written as:

Code
while ($line = <XMLSLAVE>) { 
last if $line =~ /^\s*<Module_Size>/;
}


or

Code
while ($line = <XMLSLAVE>) { 
if ($line =~ /<Module_Size>(.+?)</Module_Size>/) {
$module_size = $1;
last;
}
}


An even better method would be to use one of the XML parsers available on cpan, but that would require a more extensive adjustment/correction of the code.


(This post was edited by FishMonger on May 13, 2014, 7:49 AM)


stokastic
New User

May 13, 2014, 1:02 PM

Post #3 of 3 (2248 views)
Re: [FishMonger] Use of uninitialized value $line in pattern match (m//) [In reply to] Can't Post

Thanks, it works great!

 
 


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

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