Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Fellow Monks,

Today I encountered some issue trying to use Object inheritance and AUTOLOAD at the same time.

Let me explain : Let's say we have a Dog class, of which a Husky class inherits. AUTOLOAD is used to managed simple attributes, for exampe for setting and getting.

Now there are some attributes that should be managed by AUTOLOAD in the Dog class, and some specific attributes that should be in the Husky class. That raises the problem : AUTOLOAD is only called once, from the package the objects belongs to. There is not AUTOLOAD automatic inheritance.

Easy, will you say, let's call parent AUTOLOAD. Ok, by one must take care of the fact the scalar $AUTOLOAD is only defined in the object package.

So, to make it work, I finally wrote the code below. The idea is really simple, and I guess I may be reinventing the wheel, but anyway... Husky AUTOLOAD is as expected, but finally calls Dog's AUTOLOAD. Dog AUTOLOAD takes care of the fact that it could be called in two ways : either directly, if we have a Dog object, or from Husky AUTOLOAD. Comments are welcome...

# # Parent Object Dog # package Dog; use vars qw/$AUTOLOAD/; sub AUTOLOAD { my ($self, $autoload) = @_; my @attributes = qw / bone /; my $attr = $AUTOLOAD; # If called through Husky, retrieve the original $AUTOLOAD $attr ||= $autoload; $attr =~ s!(.*?)::!!; return unless $attr =~ /[^A-Z]/; # Get rid of DESTROY if ($attr =~ m!get_(.*)! and grep { /^$1$/ } @attributes) { print "Bonga\n"; return $self->{$1}; } } sub new { my $class = shift; return bless {}, $class; } # # Object Husky, A Husky is a Dog ;) # package Husky; #=pod use vars qw/$AUTOLOAD/; sub AUTOLOAD { my $self = shift; my @attributes = qw / eyes /; my $attr = $AUTOLOAD; $attr =~ s!(.*?)::!!; return unless $attr =~ /[^A-Z]/; # Get rid of DESTROY if ($attr =~ m!get_(.*)! and grep { /^$1$/ } @attributes) { print "Bongo\n"; return $self->{$1}; } $self->SUPER::AUTOLOAD($AUTOLOAD); } #=cut use base 'Dog'; # # Main package # package Main; my $billou = new Husky; print "Get bone : \n"; $billou->get_bone; print "Get eyes : \n"; $billou->get_eyes;

--
zejames

In reply to Object inheritance and AUTOLOAD by zejames

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, 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:
    & &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 avoiding work at the Monastery: (3)
    As of 2020-10-22 19:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (229 votes). Check out past polls.

      Notices?