Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

On the 11th our I look to the great monks for a stay of execution

I have one class:

package Node use strict; my ($id, $u, $p, $log, @mess); sub New { my $class =shift; my $this ={}; @mess =(); $log =0; bless ($this, $class); return ($this); }
and another class:
package System; use strict; my (@nodes, $n1, $server, @messages); ... sub ANodeLogin{ my $self =shift; use Node; my $node=Node->New; push (@nodes, \$node); ... sub ANodeLogout{ my $self =shift; my $node; print "What node would you like to log out of\n"; foreach $node (@nodes){ print "--->$node->{u}<---\n";#this is line 62 } }
Which produces this: Not a HASH reference at System.pm line 62, <STDIN> line 6. Please help me see why I cannot defrefrence this object in the array of objects called @nodes. Sincerely, Lost

Replies are listed 'Best First'.
Re: derefrencing an object
by Thelonius (Priest) on May 03, 2004 at 17:01 UTC
    It looks like this line:
    push (@nodes, \$node);
    is pushing a reference to a scalar. This scalar happens to be a reference to a hash. So you don't have a reference to a hash in line 62, you have a reference to a reference to a hash. Probably what you want to do is:
    push (@nodes, $node);
      I made the change and I still get the same error. Could it be b/c I use my $node in ANodeLogin and once I leave that methode $node evaporates? thanks for your help!
        Could it be b/c I use my $node in ANodeLogin and once I leave that methode $node evaporates?

        That shouldn't be a problem. The object should still exist as long as there's a reference to it. It's hard to say what's wrong looking at just these few lines. What I would probably try next is to add:

        use Data::Dumper; print Dumper($node);
        right before line 62.

        Update: Given what sgifford says below, I would suspect either (1) you have another code path which adds to @nodes, or (2) you didn't change the code you were running. In fact, one reason I suggested adding the print Dumper statement was to make sure that you were changing (and saving) the right code. Don't feel too bad if this happens to you. I have to be on the guard against this all the time; it has happened to me too many times over the years. Given how Perl can search many places for a module, that I have two different versions of Perl on my PC, that I often edit on a different system from the one I run on, that there are development, test, and production environments, etc., etc. you have to keep on your toes. Eternal vigilance!

        This fix works for me. I'm running this program: and it outputs:
        What node would you like to log out of
        --->0<---
        --->1<---
        --->2<---
        --->3<---
        
        Without the fix from Thelonius, I get the error you were seeing.

        Maybe the problem is being introduced elsewhere in your program.