I'm currently working on a module which uses Tkx. I came across something tricky involving appending bindings. It took a while to work out and, in the process, I ran a lot of tests: the results were quite surprising. I've posted this here in case anyone else finds themselves in a similar position: hopefully, it might save them some time and effort.

Tkx is a thin wrapper around Tcl. Its documentation is minimal: it just links to the Tcl documentation and leaves you to work out how to use it. In this instance, I was looking at the bind command documentation for information on appending a binding. I've linked to all of it, here's the relevant parts for this specific post:

NAME

bind — Arrange for X events to invoke Tcl scripts

SYNOPSIS

bind tag ?sequence? ?+??script?

INTRODUCTION

... If script is prefixed with a "+", then it is appended to any existing binding for sequence; ...

That's all it says about appending bindings. I investigated this; ran some tests; and was somewhat surprised at the outcome. The module I'm currently working on now contains this documentation:

Appending Bindings

When appending bindings, using the ?+??script? format, the plus (+) isn't a separate argument. Any of the following syntax variations are valid (subname works for normal named subroutines as well as lexical subroutines).

See Update below.

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

The surprising part was all the different ways of concatenating a string ('+') with an anonymous coderef, a named coderef, and an anonymous arrayref, without the code blowing up in my face.

This may also be useful to those using related modules, like Tcl::Tk and Tcl::pTk; although, I could be completely wrong on that (I have little knowledge of these beyond knowing of their existence).

Update: Despite successfully running two dozen or so tests on all those syntax formats, none of them appear to be actually functional. My apologies to anyone who's been trying to get them to work.

I've spent a bit of time looking into this. I can append one binding using either

... '+' . Tkx::i::interp->create_tcl_sub( CODEREF ) ...

or

my $interp = Tkx::i::interp(); ... '+' . $interp->create_tcl_sub( CODEREF ) ...

And that works for CODEREF as any of these three:

sub { ... } \&subname $coderef

However, whenever I attempt to append a second (or third) binding, none of the appended bindings work; although, the original binding works as expected. The only feedback I get looks like the following (there's no line numbers or other useful information):

Error: invalid command name "::perl::CODE(0x7ffef5dd34d8)"

So again, my apologies to anyone who rushed off to try what I originally posted. I will spend some more time on this: I'll let you know if that proves fruitful.

— Ken


In reply to 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.