Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

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

I was recently playing with inheritance and built an object.pm because i was tired of haveing to remake those bits everytime. Here is my object.pm followed by your modified Car and Bike objects. I wish that inherited modules also used the same modules that there parent used....if that makes since. So that i wouldn't need strict, warnings,and Carp because they are all part of the parent object. Anyway here it is.

package object; use Carp; our $AUTOLOAD; use Data::Dumper; sub defaults { return {}; } sub readonly { return (); } sub init {}; sub params { my $self = shift; my $defaults = $self->defaults(); my %params = (%$defaults, @_); for (keys %params) { warn "unknown parameter '$_' passed to new()" unless exists $defaults->{$_}; $self->{$_} = $defaults->{$_}; $self->set($_, $params{$_}); } } sub AUTOLOAD { my $self = shift; my $type = ref($self) or croak "$self is not an object"; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion return if $name eq 'DESTROY'; unless (exists $self->{$name} ) { carp "Can't access `$name' field in class $type"; return; } { no strict 'refs'; *{$AUTOLOAD} = sub { $_[1] ? $_[0]->set($name, $_[1]) : $_[0]->g +et($name); } } &{$AUTOLOAD}($self,@_); } sub new { my $class = shift; my $self = {}; bless $self, $class; $self->params(@_); $self->init(); return $self; } sub _readonly { my ($self,$key) = @_; return 1 if grep {$_ eq $key} $self->readonly; return 0; } sub get { my ($self,$key) = @_; return $self->{$key} if exists $self->{$key}; warn "Accessing undefined key '$key'."; return undef; } sub set { my ($self,$key,$new) = @_; return if $self->_readonly($key); carp "Accessing undefined key '$key'." and return unless exists $se +lf->{$key}; my $old = $self->{$key}; $self->{$key} = $new; return $old; } 1;
package Car; use base object; use strict; use warnings; use Carp; sub defaults {{ Wheels => 4, Doors => undef, Color => undef, Passengers => undef }}; sub readonly { qw/Wheels/ }; 1;
package Bike; use base object; use strict; use warnings; use Carp; sub defaults {{ Wheels => 2, Doors => undef, Color => undef, Passengers => 2, }}; sub readonly { qw/Wheels Passengers/ }; 1;

___________
Eric Hodges

In reply to Re: OO Inheritence by eric256
in thread OO Inheritence by Limbic~Region

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
  • 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 cooling their heels in the Monastery: (11)
    As of 2015-07-30 09:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (270 votes), past polls