Friends,

I am trying to automate my Tam's Chinese Peg Game. I can't seem to figure out what I am doing wrong here. I keep getting ...
 Can't call method "hasPeg" on unblessed reference at boardTree.pm line 40.
... errors. And I don't understand why.

Line 40 is this ...
if ( $jumper->hasPeg() ) {
... line of the sub makeLinks.
Please excuse me if this seems like too much code to post. But here is the entire boardTree.pm package.
package boardTree; use lib('.'); use board; use hole; use warnings; use diagnostics; use Data::Dumper; use strict; use Exporter; use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = (); @EXPORT_OK = qw( &new ); %EXPORT_TAGS = ( DEFAULT => [qw ( &new )] ); # # new - boardTree constructor # sub new { my ($pkg, $board, $index, $level, $links ) = @_; my $obj = bless { board => $board, index => defined( $index ) ? $index : 0, level => defined( $level ) ? $level : 0, links => defined( $links ) ? $links : [] }, $pkg; return $obj; } sub makeLinks { my $obj = shift; my $nextLevel = $obj->{'level'} + 1; print "nextLevel => $nextLevel\n"; for my $jumper ( $obj->{'board'}->getHoles() ) { print __FILE__ . " $jumper\n"; if ( $jumper->hasPeg() ) { for my $jumpy ( $jumper->getLinks() ) { my $rc = $jumpy->jumpingOver( $jumper->getIndex() ); if ( $rc > -1 ) { my @nholes = (); for ($obj->{'board'}->getHoles()) { my %newHole = %$_; push ( @nholes, \%newHole ); } $nholes[$jumpy->getIndex()]->{'peg'} = 'white'; $nholes[$jumper->getIndex()]->{'peg'} = 'white'; $nholes[$rc]->{'peg'} = 'black'; my $newBoard = new board( \@nholes, $nextLevel ); push( @{$obj->{'links'}}, new boardTree ( $newBoard, $obj->{'index'} + 1, $nextLevel, [] )); $obj->{'links'}[$obj->getNumLinks()-1]->makeLinks( +); } } } } } # # returns the links attribute # sub getLinks { my $obj = shift; return @{$obj->{'links'}}; } sub getNumLinks { my $obj = shift; return $#{@{$obj->{'links'}}} + 1; } # # print out the object # sub dumpPretty { my $obj = shift; print __FILE__ . "[" . __LINE__ . "] level => " . $obj->{'level'} +. ":\n"; $obj->{'board'}->dumpPretty(); print "\tno links = " . $obj->getNumLinks() . "\n"; for my $l ( $obj->getLinks() ) { $l->dumpPretty(); } }
I suspect I am not following the explantion of cloning of an array in Copying an Array of Hashes. I attempt to make a copy of a boards holes on these lines ...
my @nholes = (); for ($obj->{'board'}->getHoles()) { my %newHole = %$_; push ( @nholes, \%newHole ); } $nholes[$jumpy->getIndex()]->{'peg'} = 'white'; $nholes[$jumper->getIndex()]->{'peg'} = 'white'; $nholes[$rc]->{'peg'} = 'black'; my $newBoard = new board( \@nholes, $nextLevel );
... It seems to that @nholes is not getting into the $newBoard object correctly. Here is the output I am getting.

$ ./testBoardTree.pl 
Can't call method "hasPeg" on unblessed reference at boardTree.pm line 40 (#1)
    (F) A method call must know in what package it's supposed to run.  It
    ordinarily finds this out from the object reference you supply, but you
    didn't supply an object reference in this case.  A reference isn't an
    object reference until it has been blessed.  See perlobj.
    
Uncaught exception from user code:
        Can't call method "hasPeg" on unblessed reference at boardTree.pm line 40.
        boardTree::makeLinks('boardTree=HASH(0x193b930)') called at boardTree.pm line 60
        boardTree::makeLinks('boardTree=HASH(0x198c240)') called at ./testBoardTree.pl line 46
nextLevel => 2
boardTree.pm hole=HASH(0x19504ac)
boardTree.pm hole=HASH(0x195b27c)
boardTree.pm hole=HASH(0x1958a98)
boardTree.pm hole=HASH(0x1975a70)
boardTree.pm hole=HASH(0x1975c38)
boardTree.pm hole=HASH(0x193bc9c)
boardTree.pm hole=HASH(0x194d804)
boardTree.pm hole=HASH(0x198718c)
boardTree.pm hole=HASH(0x19833ac)
boardTree.pm hole=HASH(0x193aeb0)
boardTree.pm hole=HASH(0x196ceb8)
boardTree.pm hole=HASH(0x198724c)
nextLevel => 3
boardTree.pm HASH(0x193b4bc)
What do you think I am doing wrong? Note that for some reason at nextLevel => 3 hashes don't seem to be recognized as holes. I think this happens at the 1st level of recursion.

Plankton: 1% Evil, 99% Hot Gas.

In reply to Can't call method "foo" on unblessed reference by Plankton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.