> Damian Conway wrote:
> >
> > > Now, if this is what you want, add a :readonly attribute:
> >
> > So the proposal is: make the dangerous one the default.
> > I don't think that's a good idea.
>
> You're going to have to explain to me how these differ in their
> dangerousness:
>
> $r->func = $x; # this more dangerous
> $r->func($x); # than this somehow?
>
> I know that the current implementation of :lvalue subs is broken.
> However, if this is fixed and $x is just a regular argument (so that the
> two examples above are equivalent in every way), then it seems there is
> no difference.
As an alternative, I'd rather see something like:
package Foo;
sub new {
my %foo = (foo => 0, bar => 0, bat => 0);
}
sub foo : lvalue($newval) {
# $newval is a lexical scaler in this block, initialised with a
reference
# to the value to be assigned to $self{foo}
my $self = shift;
return $self{foo} unless defined $newval; # rvalue context
# lvalue context
# verify new foo is in correct range
return $self{foo} if $$newval < $MINFOO;
return $self{foo} if $$newval > $MAXFOO;
# invarient: foo == phi*bar == phi*phi*bat
$self{foo} = $$newval;
$self{bar} = $self{foo} / $PHI;
$self{bat} = $self{bar} / $PHI;
return $self{foo};
}
sub bar {
$self{bar};
}
sub bat {
return $self{bat};
}
package main;
$myfoo = Foo::new();
$myfoo->foo = 1;
print "(foo,bar,bat) = ($myfoo->foo,$myfoo->bar,$myfoo->bat)\n";
# prints "(foo,bar,bat) = (1,1.6180339887,2.6180339887)
print 1/$myfoo->bar; #prints "0.6180339887"
$myfoo->bar = 2; #error -- $myfoo->bar is not an lvalue
Does that example make sense?
--
Buddha Buck [EMAIL PROTECTED]
"Just as the strength of the Internet is chaos, so the strength of our
liberty depends upon the chaos and cacophony of the unfettered speech
the First Amendment protects." -- A.L.A. v. U.S. Dept. of Justice