Has-a relationship methods are generally not directly usable by callers of the delegating class. calling scratch directly on cat doesn't indicate which paw should scratch. Hence, the delegating class has-a method that uses it parameter(s) to determine which instance of paw should do the scratching.

#! perl -slw use strict; package paw; sub new { my $class = shift; return bless { @_ }, $class; } sub scratch { my $self = shift; print "Scratching with $self->{ which } paw"; } package cat; sub new { my $class = shift; my %self = @_; $self{ paws } = { map{ $_ => paw->new( which => $_ ) } 'left front', 'right front', 'left hind', 'right hind' }; return bless \%self, $class; } sub scratchesWith{ my( $self, $paw ) = @_; $self->{ paws }{$paw}->scratch(); } sub speaks { my $self = shift; print $self->{name}, ' says: ', $self->{voice}; } 1; package main; my $cat = cat->new( name => 'Tiddles', voice=> 'Meow!', ); $cat->scratchesWith( 'right front' ); $cat->speaks(); __END__ [ 4:08:26.92] P:\test>405381 Scratching with right front paw Tiddles says: Meow!

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re^5: Help me understand inheritance please! by BrowserUk
in thread Help me understand inheritance please! by jdtoronto

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.