Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
There is probably a better way to do this. (Likely on CPAN.) Plus this is slow. But here is a very simple implementation of a different inheritance scheme than the one that Perl uses. This is based on each object being its own class. Call the def method to add new methods to the object. Call new to generate an object that inherits from the parent. Call "call" to call methods you have inherited, or just call them directly. There is a basic search mechanism. Multiple inheritance is not supported. Methods follow the usual Perl semantics. (The first argument is the object, the rest are arguments to the method.)
package ObjectClass; use Carp; use strict; use vars qw($AUTOLOAD); sub call { my $self = shift; my $meth = $self->find_meth(shift); $meth->(@_); } sub def { my $self = shift; { my $meth = shift; my $implement = shift; $self->{meth_cache}->{$meth} = $implement; redo if @_; } } sub find_meth { my $self = shift; my $meth = shift; my $cache = $self->{meth_cache}; if (exists $cache->{$meth}) { return $cache->{$meth}; } elsif (exists $self->{proto}) { return $self->{proto}->find_meth($meth); } else { confess ("Object does not implement method '$meth'"); } } sub new { my $proto = shift; # Note for Merlyn, this time it is not cargo-cult programming! :-P unless (ref($proto)) { my $class = $proto; $proto = { meth_cache => {}, }; bless($proto, $class); } my $self = { meth_cache => {}, proto => $proto }; bless $self, ref($proto); } sub AUTOLOAD { my $self = shift; $AUTOLOAD =~ s/.*:://; return if $AUTOLOAD eq "DESTROY"; $self->call($AUTOLOAD, @_); } package main; # A simple test my $parent = new ObjectClass; $parent->def("hello", sub {print "Hello world\n"}); my $child = $parent->new(); $child->def("goodbye", sub {print "Goodbye\n"}); $parent->hello(); # Calling the defining object $child->hello(); # Inherit the method $child->goodbye(); # Method in the subclass $parent->goodbye(); # Blows up.
Does this make your mock classes easier to implement?

UPDATE
I did this mostly because I thought it would be fun to write. If you want to use this idea, I would suggest looking at Class::Classless or Class::SelfMethods.


In reply to Re (tilly) 1: Nested Classes by tilly
in thread Nested Classes by dcorbin

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2014-09-23 06:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (210 votes), past polls