in reply to Trouble getting started with Perl OO

Taking a quick glance I agree with most of what has already been posted here. Your two main problems are a) not using the data you pass to the constructor and b) only attempting to print the first line of data.

I tend to agree that the data in this case should be passed as an array reference and not a hashref. Passing as a hashref is limiting you to either only having boxes with the same number of lines each time:

# Only ever produces 3 lines sub print_shape { my $self=shift; print $self->{shape}->{line1}."\n"; print $self->{shape}->{line2}."\n"; print $self->{shape}->{line3}."\n"; }

or having to get into some weird eval stuff:

# Allows for arbitary number of lines # (as long as they are sequentially numbered from 1) sub print_shape { my $self=shift; my $em=''; for (my $i=0; $i<scalar(keys %{$self->{shape}}); $i++) { $em = 'print $self->{shape}->{line' . ($i+1) . '}."\n"'; eval($em); } }

Would be much neater to define your shape thus:

my $box = [ '----', '| |', '----' ];

... which would mean you could then have your print_shape() as a nice little:

sub print_shape { my $self=shift; foreach (@{$self->{shape}}) { print "$_\n"; } }

Remember to be careful with hashes as you can't ever gurantee you'll get the keys/values back out in the order in which you put them in (unless you start sorting.

--- Jay

All code is untested unless otherwise stated.