mop problem 2 - Can't weaken attribute variable from outside

mop can't weaken attribute variable from outside. mop have weaken feature, but this is defined in only class and this work only when value is set. We can't weaken attribute from outside. This is not useful and some problems will occur.

# Hash base object
my $point = Point->new;
my $foo = {bar => 1};
$point->foo($foo);
weaken $point->{foo};

# mop
# We can't weaken attribute variable from outside of object.

Perl garbage collection is reference-count type. This mean if objects is referenced each other(A -> B -> A) or circle(A -> B -> C ->A), object is not released. so we should be able to weaken any object freely.

1 Comment

Fiddling with an attribute from outside the class is a recipe for disaster anyway. Let's consider the Person class; all Person objects have a Head object associated with them because all (living) people have heads.

The Person object probably has many methods which make use of the Head (e.g. methods: speak, look, eat, drink). Because the head is a required attribute, and it is checked by the constructor, the Person object can then assume that its Head will always be available to help implement these methods.

If it's possible to weaken a Person's Head from outside, it will suddenly become undef when all other references to the Head object go out of scope. The Person gets no notification that the Head has gone out of scope (therefore no opportunity to grow a new replacement head). It's head is suddenly no longer there. Those methods (like speak, look, drink, eat) suddenly break in interesting ways.

If an attribute needs to be weakened, then this functionality needs to be built into the class, so that the class is aware of (and can cope gracefully with) the possibility that the attribute can suddenly become undef.

Leave a comment

About Yuki Kimoto

user-pic I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.