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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.