[tpm] File handle problem in a script called by another script

Madison Kelly linux at alteeve.com
Tue Oct 16 12:53:51 PDT 2007


Hi all,

   I've got a program (main) that calls a second program (small), both 
written in perl. Both the main and small program access a common library.

   When I call the second, small program directly from the command line, 
it works fine. At one point it calls a subrouting from the library that 
opens a file, reads it and writes back out a modified version 
(specifically, it tracks various records and keeps track of a counter).

   Now, when I call the small program from within the main program, I 
get an error:

-=-=-=-=-
[<date>] small_script.pl: Use of uninitialized value in concatenation 
(.) or string at ./nmc.lib line 1741.
[<date>] small_script.pl: readline() on closed filehandle GEN16 at 
./nmc.lib line 1746.
-=-=-=-=-

   Here, nmc.lib is the library.

   I use 'IO::Handle' for creating the filehandle used to write out the 
file. Here is the specific block of code that generates the error:

nmc.lib lines 1069 - 1076
-=-=-=-=-
# Now re-write the file.
my $write_inc=IO::Handle->new();
open ($write_inc, ">$inc_file") || &error($conf, "error_0042", 1, 0, 
$this_file, __LINE__, $inc_file, $!);
for (@lines)
{
	print $write_inc $_;
}
$write_inc->close();
-=-=-=-=-

   If I comment out this section, the program works fine (but obviously, 
the file is not updated...). Again, if I run the small program directly, 
there is no problem, either.

   My *guess* is that for some reason, the filehandle created by the 
main program to read the output from the call to the second, small 
program must be clobbering the filehandle above. I can't understand why 
though, because they are different sections of code using different file 
handle names (though the code to call the second program IS in the same 
library).

   In case it matters, here is the code from the main program that calls 
the small program (cleaned up to make it more readable):

nmc.lib lines 2266 - 2284
-=-=-=-=-
my $gzf=IO::Handle->new();
my $shell_call=”/path/to/small_script.pl 2>&1 |";
# write to log what I am calling...
open ($gzf, $shell_call) || die...;
while (<$gzf>)
{
	chomp;
	my $line=$_;
	# write to log this line...
		
	$line=~s/\[(.*?)\]/\[<span class=\"tech\">$1<\/span>\]/g;
	my %value=(
		replace_0	=>	"",
		replace_1	=>	"$line",
	);
	&print_template($conf, "tool_item", $$conf{template_ga}, \%value); 
%value=();

}
$gzf->close();
-=-=-=-=-

   Any idea why the second example code could be clobbering the first 
block of code (if that is what is even happening)?

Thanks!!

Madi


More information about the toronto-pm mailing list