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 ...


In reply to Re: Trouble getting started with Perl OO by pernod
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.