Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: object oriented tutorial question

by Anno (Deacon)
on Sep 04, 2007 at 00:36 UTC ( [id://636808]=note: print w/replies, xml ) Need Help??


in reply to Re^2: object oriented tutorial question
in thread object oriented tutorial question

Your inside-out implementation is missing the obligatory destructor.
sub DESTROY { my $id = refaddr shift; delete $_->{ $id} for \ ( %street, %city, %state, %zip); }

I'd like to contrast an implementation of the Address class based on the Alter module. It is itself hash-based and thus closer to the original hash-based implementation than the inside-out variant, while still offering the black-box-inheritance properties. Essentially, the changes are that the ego() function is called on the object before each access to the object data.

#class Address (Alter-based) package Address; use strict; use warnings; use Alter ego => {}; # alter ego is a hash #constructor sub new { my ($class) = @_; my $self = \ my $o; # the object proper (a scalar ref) %{ ego $self } = ( # set up the ego for this class _street => undef, _city => undef, _state => undef, _zip => undef ); bless $self, $class; return $self; } sub street { my ( $self, $street ) = @_; ego( $self)->{_street} = $street if defined($street); return ego( $self)->{_street}; } sub city { my ( $self, $city ) = @_; ego( $self)->{_city} = $city if defined($city); return ego( $self)->{_city}; } sub state { my ( $self, $state ) = @_; ego( $self)->{_state} = $state if defined($state); return ego( $self)->{_state}; } sub zip { my ( $self, $zip ) = @_; ego( $self)->{_zip} = $zip if defined($zip); return ego( $self)->{_zip}; } sub print { print "Address: $_->{_street}\n". "$_->{_city}\n" . "$_->{_state}\n" . "$_->{_zip}\n\n" for ego shift; } 1;
Anno

Replies are listed 'Best First'.
Re^4: object oriented tutorial question
by cdarke (Prior) on Sep 04, 2007 at 10:54 UTC
    Oops. Apologies.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found