Hadn't someone (Abigail, perhaps) brought up the notion of per-object methods? That, is something not unlike:
package GenObj; my %methods = ( name => sub { $_[0]{DATA}{name} }, setname => sub { $_[0]{DATA}{name} = $_[1] }, # ... ); sub new { my ($class) = @_; bless { DATA => {}, METHODS => {%methods} }, $class; } AUTOLOAD { (my $method = $AUTOLOAD) =~ s/.*:://; $_[0]{METHODS}{$method} ? goto &{$_[0]{METHODS}{$method}} : die "unsupported method $method for object"; } package main; $foo = GenObj->new; $foo->setname("Jeff"); print $foo->name;
Update
To stop the repeated calls to the AUTOLOAD method, I think it would be pragmatic to set each object up in its own sub-class of the main class. Then, after the first call to a method has been made, the methods are created as functions in the sub-class. Perhaps we could do away with the hash of default methods altogether. Take this example:
$x = new GenObj; # ref($x) eq 'GenObj::a' $y = new GenObj -foo; # ref($y) eq 'GenObj::b' $x->get_foo; # calls $x->GenObj::get_foo $y->get_foo; # calls $y->GenObj::b::get_foo
Here's my sample implementation:
package GenObj; # the following methods can be generated # automatically with one of the Class:: # modules... # these are the EXPECTED inherited methods # of GenObj objects sub set_name { $_[0]{name} = $_[1] } sub get_name { $_[0]{name} } sub set_age { $_[0]{age} = $_[1] } sub get_age { $_[0]{age} } my $CHILD = 'a'; sub new { no strict 'refs'; # naughty things transpire my $class = shift; my $obj = bless {}, "${class}::$CHILD"; @{"${class}::${CHILD}::ISA"} = ($class); for (map s/^-//, @_) { *{"${class}::${CHILD}::get_$_"} = \&{"${class}::Sub::get_$_"}; *{"${class}::${CHILD}::set_$_"} = \&{"${class}::Sub::set_$_"}; } return $obj; ); package GenObj::Sub; # these are the specialized methods # to be inherited on demand sub set_name { $_[0]{name} = condense($_[1]) } sub get_name { expand($_[0]{name}) } sub set_age { $_[0]{age} = age2sec($_[1]) } sub get_age { sec2age($_[0]{age}) } # these are some utility functions # as called above sub condense; sub expand; sub age2sec; sub sec2age; 1;
I have not yet tested this code, but its use is something like:
use GenObj; my $normal = new GenObj; my $diffage = new GenObj -age; my $diffname = new GenObj -name; my $diffboth = new GenObj -age, -name; $normal->set_age(10); # GenObj::set_age $normal->get_name; # GenObj::get_name $diffage->set_age(10); # GenObj::Sub::set_age $diffage->get_name; # GenObj::get_name $diffname->set_age(10); # GenObj::set_age $diffname->get_name; # GenObj::Sub::get_name $diffboth->set_age(10); # GenObj::Sub::set_age $diffboth->get_name; # GenObj::Sub::get_name


$_="goto+F.print+chop;\n=yhpaj";F1:eval

In reply to Re: Best Method of Object-Oriented Behavior Addition? by japhy
in thread Best Method of Object-Oriented Behavior Addition? by princepawn

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.