Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

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

Perhaps it helps if i use "attribute" instead of "member", so one can't confuse "member" with "instance"...

Abigail's proposal is to store attribute values of instances in lexical (or only package-global) hashes to make them safe from subclasses that might clobber attributes that are private to the superclass:

sub some_method { my $self = shift; $self->{'_very_internal_flag'} = 1; #... }

A subclass might also need a '_very_internal_flag' (stored as attribute of an instance) and suddenly it all breaks.

So the idea is to store the attribute data in kind-of "registry": a central repository that keeps thes attributes in it's defining class.

My first thought on that was to write a module that is a superclass and contains such a registry for all classes. It gives you two methods: get( attribute ) and set(attribute,value) (probably with diofferent names). These methods get/store these values in that central registry and determine the class, in which that actual attribute was defined and thus must be kept apart from other descending classes defining the same attribute. I thought of something like that because of my lazyness: it would save typing for many classes based on that principle.

Such a module is easy to write:

package OO; # RFC use strict; use warnings; use Carp qw/croak/; # # A very base class of all classes # # # The registry # my %Object = (); # Class => field => object # # inheritable constructor (RFC: should there be one?) # sub new { my $class = shift; # so now objects are themselves dummies # why not store debugging information of an object's origin by + default? # if you don't like this, don't inherit the constructor! my $self = bless [ caller ], $class; # is that neccessary if you can override new() if( $self->can( 'initialize' ) ){ $self->initialize( @_ ); } return $self; } # # oo_create_accessor class method (RFC: usefull?) # sub oo_create_accessor { my $pkg = shift; no strict "refs"; foreach my $mem ( @_ ){ my $symbol = $pkg . '::' . $mem; if( defined *{ $symbol } ){ croak "Attempt to redefine $symbol via create_ +accessor"; } else { *{ $symbol } = sub { my $self = shift; if( @_ ){ $self->oo_set( $mem , $_[0] , +$pkg ); } else { $self->oo_get( $mem , $pkg ); } }; } } } # # Use these two methods to get and set members of your # object and they will do encapsulation for you # # BE CONSISTENT our your OO will BREAK! # sub oo_get { my $obj = shift; my $field = shift; # member hash is based on caller class # and may be overwritten by third argument to get() my $class = @_ ? shift : caller; $Object{ $class }{ $field }{ $obj } } sub oo_set { my $obj = shift; my $field = shift; my $value = shift; my $class = @_ ? shift : caller; $Object{ $class }{ $field }{ $obj } = $value } # # debugging function # sub oo_registry { #intended as class/instance method or funtion return \%Object } 1;

Usage as follows:

package Car; use base 'OO'; sub color { #my accessor/mutator my $self = shift; if( @_ ){ $self->oo_set( 'color' , $_[0] ); } else { $self->oo_get( 'color' ); } } package Rover; #a class of it's own ;) sub get_set_color { my $self = shift; if( @_ ){ $self->oo_set( 'color' , $_[0] ); } else { $self->oo_get( 'color' ); } }

The above example shows how the subclass Rover defines an attribute that perhaps is ment to override the Car's color attribute but it won't: The attribute you access via get_set_color() is different from the attribute you access via color(), because they are accessing the data from different packages. The subclass can only override the superclass' attribute by using the superclass' method for that (or by using the optional last argument to oo_get,oo_set, which is the dirty/unofficial method).

This is great, but it's different from what you're used to when writing objects that store their attributes(aka members) in themselves.

--
http://fruiture.de

In reply to Re^5: Tutorial: Introduction to Object-Oriented Programming by fruiture
in thread Tutorial: Introduction to Object-Oriented Programming by jreades

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 romping around the Monastery: (10)
    As of 2014-09-23 08:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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











      Results (212 votes), past polls