I have two perl functions defined, both run a set of shell commands on some somplied data and return hashs of the resulting parsed output from these shell commands.
One works, one doesn't and I can't seem to see why. It's driving me insane
The working one:
sub getcellstatus {
my %cells;
my @cellnames=@{$_[0]};
my @sysmanhandles;
foreach my $check (@allchecks) {
my $sysmancommand="$config{'su'} - $config{'user'} -c \'$config{'sysman'} ${check}Check -l all\'";
local *SYSMAN;
open(SYSMAN,"$sysmancommand|") || return;
push(@sysmanhandles, *SYSMAN);
}
foreach my $check (@allchecks) {
my $cellcount=0;
$sysmanhandle=shift @sysmanhandles;
while(<$sysmanhandle>) {
if (/^<([^>]*)> <([^>]*)> (.*)$/) {
$ip=$1;
$result=$2;
$note=$3;
$cells{$cellnames[$cellcount]}{$check}="${result}:${note}";
$cellcount++;
}
}
close($sysmanhandle);
}
%cells;
}
The not-working one:
sub getndstatus {
my %results;
my @nds=@{$_[0]};
my @sysmanhandles;
foreach my $check("EnableND","StartND") {
foreach my $nd(@nds) {
my $sysman="$config{'su'} - $config{'user'} -c \'$config{'sysman'} ${check}Check -m $nd\'";
local *SYSMAN;
open(SYSMAN, "$sysman|") || return;
push(@sysmanhandles, *SYSMAN);
}
}
foreach my $check("EnableND","StartND") {
foreach my $nd(@nds) {
$sysmanhandle=shift @sysmanhandles;
while(<$sysmanhandle>) {
if (/^<([^>]*)> <([^>]*)> (.*)$/) {
$ip=$1;
$result=$2;
$note=$3;
$results{$nd}{$check}="${result}:${note}";
}
close($sysmanhandle);
}
}
}
%results;
}
As you can see, each subroutine opens a set of filehandles, one per combination of inputs, gets them all running in the background (as they can take a few seconds to run) then parses the output from them in the order they were opened.
The non-working example above looks like it's working perfectly (I can see the shell commands executing via ps and the sulog) but nothing comes back via STDOUT when I go to read from the handle.
I've even tried adding:
> /tmp/test$check$$
to the end of the commandline and it does indeed write sensible output to the temp files that creates, but I'll be damned if I can get the same data to read off the filehandle instead.
I've also tried rewriting the subroutines to do each command one by one on a single handle, rather than use the array of handles and I get the same effect, the first one works (although more slowly), the second does not.
I've added print statements all throughout and the command looks good (if I copy-paste it into a prompt it runs just fine), but a print $_; in the while loops shows good data on the working one and nothing at all on the failing one.
I've even gone so far as to rewrite the troblesome routine from scratch to see if I just had some typo in there but the problem comes back even then.
I've bolded the filehandle open, close and commandline string creation to make it easier to read.
Any bright ideas? (And yes, I know the getcellstatus one is a bit ugly with all that $cellcount indexing but I'm afraid to touch it as it's the only one that works! :))