.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "callbacks 3" .TH callbacks 3 2023-07-25 "perl v5.38.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Tk::callbacks \- Specifying code for Tk to call. .SH SYNOPSIS .IX Header "SYNOPSIS" One can specify a callback in one of the following ways: .PP Without arguments: .PP .Vb 3 \& ... => \e&subname, ... \& ... => sub { ... }, ... \& ... => \*(Aqmethodname\*(Aq, ... .Ve .PP or with arguments: .PP .Vb 3 \& ... => [ \e&subname, args ... ], ... \& ... => [ sub { ... }, args... ], ... \& ... => [ \*(Aqmethodname\*(Aq, args... ], ... .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" 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: .IP \(bu 4 Reference to a subroutine \f(CW\*(C`\e&subname\*(C'\fR .IP \(bu 4 Anonymous subroutine (closure) \f(CW\*(C`sub { ... }\*(C'\fR .IP \(bu 4 A method name \f(CW\*(Aqmethodname\*(Aq\fR .PP Any of these can be provided with arguments by enclosing them and the arguments in \fB[]\fR. Here are some examples: .PP \&\fR\f(CI$mw\fR\fI\fR\->\fBbind\fR(\fI\fR\f(CI$class\fR\fI,\fR \fB"" => 'Delete'\fR); .PP This will call \fR\f(CI$widget\fR\fI\fR\->\fBDelete\fR, the \fI\fR\f(CI$widget\fR\fI\fR being provided (by bind) as the one where the Delete key was pressed. .PP 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: .PP \&\fR\f(CI$a\fR\fI\fR\->\fBbind\fR(\fB""\fR,[\fI\fR\f(CI$b\fR\fI\fR => 'Delete']); .PP because the first element \fR\f(CI$b\fR\fI\fR is an object bind will call \fI\fR\f(CI$b\fR\fI\fR\->\fBDelete\fR. .PP Note that method/object ordering only matters for \f(CW\*(C`bind\*(C'\fR callbacks, the auto-quoting in perl5.001 makes the first of these a little more readable: .PP .Vb 2 \& $w\->configure(\-yscrollcommand => [ set => $ysb]); \& $w\->configure(\-yscrollcommand => [ $ysb => \*(Aqset\*(Aq ]); .Ve .PP but both will call \f(CW$ysb\fR\->set(args provided by Tk) .PP Another use of arguments allows you to write generalized methods which are easier to re-use: .PP .Vb 2 \& $a\->bind("",[\*(AqNext\*(Aq,\*(AqPage\*(Aq]); \& $a\->bind("",[\*(AqNext\*(Aq,\*(AqLine\*(Aq]); .Ve .PP This will call \f(CW$a\fR\->\fINext\fR('Page') or \f(CW$a\fR\->\fINext\fR('Line') respectively. .PP Note that the contents of the \f(CW\*(C`[]\*(C'\fR 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 the \&\f(CWEv(...)\fR "constructor". \&\f(CWEv(...)\fR 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. .SH EXAMPLES .IX Header "EXAMPLES" .Vb 1 \& $entry\->bind(\*(Aq\*(Aq => [$w , \*(Aqvalidate\*(Aq, Ev([\*(Aqget\*(Aq])]); \& \& $toplevel\->bind(\*(Aqall\*(Aq, \*(Aq\*(Aq, [\e&unobscure, Ev(\*(Aqs\*(Aq)]); \& \& $mw\->bind($class, \*(Aq\*(Aq, [\*(AqSetCursor\*(Aq, Ev(\*(AqUpDownLine\*(Aq,1)]); .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Tk::bind Tk::after Tk::options Tk::fileevent .SH KEYWORDS .IX Header "KEYWORDS" callback, closure, anonymous subroutine, bind