Perl: Opening a filehandle but not getting anything back from it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Opening a filehandle but not getting anything back from it
# 1  
Old 08-11-2008
Question Perl: Opening a filehandle but not getting anything back from it

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 Smilie

The working one:
Code:
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:
Code:
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:
Code:
> /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! Smilie)
# 2  
Old 08-11-2008
I didn't know you were allowed to do stuff like that with file handles. Does it help if you upgrade to File::IO objects instead? They're supposed to make stuff like this doable without resorting to deep voodoo and hoping the symbol table stays sane.
# 3  
Old 08-11-2008
You localized a typeglob named SYSMAN in the loop where you build up
the filehandle array.
I am still also used to localizing filehandles like that.
But anyway, as current Perl releases allow to use lexically scoped variables as filehandles (see POD for open, or perlopentut)
have you tried if using them would make a difference?

May I ask why you are using two separate foreach loops in your subs?
If you put it in one loop there was also no need for the @sysmanhandles array.

Also I would assume that the backslashes that quote the single quotes in your su command are redundant here
Quote:
my $sysman="$config{'su'} - $config{'user'} -c \'$config{'sysman'} ${check}Check -m $nd\'";

Last edited by buffoonix; 08-11-2008 at 06:15 AM..
# 4  
Old 08-11-2008
Quote:
Originally Posted by buffoonix
But anyway, as current Perl releases allow to use lexically scoped variables as filehandles (see POD for open, or perlopentut)
have you tried if using them would make a difference?
I'll give that a try but I suspect I'll hit trouble as I'm obliged to use a fairly old perl (old system that's not to be fiddled with more than absolutely necessary).
Quote:
Originally Posted by buffoonix
May I ask why you are using two separate foreach loops in your subs?
If you put it in one loop there was also no need for the @sysmanhandles array.
Ah, this is my cunning way to improve the speed, the sysman commands are quite long-running so I'm starting all of them at once rather than running them one by one. I got the same problem with it dropping all stdout (it is actually executing, I can see the results of the command run) back when it was just a single filehandle and one foreach loop.
Quote:
Originally Posted by buffoonix
Also I would assume that the backslashes that quote the single quotes in your su command are redundant here
That's to make sure the entire sysman command gets passed to su properly, I don't want it to try and do something silly with the parameter groupings. That said, su is fairly smart so I'll try taking them out and see if it helps (simpler commandlines are always better IMO Smilie )
# 5  
Old 08-11-2008
Quote:
Originally Posted by era
I didn't know you were allowed to do stuff like that with file handles. Does it help if you upgrade to File::IO objects instead? They're supposed to make stuff like this doable without resorting to deep voodoo and hoping the symbol table stays sane.
I had the same trouble before I started doing voodoo though so I don't think it's a handle getting muddled up Smilie

I got the idea from my own insane musings then found an example on PerlMonks - The Monastery Gates
# 6  
Old 08-11-2008
Interesting news on this:
Once again, I rewrote it (feeling sure it had to be a typo someplace as such a similar function elsewhere was working) and it's now working!
The working code:
Code:
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|");
                        push(@sysmanhandles, *SYSMAN);
                }
        }
        foreach my $check("EnableND","StartND") {
                foreach my $nd(@nds) {
                        my $sysmanhandle=shift @sysmanhandles;
                        while(<$sysmanhandle>) {
                                if (/^<([^>]*)> <([^>]*)> (.*)$/) {
                                        $ip=$1;
                                        $result=$2;
                                        $note=$3;
                                        $results{$nd}{$check}="${result}:${note}";
                                }
                        }
                        close($sysmanhandle);
                }
        }
        %results;
}

It turned out the escaped single quotes in the command where required, otherwise su took the remaining parameters to sysman and assumed they where for itself instead.

I can't actually see any differences in this version but I made a special effort to write it 'blind' - that is, I avoided looking at the old code when I wrote this.
# 7  
Old 08-12-2008
Hi Smiling Dragon,

glad you could fix that.

Because my eyes got too distracted I copied and pasted your buggy and fixed versions of the second sub and made a diff
Code:
$ diff bla?
9c9
<                         open(SYSMAN, "$sysman|") || return;
---
>                         open(SYSMAN, "$sysman|");
15c15
<                         $sysmanhandle=shift @sysmanhandles;
---
>                         my $sysmanhandle=shift @sysmanhandles;
23d22
<                         close($sysmanhandle);
24a24
>                         close($sysmanhandle);

I think the removal of return on a failed open didn't make the difference
(though I would assume it to be better style after all if you couldn't reconcile with a die on that condition).
However, what I think made the difference was the explicit lexical redeclaration of $sysmanhandle.

I also would have advised to set the autoflush before the reading loop by squeezing in a
Code:
$|++;

hadn't you come up with the solution.

Sorry, for ill-advising you on presumed redundancy of quotes.
Even if I were right it is always better to be explicit.
I just was thinking about a talk I once attended at a Perl conference or workshop held by
Mark Jason Dominus (author of Higher Order Perl)
where he ranted about cave men dwellers' useless quoting voodoo.
He there brought onto my radar the wonderful Deparse module
to check what Perl really makes out of your expressions.
Have a look at
Code:
$ perldoc B::Deparse

in case you haven't come about it yet.
Quote:
I'll give that a try but I suspect I'll hit trouble as I'm obliged to use a fairly old perl (old system that's not to be fiddled with more than absolutely necessary).
The lexical vars as valid file handles to an open call became available as of Perl 5.6.0
if I remember correctly.
If your Perl is that old or even older then you should really consider an update Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing a message in file without opening it in perl

Hello friends, i have a perl script as below ... for (0 ..$#values) { ##want to print some message here in Report.txt file print `find /abc/xyz/pqr/$values" -type f -ls` >> Report.txt } I am able to get output of print `find /abc/xyz/pqr/$values" -type f -ls` >> Report.txt in... (2 Replies)
Discussion started by: harpal singh
2 Replies

2. Shell Programming and Scripting

perl FileHandle Closure during after unlock

Hi we have one function which is used to append data the file in exclusive lock mode in aperl script. This script is executed by multiple threads at the same time. accessing the same file.this script runs throught the day. sometimes the file2.txt size is getting reduced. for eg from 10 M... (1 Reply)
Discussion started by: Shahul
1 Replies

3. UNIX for Dummies Questions & Answers

Opening a file in perl

Hi I need to open a file if a condition(for example a if a regular expression) is met. How do i do this ? open (file) if (some regex)..... (3 Replies)
Discussion started by: manutd
3 Replies

4. Programming

Error:readline() on closed filehandle Perl

Hi, i have run the below perl code and i am getting an error Error:readline() on closed filehandle OR at run.pl line 31. CODE: =========================================== open OR,$ARGV; while (<OR>) { # find the batch date next if length $_ < 3; # BLANK LINE # last if $. > 120; #... (3 Replies)
Discussion started by: pspriyanka
3 Replies

5. Shell Programming and Scripting

Perl-opening a file then copying files

Good morning guys!! Im still practicing with Perl and now Im trying to open a file, and copy its contents to another file. Them I want to remeove the information out of the orginal file after it is copied over. The flow should be messages-->messages1-->messages2. Kind of like a log... (1 Reply)
Discussion started by: bigben1220
1 Replies

6. Shell Programming and Scripting

How to call a shell script from a perl module which uses Filehandle to login

Hi Guru's, Pardon me for the breach of rules..... I have very little knowledge about Shell Programming and Scripting hope you guys help me out of this troble I have very little time hence could not find the right way to direct my queries. coming to the problem I need to call a... (2 Replies)
Discussion started by: saikrishna_tung
2 Replies

7. Shell Programming and Scripting

Opening Mulitple files using For loop in Perl

Hi All, I have a total of ten file to open in the Perl script and i am using a for loop to open each file and capture some strings inside each file. Unfortunately, i encounter the below syntax error. I think there should be something wrong with this term reports_${counting}_${_}.txt but i do... (4 Replies)
Discussion started by: Raynon
4 Replies

8. Shell Programming and Scripting

Opening Files and checking contents in Perl

Hi All, I need some expert help in performing the following in Perl. I have a code below but it doesn;t seem to work. Can any expert give me some advice? Below are the requirements 1) Open numerous files assigned to an array @FILES. Note that the files are always named with the term... (7 Replies)
Discussion started by: Raynon
7 Replies

9. Shell Programming and Scripting

perl: howto print to screen & filehandle

Hello, I need to print messages both to screen and to file handle in perl , like tee does in unix . Any suggestions ? Thanks (2 Replies)
Discussion started by: Alalush
2 Replies

10. Shell Programming and Scripting

Opening Perl

I have gone to /usr/bin/ and click on perl but notting happens.also notting happens when i click on c/c++ or any other program whats wrong ? (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question