Where are you going to store your instance data?
Since you asked... in a variable outside the object itself: a
closure, in all likelihood. In my experience, this is the best
use for flyweight objects in Perl, far better than as an awkward
method of strong encapsulation. Here is how I'd do it:
#!/usr/bin/perl
use strict;
use warnings;
package BaseballPlayer;
use Carp;
my %attrib;
sub BEGIN {
%attrib = map { $_ => 1 }
qw( RBI Batting_Average Hits Runs Stolen_Bases Games_Played );
no strict 'refs';
for my $n ( keys %attrib ) { *$n = sub { $_[0]->{$n} } }
}
sub new {
my( $class, %arg ) = @_;
exists $attrib{$_} or croak "Unknown stat: $_" for keys %arg;
$arg{$_} ||= 0 for keys %attrib;
bless \%arg, $class;
}
package BaseballPlayer::Pitcher;
our @ISA = 'BaseballPlayer';
{
my %object;
sub new {
my( $class, %arg ) = @_;
my %pitcher_stat = map { $_ => delete $arg{$_} || 0 } qw( ERA Stri
+keouts );
my $base = BaseballPlayer->new(%arg);
my $ret = bless $base, $class;
$object{$ret} = \%pitcher_stat;
$ret;
}
sub ERA { $object{$_[0]}{ERA} }
sub Strikeouts { $object{$_[0]}{Strikeouts} }
sub DESTROY { delete $object{$_[0]} }
}
package main;
my $p = BaseballPlayer::Pitcher->new( Hits => 23, ERA => 4.32 );
print $p->Hits, "\n";
print $p->ERA, "\n";
Likely you have your own solution; if it is significantly different
(or especially if it's better) than mine, do share it. This solution
passes the tests you mentioned, and has become a regular habit for me.
I agree with your point: Perl doesn't make it easy to use inheritance.
This is a wordy, tiresome ritual, and thus is error-prone. Various
aspects of Perl's OO require such rituals, however; personally, I wouldn't
single out inheritance on this account.
Update: Changed $p's ERA to something realistic, upon zdog's
advice.
Update: Simplified my code, upon tye's advice. My inclusion
of needless code was a cargo-cult imitation of my own practices, which
reflected the needs of prior projects. This, I think, underscores my
point about the unfortunate effects of rituals which compensate for the
shortcomings of a language.
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|