Perl hashes "Can't use subscript on private hash"

This is driving me mad, where am I going wrong?

The relevant segment of code:

sub getndsybcons {
        my @servers=@{$_[0]};
        my @sybservers=@{$_[1]};
        my %results;
        foreach my $server(@servers) {
                my $biggestsyb;
                my $biggestsybval=0;
                foreach my $sybserver(@sybservers) {
                        open(NETSTAT,"$config{'su'} - $config{'user'} -c \'ssh $server \"netstat -a\"\' | grep $sybserver | wc -l|") || return;
                        while(<NETSTAT>) { $tempval=$_ }
                        if ($tempval > $biggestsybval) {
                                $biggestsybval=$tempval;
                                $biggestsyb=$sybserver;
                        }
                }
                if ($biggestsybval > 5) {
                        $results{$server}=$biggestsyb;
                } else {
                        $results{$server}="";
                }
        }
        return %results;
}

The error:

Can't use subscript on private hash at ./ib-lib.pl line 22, near "$server}"
(Did you mean $ or @ instead of %?)
Can't use subscript on private hash at ./ib-lib.pl line 24, near "$server}"
(Did you mean $ or @ instead of %?)

Lines 22 and 24 are bolded.

This code is for a webmin module to control an application's various components.
This bit of perl is part of a library of functions called by the main GUI interface code. Webmin provides the %config structure.

I've never been that great with hashes at the best of times but this one's got me really boggled.

It feels like I've got my variable declarations wrong (I'm attempting to minimise global variables that are not used as constants (eg %config), but have been finding this rather fiddly as I'm _very_ rusty at "real" coding :wink: ) but I don't seem to be getting anywhere playing round with it...

Can someone cast some light on this for me?

I can't repro that. Do you have a global variable %results or a subroutine results elsewhere in your script? (Still can't repro with either of those, but I can't come up with anything better ...) Or just a missing closing brace somewhere -- those can trigger quite misleading error messages.

What's the point of an open("long pipeline |") loop if all you want is a single value? I'd use backticks for that. Also the return on error without a proper error message looks user-hostile and fragile. (Of course if the error checking is deferred to the caller then I suppose it's as it should be.)

hmm.... very weird. I think you should print the value or $server while the script runs, might help debug the problem. Just looking at the code I don't see any problem with syntax.

That's a really good point, it used to be a more complex regex :slight_smile:

I've since got it running, by removing the 'my' keyword where it's setting $server if the foreach loop:

my %results;
        foreach my $server(@servers) {
                my $biggestsyb;
                my $biggestsybval=0;

I won't pretend to understand why this worked though...

Me either, I can't see why that would make a difference. Is $server scoped to that block of code elsewhere in the script? What is the value of $server with "my" and without "my"?

If I leave the 'my' in, it won't execute at all, if I leave it out, $server has the correct value. Very hard to debug....

That just makes no sense from the code that you posted. What are the values in @servers?

That's the crazy bit, I'm not calling this function at all yet (!?!)
I've checked through the rest of the code and I'm not defining a variable called server or servers in any manner elsewhere.
I'll be using this function to track the number of connections to two different sybase backends to determin which one the application is currently pointing to, but that section is still unused.
Perl's calling it a syntax error by the looks of it, but I can't see how it possibly could be. It's making my head hurt. :confused: :confused: :confused:

At least it's working so far but I have a feeling I've done what era suggests and introduced a brace or quote someplace that's really confusing things. Not that I can find it anyplace and I'm generally careful with my formatting so problems like that are obvious...

Ah well, if I do find it, I'll post back here. Meantime, I'll press on and add it to the "it's a mystery file"

I'm interested in seeing your 'perl -V'. Is it possible that you are using a version either too old or too new, or from the unstable branch (say 5.7.x)?

It certainly looks like something someone at comp.lang.perl.misc may be interested in seeing ....

Interesting... It's a really old perl (5.005_03), I'll try a newer interpreter and see if the problem persists. Will report back :slight_smile: