1callbacks(3) User Contributed Perl Documentation callbacks(3)
2
3
4
6 Tk::callbacks - Specifying code for Tk to call.
7
9 One can specify a callback in one of the following ways:
10
11 Without arguments:
12
13 ... => \&subname, ...
14 ... => sub { ... }, ...
15 ... => 'methodname', ...
16
17 or with arguments:
18
19 ... => [ \&subname, args ... ], ...
20 ... => [ sub { ... }, args... ], ...
21 ... => [ 'methodname', args... ], ...
22
24 Perl/Tk has a callback, where Tcl/Tk has a command string (i.e. a
25 fragment of Tcl to be executed). A perl/Tk callback can take one of
26 the following basic forms:
27
28 · Reference to a subroutine "\&subname"
29
30 · Anonymous subroutine (closure) "sub { ... }"
31
32 · A method name 'methodname'
33
34 Any of these can be provided with arguments by enclosing them and the
35 arguments in []. Here are some examples:
36
37 $mw->bind($class, "<Delete>" => 'Delete');
38
39 This will call $widget->Delete, the $widget being provided (by bind) as
40 the one where the Delete key was pressed.
41
42 While having bind provide a widget object for you is ideal in many
43 cases it can be irritating in others. Using the list form this
44 behaviour can be modified:
45
46 $a->bind("<Delete>",[$b => 'Delete']);
47
48 because the first element $b is an object bind will call $b->Delete.
49
50 Note that method/object ordering only matters for "bind" callbacks, the
51 auto-quoting in perl5.001 makes the first of these a little more
52 readable:
53
54 $w->configure(-yscrollcommand => [ set => $ysb]);
55 $w->configure(-yscrollcommand => [ $ysb => 'set' ]);
56
57 but both will call $ysb->set(args provided by Tk)
58
59 Another use of arguments allows you to write generalized methods which
60 are easier to re-use:
61
62 $a->bind("<Next>",['Next','Page']);
63 $a->bind("<Down>",['Next','Line']);
64
65 This will call $a->Next('Page') or $a->Next('Line') respectively.
66
67 Note that the contents of the "[]" are evaluated by perl when the
68 callback is created. It is often desirable for the arguments provided
69 to the callback to depend on the details of the event which caused it
70 to be executed. To allow for this callbacks can be nested using the
71 "Ev(...)" "constructor". "Ev(...)" inserts callback objects into the
72 argument list. When perl/Tk glue code is preparing the argument list
73 for the callback it is about to call it spots these special objects and
74 recursively applies the callback process to them.
75
77 $entry->bind('<Return>' => [$w , 'validate', Ev(['get'])]);
78
79 $toplevel->bind('all', '<Visibility>', [\&unobscure, Ev('s')]);
80
81 $mw->bind($class, '<Down>', ['SetCursor', Ev('UpDownLine',1)]);
82
84 Tk::bind Tk::after Tk::options Tk::fileevent
85
87 callback, closure, anonymous subroutine, bind
88
89
90
91perl v5.30.1 2020-01-30 callbacks(3)