Well, there are several things that come to mind here. First off, you don't store the parameter hash you're giving the object in the constructor (actually you could bless the hash itself, but this is a bit beside the point). I would change the constructor to the following:
sub new {
my ( $invocant, %shape ) = @_; # Stash all params into shape
my $class = ref($invocant) || $invocant;
my $self = {
shape => \%shape, # Store a reference to the shape
};
return bless $self, $class;
}
Secondly, you're only making an object, not actually asking it to do anything. To get it to print, you have to callt the print_shape method:
my $box = shapes->new(shape => %box);
$box->print_shape(); # Do som real work
Thirdly, your print_shape is a bit odd, in that it only will print the first line of the shape, not the entire shape. To print the entire box, you would have to iterate through the hash you've stored in shape, but then you'll lose your ordering.
Lastly, to develop on the point of the print_shape, you should perhaps store some ordering information with the hash. As an alternative you could use an array instead of a hash, as this would retain the order of your lines. Eg:
sub new {
my ( $invocant, @lines ) = @_; # Take the lines in orden
# The line below is a bit cargo cultish, and I won't go into the reaso
+n here
# Super search for clone / constructor for more info / enlightenment
# my $class = ref($invocant) || $invocant;
my $self = {
shape => \@lines,
};
return bless $self, $invocant;
}
sub print_shape {
my $self = shift;
my @lines = @{ $self->{ 'shape' } };
foreach my $line( @lines ) {
print $lines, "\n";
}
}
This code is untested, of course. Good luck!
pernod
--
Mischief. Mayhem. Soap.
Update: Take a look at this node for an example discussion of the hornet's nest that is the copy-constructor. I realize that I was a bit impatient in my pointing to super search ... |