Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://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":



  • 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 contemplating the Monastery: (4)
As of 2024-04-19 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found