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

Madison Kelly linux at alteeve.com
Tue Oct 16 13:39:48 PDT 2007


Indy Singh wrote:
> The line numbers that you supplied code for (1069 - 1076) don't cover
> where you say the error is (line 1741)
> Assuming that that is just an oversight.
> 
> It would probably be a good idea to print out the content of $inc_file
> to verify that it actually contains a valid file name and path.
> How do you know that there is something actually in @lines ?
> A print statement in the loop would be a good idea.
> How do you know that your error subroutine does actually print out
> something?
> How do you know that your error does NOT print out something?
> How do you know that your error does not return (and therby cause a
> cascading error).
> 
> Rewrite the code, add print statements, remove calls to external
> functions if you cal (error, IO::Handle).
> 
> 
> 
> Indy Singh
> IndigoSTAR Software -- www.indigostar.com
> 
> 
> ----- Original Message ----- From: "Madison Kelly" <linux at alteeve.com>
> To: "TPM Mailing List" <tpm at to.pm.org>
> Sent: Tuesday, October 16, 2007 3:53 PM
> Subject: [tpm] File handle problem in a script called by another script
> 
> 
> 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
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
> 

GAH!

Thanks both, turns out it was all fine and the problem was file 
permissions....

I'll go head->desk now.

Sorry for the line noise!

Madi


More information about the toronto-pm mailing list