in reply to Re^3: Prevent direct acces to object's attributes
in thread Prevent direct acces to object's attributes
If an object already is an inside-out object, you have no choice but to continue down that road.That's not true.
As you can see, you can inherit from an inside out object, and use tradition hash based objects in the derived class.package InsideOut; use strict; use warnings; use Hash::Util::FieldHash qw[fieldhash]; fieldhash my %name; sub new { my $class = shift; bless do {\my $var}, $class; } sub set_name { my ($self, $name) = @_; $name{$self} = $name; } sub get_name { my ($self) = @_; $name{$self}; } 1; __END__ package Traditional; use strict; use warnings; use InsideOut; our @ISA = qw[InsideOut]; sub new { my $class = shift; bless {}, $class; } sub set_colour { my ($self, $colour) = @_; $self->{colour} = $colour; } sub get_colour { my ($self) = @_; $self->{colour}; } 1; __END__ #!/usr/bin/perl use 5.010; use strict; use warnings; use Traditional; my $obj = Traditional->new; $obj->set_name("NAME"); $obj->set_colour("COLOUR"); say "The name is ", $obj->get_name, " and its colour is ", $obj->get_c +olour; __END__ The name is NAME and its colour is COLOUR
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Prevent direct acces to object's attributes
by SuicideJunkie (Vicar) on Sep 08, 2009 at 14:20 UTC | |
by JavaFan (Canon) on Sep 08, 2009 at 16:12 UTC |