in reply to Re^5: Tim O'Reilly on Perl
in thread Tim O'Reilly on Perl

Is there something wrong with Perl 5's lvalue attribute? As far as I can tell, it produces rather functional lvalue accessors:

package Foo; sub foo : lvalue { my $self = shift; warn "Only first argument to method foo is used" if @_ > 1; $self->{ foo } = shift if @_; $self->{ foo }; } my $x = bless +{}, 'Foo'; $x->foo = 3; print $x->foo, "\n"; $x->foo++; print $x->foo, "\n"; $x->foo += 6; print $x->foo, "\n"; __END__ 3 4 10
I know that user-defined functions with the lvalue attribute, in general, will give wrong results in the debugger, but that's only a problem for those weenies who use the debugger ;-) (Plus, there are good ways around this.)

the lowliest monk

Replies are listed 'Best First'.
Re^7: Tim O'Reilly on Perl
by holli (Abbot) on Jul 17, 2005 at 22:41 UTC
    Yeah. The problem with them is one can't check the value before it is set. I would like to have something like
    sub foo : get { my $self = shift; return $self->{_foo}; } sub foo : set { my $self = shift; my $value = shift; die "foo is a integer field!\n" unless $value =~ /[+-]?[0-9]+/; $self->{_foo} = $value; }


    holli, /regexed monk/
      The problem with them is one can't check the value before it is set.

      Oh, but you can. I addressed that in To Validate Data In Lvalue Subs. The Constrained package from that node turned out to have lots of other uses, too. It's now called Tie::Constrained on CPAN.

      After Compline,
      Zaxo