Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Inspired by the discussion of How to use Inheritance with Inside-Out Classes and Hash Based Classes, I have added the capability for Object::InsideOut objects to inherit from non-Object::InsideOut classes. (I refer to this as foreign inheritance in the POD.) Thus, using Object::InsideOut, you can now sub-class other Perl classes, and have access to their methods from your own inside-out objects.

Here's a quick-and-dirty sample that illustrates foreign inheritance using Object::InsideOut v1.18:

use strict; use warnings; # Borg is a foreign hash-based class package Borg; { # A regular hash-based constructor sub new { return (bless({}, shift)); } # A 'get' accessor sub get_borg { my ($self, $data) = @_; return ($self->{$data}); } # A 'put' accessor sub set_borg { my ($self, $key, $value) = @_; $self->{$key} = $value; } # A class method sub assimilate { return ('Resistance is futile'); } } # Foo is an Object::InsideOut class that inherits from class Borg package Foo; { use Object::InsideOut qw(Borg); # A data field with standard 'get_/set_' accessors my @foo :Field('Standard'=>'foo'); # Our class's 'contructor' sub init :Init { my ($self, $args) = @_; # Create a Borg object and inherit from it my $borg = Borg->new(); $self->inherit($borg); } # A class method sub comment { return ('I have no comment to make at this time'); } } package main; # Inheritance works on class m +ethods print(Foo->comment(), "\n"); # Call a 'native' class meth +od print(Foo->assimilate(), "\n"); # Call a 'foreign' class met +hod my $obj = Foo->new(); # Create our object $obj->set_foo('I like foo'); # Set data inside our object print($obj->get_foo(), "\n"); # Get data from our object $obj->set_borg('ID' => 'I am 5-of-7'); # Set data inside inherited ob +ject print($obj->get_borg('ID'), "\n"); # Get data from inherited obje +ct
Other points of interest:
  • Muliple inheritance is supported with any mix of Object::InsideOut and foreign classes.
  • The encapsulation of the inherited objects is strong, meaning that only the class where the inheritance takes place has direct access to the inherited object.
  • Disinheritance is supported to remove the association with an inherited object.
Any comments or suggestions from my fellow monks would be greatly appreciated. Enjoy.

Remember: There's always one more bug.

In reply to Inside-out objects inheriting from hash-based classes by jdhedden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found