in reply to Can't call method "foo" on unblessed reference

What does getHoles return? An array of objects?

We're not really tightening our belts, it just feels that way because we're getting fatter.

Replies are listed 'Best First'.
Re^2: Can't call method "foo" on unblessed reference
by Plankton (Vicar) on Jun 16, 2004 at 18:00 UTC
    Yes getHoles returns an array of holes. At least I had hoped. Here's a shortened version of the board.pm where getHoles is defined.
    package board; ... use lib('.'); use hole; # # new - board constructor # # holes ) is an ref to a array of hole objects # This is not typically passed in to the # constructor. The constructor will build # the holes attribute $holes is not passed. # # level ) is also not typically used. # sub new { my ($pkg, $holes, $level) = @_; unless ( $holes ) { # # create the holes # ... } my $obj = bless { holes => $holes, # ref to array of holes level => defined( $level ) ? $level : 0 }, $pkg; return $obj; } sub getHoles { my $obj = shift; return wantarray ? @{$obj->{'holes'}} : $obj->{'holes'}; ... 1; }
    If you don't mind could you please take a look at Tam's Chinese Peg Game. That's should explain some of the why's and how's of data structures I am mangling. :)

    Plankton: 1% Evil, 99% Hot Gas.
      Within the code that you omitted:
      unless ( $holes ) { # # create the holes # ... }
      you call holes::new and insert it into @$holes? Is it possible for $obj->{'holes'} to return a reference to undef?

      Nothing else suggests itself at the moment.


      We're not really tightening our belts, it just feels that way because we're getting fatter.
        Hi Roy.

        Yes I do call hole::new in the code I omitted. Below is that code ...
        sub new { my ($pkg, $holes, $level) = @_; unless ( $holes ) { # # create the holes # my $holeLevel = 0; for ( my $i=0; $i<15; $i++ ) { if ( $i > 0 ) { $holeLevel = 1; } if ( $i > 2 ) { $holeLevel = 2; } if ( $i > 5 ) { $holeLevel = 3; } if ( $i > 9 ) { $holeLevel = 4; } # # the 5th hole is usaully has no peg initially. # $holes->[$i] = new hole( ( $i == 4 ) ? 'white' : 'black' , + $i, $holeLevel ); } # # link the holes # $holes->[0]->setLinks( [ $holes->[1], $holes->[2] ] ); $holes->[1]->setLinks( [ $holes->[0], $holes->[2], $holes->[3] +, $holes->[4] ] ); $holes->[2]->setLinks( [ $holes->[0], $holes->[1], $holes->[4] +, $holes->[5] ] ); $holes->[3]->setLinks( [ $holes->[1], $holes->[4], $holes->[6] +, $holes->[7] ] ); $holes->[4]->setLinks( [ $holes->[1], $holes->[2], $holes->[3] +, $holes->[5], $holes->[7], $holes->[8] ] ); $holes->[5]->setLinks( [ $holes->[2], $holes->[4], $holes->[8] +, $holes->[9] ] ); $holes->[6]->setLinks( [ $holes->[3], $holes->[7], $holes->[10 +], $holes->[11] ] ); $holes->[7]->setLinks( [ $holes->[3], $holes->[4], $holes->[6] +, $holes->[8], $holes->[11], $holes->[12] ] ); $holes->[8]->setLinks( [ $holes->[4], $holes->[5], $holes->[7] +, $holes->[9], $holes->[12], $holes->[13] ] ); $holes->[9]->setLinks( [ $holes->[5], $holes->[8], $holes->[13 +], $holes->[14] ] ); $holes->[10]->setLinks( [ $holes->[6], $holes->[11] ] ); $holes->[11]->setLinks( [ $holes->[6], $holes->[7], $holes->[1 +0], $holes->[12] ] ); $holes->[12]->setLinks( [ $holes->[7], $holes->[8], $holes->[1 +1], $holes->[13] ] ); $holes->[13]->setLinks( [ $holes->[8], $holes->[9], $holes->[1 +2], $holes->[14] ] ); $holes->[14]->setLinks( [ $holes->[9], $holes->[13] ] ); } my $obj = bless { holes => $holes, # ref to array of holes level => defined( $level ) ? $level : 0 }, $pkg; return $obj; }
        What do you think of the line ...
        $holes->[$i] = new hole( ( $i == 4 ) ? 'white' : 'black' , $i, $holeLe +vel );
        ... could that be the problem?

        Plankton: 1% Evil, 99% Hot Gas.