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.
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
As you can see, you can inherit from an inside out object, and use tradition hash based objects in the derived class.

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

    I note that for the simple example there, the methods and variables are completely independent of each other.

    What happens when the child needs to affect the parent's values? I suspect it would be difficult to keep the Traditional child pure in a practical situation, and you'd end up with an inflexible hybrid child, or a significant re-implementation of the parent to handle the Traditional hijinks that ensue when get/set methods are skipped.

      What happens when the child needs to affect the parent's values?
      Then the child uses the API the parent provides, typically in the form of accessors.

      Or do you mean "the child cannot access the attributes of the parent directly?" Well, of course it cannot. That's what the OP wanted, and what Inside-Out objects can provide. (Note that Inside-Out objects can allow for direct access by using 'our' instead of 'my' for the attribute hashes).