"I will spend some more time on this: I'll let you know if that proves fruitful."

After some delving into the source code for Tkx.pm and Tcl.pm, I've now got this working pretty much the way I wanted. Here's an extract of the module code:

... use 5.026; ... use constant { ... ERR_CALLBACK_NOT_CODEREF => 'Callback is not a coderef.', }; ... use Scalar::Util 'reftype'; ... use Tkx; ... { use constant BINDPLUS_REF_BASE => 'bindplus_ref_base_'; my ($tcl_interp, $tcl_sub_ref_id); BEGIN { $tcl_interp = Tkx::i::interp(); $tcl_sub_ref_id = 0; } sub bindplus { my ($tag, $seq, $callback) = @_; my $callback_ref = reftype $callback; if ($callback_ref eq 'CODE') { Tkx::bind($tag, $seq, '+' . $tcl_interp->create_tcl_sub( $callback, undef, undef, BINDPLUS_REF_BASE . $tcl_sub_ +ref_id++ )); } elsif ($callback_ref eq 'ARRAY') { if (reftype $callback->[0] eq 'CODE') { Tkx::bind($tag, $seq, '+' . $tcl_interp->create_tcl_su +b( sub { $callback->[0]->($callback->@[1 .. $callback +->$#*]) }, undef, undef, BINDPLUS_REF_BASE . $tcl_sub_ref_id+ ++ )); } else { croak ERR_CALLBACK_NOT_CODEREF; } } else { croak ERR_CALLBACK_NOT_CODEREF; } } }

The trick to appending multiple times, was to use a unique identifier for the fourth create_tcl_sub() argument:

$callback, undef, undef, BINDPLUS_REF_BASE . $tcl_sub_ref_id++

In my tests, I created an initial binding in the normal way (with Tkx::bind()); I then appended a further eleven bindings using the function above. That was nine tests (one for each of the callback forms shown below) plus two that were somewhat more involved (using tk_messageBox). All were called in the correct order.

This works for callbacks in any of these forms:

sub { ... } \&subname $coderef [sub { ... }] [sub { ... }, @args] [\&subname] [\&subname, @args] [$coderef] [$coderef, @args]

One thing this code doesn't do is handle Tkx::Ev() calls in the @args. That's not a current requirement for me: perhaps I'll look into it at a later date.

— Ken


In reply to Re: Tkx - bind - append binding [Working code] by kcott
in thread Tkx - bind - append binding by kcott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.