in reply to Re: Make your classes use their own methods
in thread Make your classes use their own methods

"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!

  • Comment on Re: Re: Make your classes use their own methods

Replies are listed 'Best First'.
Re^3: Make your classes use their own methods
by adrianh (Chancellor) on Nov 25, 2003 at 00:17 UTC
    "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.

        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.

        To-may-to. To-mah-to. Personally I think it is a trivial distinction.

        Any use of lexically scoped hashes by classes implemented using inside out objects create closures - whether block or file scoped. So it's not using a closure that's making the difference, it's using a particular lexical scope for the attribute hash.

        The point is that classes implemented in an inside-out style allow you to scope object attributes at a finer level of detail than the whole class. Something that the traditional approach of sticking the attributes in a blessed hash does not.