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.


In reply to Re: Trouble getting started with Perl OO by gothic_mallard
in thread Trouble getting started with Perl OO by ant

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.