On Sun, 20 Aug 2000 16:32:16 +1000 (EST), Damian Conway said:
>    > I think we agree.
>     > 
>     > $foo is a tied variable
>     > $bar is a normal variable
>     > 
>     >         sub zap : lvalue { return $_[0] ? $foo : $bar }
>     > 
>     >         zap(1) = 5;     # Goes to tied variable
>     >         zap(0) = 5;     # Normal store
>  
>  And it would also be good to be able to overload operator = for objects,
>  so that assigment to then could be intercepted without having to tie the
>  reference as well.

So if all I want to do is make sure that certain attributes are positive
integers, I have to do this:

sub new{
  my $class=shift;
  my $self=bless {
    # This is an overloaded object which only allows positive int assignments
    x => PositiveOnly->new,
  },$class;
}

sub x: lvalue{
  my $self=shift;
  $self->{x};
}

It looks like I need a whole new class for each different behaviour. I can
live with that. However, I also need an instance of that class for each
attribute with the behaviour. That seems like unnecessary overhead. You could
probably get round that:

sub new{
  my $class=shift;
  my $self=bless {},$class;
}

sub x:lvalue{
  my $self=shift;
  # This creates a temporary overloaded object
  PositiveOnly->new(\$self->{x});
}

With this, there are less objects kicking around at any given moment, but
depending on the application, this will see far more objects being created
and destroyed as attributes are accesssed.

I'd much prefer a solution where the positive-only logic was in a method
belonging to the class, rather than being attached to afflicted attributes.
The only ways I can see of doing this are like Gnat or Andy's RFCs.

Reply via email to