in reply to Make your classes use their own methods

Classes need to use their own methods inside the class, and not access the internal object store directly, even though Perl allows it.
"Perl" only allows it if you allow it (heard of "inside out objects"?).
  • Comment on Re: Make your classes use their own methods

Replies are listed 'Best First'.
Re: Re: Make your classes use their own methods
by Anonymous Monk on Nov 24, 2003 at 17:59 UTC
    "Perl" only allows it if you allow it (heard of "inside out objects"?).

    "Inside out objects" do not prevent direct access to the attribute data within the class!

      "Inside out objects" do not prevent direct access to the attribute data within the class!

      While I think that the AM missed the point Andy was making they are correct on the inside out object front. You can use inside out objects to prevent a class from having global access to attribute data. For example:

      package MyClass; { # we make an attribute hash private to the foo method my %foo; sub foo { my $self = shift; $foo{$self} = shift if @_; return $foo{$self}; }; }; sub bar { my $self = shift; # so we can only access %foo using method foo here print $self->foo; };

      However, regardless of whether you implement your accessors with inside out objects or normal blessed hashes, Andy's point about being consistant with your use of accessors is a very good rule of thumb. It's certainly my policy.

      If you need to make your attribute setters/getters public (and that's often a big "if" as others have pointed out) then using them consistantly is a good idea for all the reasons Andy mentioned in the OP.

      Of course with a sane language design like Eiffel (or, I think, Perl 6 ;-) the syntax for attribute access and method calls is identical so the whole issue just disappears.

        they are correct on the inside out object front. You can use inside out objects to prevent a class from having global access to attribute data. For example:

        Yes, but that would be an "inside out object" with or without the closure. Using an "inside out object" isn't preventing direct access, using the closure is. A small, but non-trivial difference.

Re: Re: Make your classes use their own methods
by petdance (Parson) on Nov 24, 2003 at 17:13 UTC
    Fine, fine, change "Perl" to "the way people usually do objects in Perl, with a blessed hash."

    xoxo,
    Andy