At 04:14 PM 8/16/00 +0100, Andy Wardley wrote:
>On Aug 16, 9:06am, Jonathan Scott Duff wrote:
> > Passing the lvalue via some other means eliminates this problem. I
> > forget who suggested it (Buddha Buck?) but
> >
> > sub foo : lvalue($value) { ... }
>
>That should tie in with function prototypes in general, but I suspect
>that the prototype should be on the function name, not on the attribute.
>
> sub foo ($value) : lvalue { }
>
>Thus the prototype covers both cases:
>
> $x->foo($y);
> $x->foo = $y;
I suggested sub foo : lvalue($value) {...} because those two cases are
-not-, in general, equivalent. For instance, I can see a generalized
multidimensional matrix class with these methods:
sub getelement { my @indices = @_; ... }
sub setelement ( my $val = pop @_; my @indices = @_; ... }
and say "$m->getelement(1,2,3,4,5)" to read that particular matrix element,
or "$m->setelement(1,2,3,4,5,0)" to clear the same element.
Now under perl5.6, I could replace those with:
sub element : lvalue { my @indices = @_; ... }
and have
print $m->element(1,2,3,4,5);
$m->element(1,2,3,4,5) = 0;
But there is no chance to check for sane values with such an
assignment. The matrix may be constrained so that all elements must fall
within a range, and that constraint should be preserved.
My proposal would allow me to do:
sub element : lvalue($val) {
my @indices = @_;
if (defined($val)) { verify constraints and set the element }
else { return the element value }
}
See the difference?
>A
>
>--
>Andy Wardley <[EMAIL PROTECTED]> Signature regenerating. Please remain seated.
> <[EMAIL PROTECTED]> For a good time: http://www.kfs.org/~abw/