Sponsored Content
Top Forums Shell Programming and Scripting Perl: Opening a filehandle but not getting anything back from it Post 302223617 by Smiling Dragon on Monday 11th of August 2008 01:35:08 AM
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)
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy