in reply to Re: regex transforms of object attributes
in thread regex transforms of object attributes

Nice tested code:)

The problem with doing it that way is that you build a dependancy upon the implementation of the class. You have to know that the class is based upon a hash and that the data return by the $obj->content(); method is stored in the hash under the key name 'content'.

If the implementation of the class changes for some reason, then it will require modification to every calling app also. That's what is meant by 'breaking encapsulation' or 'tight coupling'.

Eg. If the class called by your test code was implemented this way, it will break your test.

package Test; my %contents; sub new{ my( $class, $value ) = @_; my $self = bless \rand, $class; $contents{ $self } = $value; return $self; } sub content : lvalue { my( $self ) = shift; $contents{ $self }; } 1;

Of course, if the content has to be verified then using an lvalue sub will also break, but that a different argument entirely:)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^3: regex transforms of object attributes
by Plankton (Vicar) on Jun 19, 2004 at 01:47 UTC
    Well hopefully the implementation does not change. Yes I know famous last words. But lvalue is an experimental feature. That has its problems too.
    <update>It just occured to me that ...
    s/foo/bar/g;
    ... could take place in the class defintion. That wouldn't violate encapsulation would it? All one would have to do is add a method to the class. Something like ...
    sub s { my ( $obj, $pat, $sub ) = @_; $obj->{'content'} =~ s/$pat/$sub/g; }
    I wonder if it would be possible to use overload ... you know something like ...
    use overload "s" => \&s;

    Plankton: 1% Evil, 99% Hot Gas.