Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: Tutorial: Introduction to Object-Oriented Programming

by fruiture (Curate)
on Dec 12, 2002 at 21:12 UTC ( #219426=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Tutorial: Introduction to Object-Oriented Programming
in thread Tutorial: Introduction to Object-Oriented Programming

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


Comment on Re^5: Tutorial: Introduction to Object-Oriented Programming
Select or Download Code
Re: Re^5: Tutorial: Introduction to Object-Oriented Programming
by rdfield (Priest) on Dec 18, 2002 at 11:41 UTC
    Maybe I'm missing something with your implementation but wouldn't something like:
    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( 'colour' ); } }
    lead to the same problem that Abigail-II was talking about in the first place?

    rdfield

      Well, as long as you base such a thing upon a hash (and a variable solution like the Superclass must use a hash), you cannot avoid that, but it's limited to a few lines of accessor method code. I don't think it's productive to assume a typo wherever possible and to make it impossible to break things.

      This cannot happen when you create an accessor method automatically.

      --
      http://fruiture.de

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://219426]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (18)
As of 2014-08-27 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (242 votes), past polls