callbacks(3pm)
π callbacks β Tk::callbacks π
okay bestie, gather round π«Ά this is the Tk::callbacks page from the User Contributed Perl Documentation section (section 3pm, for the record) and it's literally all about how you tell Tk what code to run when stuff happens. no cap. β¨
πΈ NAME πΈ
Tk::callbacks β Specifying code for Tk to call. π
β¨ SYNOPSIS β¨
so there's basically two moods here, and you can specify a callback in one of the following ways:
no arguments, just vibes β without arguments:
... => \&subname, ...
... => sub { ... }, ...
... => 'methodname', ...
or with arguments, bestie:
... => [ \&subname, args ... ], ...
... => [ sub { ... }, args... ], ...
... => [ 'methodname', args... ], ...
π¦ DESCRIPTION π¦
okay so here's the tea β β Perl/Tk has a callback, where Tcl/Tk has a command string (i.e. a fragment of Tcl to be executed). A perl/Tk callback can take one of the following basic forms:
- Reference to a subroutine
\&subnameπ - Anonymous subroutine (closure)
sub { ... }π¦ - A method name
'methodname'π
Any of these can be provided with arguments by enclosing them and the arguments in []. Here are some examples:
$mw->bind($class, "<Delete>" => 'Delete');
This will call $widget->Delete, the $widget being provided (by bind) as the one where the Delete key was pressed. π
While having bind provide a widget object for you is ideal in many cases it can be irritating in others π. Using the list form this behaviour can be modified:
$a->bind("<Delete>",[$b => 'Delete']);
because the first element $b is an object, bind will call $b->Delete. π«Ά
Note that method/object ordering only matters for bind callbacks, the auto-quoting in perl5.001 makes the first of these a little more readable:
$w->configure(-yscrollcommand => [ set => $ysb]);
$w->configure(-yscrollcommand => [ $ysb => 'set' ]);
but both will call $ysb->set(args provided by Tk) π
Another use of arguments allows you to write generalized methods which are easier to re-use:
$a->bind("<Next>",['Next','Page']);
$a->bind("<Down>",['Next','Line']);
This will call $a->Next('Page') or $a->Next('Line') respectively. β¨
Note that the contents of the"[]"are evaluated by perl when the callback is created. It is often desirable for the arguments provided to the callback to depend on the details of the event which caused it to be executed. π To allow for this callbacks can be nested using theEv(...)"constructor".Ev(...)inserts callback objects into the argument list. When perl/Tk glue code is preparing the argument list for the callback it is about to call it spots these special objects and recursively applies the callback process to them. π§
π₯ EXAMPLES π₯
$entry->bind('<Return>' => [$w , 'validate', Ev(['get'])]);
$toplevel->bind('all', '<Visibility>', [\&unobscure, Ev('s')]);
$mw->bind($class, '<Down>', ['SetCursor', Ev('UpDownLine',1)]);
π SEE ALSO π
Tk::bind Tk::after Tk::options Tk::fileevent π¦
β KEYWORDS β
callback, closure, anonymous subroutine, bind π
and that's the whole page bestie, slay, periodt ππ β this one shipped as perl v5.40.1 dated 2025-04-13, and yeah, it's literally titled callbacks(3pm) iykyk ππ«Ά
π¦ where this page comes from
- debian trixie: perl-tk 1:804.036+dfsg1-5
- ubuntu questing: perl-tk 1:804.036+dfsg1-5
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms