Re: A little tie magic



* Bjarne Steinsbø <bosteins broadpark no> [2003-12-07 18:37]:
1) Is there enough interest in this that the powers that be
should be requested to include it in the core Gtk2
distribution?
2) Is this the way the interface should work?  Any better
suggestions?
3) Would it be OK to document Gtk2::BindVariable as such, even
though at least one additional method will be compiled into
each and every Gtk2 'client' class?

I don't see why this has to be a Gtk2-specific module. Instead of
having all the widgets support ties, you should implement a
change-notifying scalar using the tie mechnism along these lines: 

  package Tie::NotifyScalar;
  require Tie::Scalar;
  our @ISA = qw(Tie::Scalar);
  
  sub TIESCALAR {
    my $pkg = shift;
    my ($updated) = @_;
    bless {
      data => \(my $data),
      callback => $updated,
    }, $pkg;
  }

  sub STORE {
    my $self = shift;
    my ($value) = @_;
    $self->callback($self->{data}, $value);
    $self->{data} = $value;
  }

  sub FETCH {
    my $self = shift;
    # unexpected aliasing effects possible if return is not a true copy
    return my $data = $self->{data};
  }
  
Now you have a scalar that runs a callback whenever it is
updated. In your app you then do something like
  
  tie my $button_label, 'Tie::NotifyScalar', sub { $button->set_label($_[1]) };

Now whenever you change $button_label, the button label will
change as well. No need to add tons of stuff to Gtk2.

Maybe I should add proper sanity checks to this code and a bunch
of tests and up it to CPAN?

-- 
Regards,
Aristotle
 
"If you can't laugh at yourself, you don't take life seriously enough."



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]