Debian Codegraph @

pod/callbacks.pod

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