Moose is an excellent suggestion.

#!/usr/bin/perl use strict; use warnings; my $red = Apple->new( color => 'green' ); my $yel = Apple->new( color => 'yellow' ); $red->color( 'red' ); # It got ripe. my $foo = Orange->new(); my $baz = Orange->new( apple => $red ); for my $obj ( $red, $yel, $foo, $baz ) { print 'served: ', $obj->serve, "\n"; } print "\n fruit stand: \n", map $_->dump, $red, $yel, $foo, $baz; BEGIN { package Apple; use Moose; use namespace::autoclean; has 'color' => ( is => 'rw', isa => 'Str', ); sub serve { return 'sliced'; } __PACKAGE__->meta->make_immutable; 1; } BEGIN { package Orange; use Moose; use namespace::autoclean; has 'apple' => ( is => 'ro', isa => 'Apple', predicate => 'has_apple', ); has 'shape' => ( is => 'ro', isa => 'Str', ); sub serve { my $self = shift; return $self->has_apple ? $self->apple->serve() : 'squeezed'; } __PACKAGE__->meta->make_immutable; 1; }

I like to put all my extra packages in BEGIN blocks since it makes them as much work as much like they were used as possible. I also put the trailing true value in, so that I can grab them and dump them straight into a separate file if it should become desirable to break them out. This simple step has prevented me from having to rerun zillions of tests via make test and prove


TGI says moo


In reply to Re^2: Perl OO best practice? by TGI
in thread Perl OO best practice? by smile4me

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.