Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Trouble getting started with Perl OO

by pernod (Chaplain)
on Oct 07, 2004 at 12:08 UTC ( #397271=note: print w/replies, xml ) Need Help??

in reply to Trouble getting started with Perl OO

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!

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

Replies are listed 'Best First'.
Re^2: Trouble getting started with Perl OO
by ant (Scribe) on Oct 07, 2004 at 12:24 UTC
    Thanks for the reply,
    It was only after submitting the question that i realised that I had left out the
    and also printing of line two and three in the method print_shape. So all that was in the original code.
    My original idea was to have each shape 3 lines long, and then just hardcode the printing of the hash. I have this sample code running with arrays, but was using a hash for extra learning value.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://397271]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2023-12-04 00:00 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (20 votes). Check out past polls.