in reply to Re^12: Perl OO and accessors
in thread Perl OO and accessors
So we have different ideas of what attribute means, fine. I absolutely agree that my conclusion don't hold for your definition of attribute but I don't understand why you think it fails for my definition.
I still maintain that if the attribute behaves exactly like a stored piece of data then you should give it the same interface that we use for every other stored piece of data in the system. I haven't heard a single reason why this is undesirable in principle or an argument why set/get methods provide a superior interface.
Thinking in shell terms, consider the variables like RANDOM, SECONDS, etc. They change out from under you in a similar manner.
They're an example of something that should never have been a variable in the first place. In everything besides shell they are functions. I'd guess the reason they're variables in shell is because there was no other way to do it way back when they were introduced. I can't see any advantage to having them as variables. Can you?
Besides, $o->foo isn't a variable. It's a CODEREF.
It's neither a variable nor a CODEREF, it's a method invocation but that's not important. The point is that using lvalue and tie, it presents the interface of a variable and can be used anywhere that a variable previously might appear. So I can do
$x++; $o->foo++; $x=7 $o->foo = 7; chomp($x); chomp($o->foo); sub truncate { my $string = shift; if (length($$string) > 10) { $$string = substr($$string, 0, 10); } } truncate(\$x); truncate(\{$o->foo});
I don't understand your comments about ${$o->foo}. Why should I be comparing that? That brings us back to exposing a part of the object directly plus it's ugly as you point out. The fact that it *would* be a variable is not only irrelevant but actually a disadvantage. I'm not saying that $circle->Area *is* a variable just that it has the exact same interface and behaviour as a variable even though internally $circle actually stores the radius not the area.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^14: Perl OO and accessors
by Perl Mouse (Chaplain) on Nov 30, 2005 at 21:13 UTC | |
by fergal (Chaplain) on Dec 01, 2005 at 00:58 UTC |