Re: Bind Module for Perl

Jens Luedicke <jens luedicke gmail com> writes:

$button->signal_connect(clicked => bind(\*MyWindow::callback, $self,
_1, _2, _3));

One thing something like that might do more than closures would be to
weaken the "$self" target held onto so as to avoid circular references.
Weakening ends up being fairly annoying and easy to get wrong - yet
desirable to make an object you forget about get destroyed.

I've ended up with refs to weakened scalars in userdata like

    my $weak = myself;
    Scalar::Util::weaken ($weak);
    $button->signal_connect(clicked => \&myfunc, \$weak);

    sub myfunc {
      my ($button, $ref_weak) = @_;
      my myself = $$ref_weak || return;

but there'd have to be an easier way.  If the underlying scalar which
signal_connect() creates was made available maybe it could be weakened,
or an option on signal_connect() to ask for that, or something.

calls &MyWindow::callback($self, ...)

The bit in the pod about $object->method won't be quite right if
"method" is inherited.  I suppose the callback func arg could be
designed as either a subr or a string method name.  For a method name it
might be more natural to have the object arg first though.

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