.
I can't seem to figure out what I am doing wrong here. I keep getting ...
... errors. And I don't understand why.
... 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. |