home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_b2692c616fa7ee8ad770c42a76e5e2ee
< prev
next >
Wrap
Text File
|
2000-03-23
|
5KB
|
129 lines
<HTML>
<HEAD>
<TITLE>Tk::callbacks - Specifying code for Tk to call.</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Tk::callbacks - Specifying code for Tk to call.</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<LI><A HREF="#examples">EXAMPLES</A></LI>
<LI><A HREF="#see also">SEE ALSO</A></LI>
<LI><A HREF="#keywords">KEYWORDS</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>Tk::callbacks - Specifying code for Tk to call.</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<P>One can specify a callback in one of the following ways:</P>
<P>Without arguments:</P>
<PRE>
... => \&subname, ...
... => sub { ... }, ...
... => 'methodname', ...</PRE>
<P>or with arguments:</P>
<PRE>
... => [ \&subname ?, args ...? ], ...
... => [ sub { ... } ?, args...? ], ...
... => [ 'methodname' ?, args...?], ...</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>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:</P>
<UL>
<LI><STRONG><A NAME="item_Reference_to_a_subroutine_%5C%26subname">Reference to a subroutine <CODE>\&subname</CODE></A></STRONG><BR>
<LI><STRONG><A NAME="item_subroutine">Anonymous subroutine (closure) <A HREF="../../../lib/Pod/perlfunc.html#item_sub"><CODE>sub { ... }</CODE></A></A></STRONG><BR>
<LI><STRONG><A NAME="item_A_method_name_%27methodname%27">A method name <CODE>'methodname'</CODE></A></STRONG><BR>
</UL>
<P>Any of these can be provided with arguments by enclosing them and the
arguments in <STRONG>[]</STRONG>. Here are some examples:</P>
<P><EM>$mw</EM>-><STRONG>bind</STRONG>(<EM>$class,</EM> <STRONG>``<Delete>'' => 'Delete'</STRONG>);</P>
<P>This will call <EM>$widget</EM>-><STRONG>Delete</STRONG>, the <EM>$widget</EM> being provided (by bind) as
the one where the Delete key was pressed.</P>
<P>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:</P>
<P><EM>$a</EM>-><STRONG>bind</STRONG>(<STRONG>``<Delete>''</STRONG>,[<EM>$b</EM> => 'Delete']);</P>
<P>because the first element <EM>$b</EM> is an object bind
will call <EM>$b</EM>-><STRONG>Delete</STRONG>.</P>
<P>Note that method/object ordering only matters for <A HREF="../../../lib/Pod/perlfunc.html#item_bind"><CODE>bind</CODE></A> callbacks,
the auto-quoting in perl5.001 makes the first of these a little more readable:</P>
<P>$w->configure(-yscrollcommand => [ set => $ysb]);</P>
<P>$w->configure(-yscrollcommand => [ $ysb => 'set' ]);</P>
<P>but both will call <CODE>$ysb</CODE>->set(args provided by Tk)</P>
<P>Another use of arguments allows you to write generalized methods which are
easier to re-use:</P>
<P>$a->bind(``<Next>'',['Next','Page']);</P>
<P>$a->bind(``<Down>'',['Next','Line']);</P>
<P>This will call <CODE>$a</CODE>-><EM>Next</EM>('Page') or <CODE>$a</CODE>-><EM>Next</EM>('Line') respectively.</P>
<P>Note that the contents of the <CODE>[]</CODE> 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
<CODE>Ev(...)</CODE> ``constructor''.
<CODE>Ev(...)</CODE> 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.</P>
<P>
<HR>
<H1><A NAME="examples">EXAMPLES</A></H1>
<PRE>
$entry->bind('<Return>' => [$w , 'validate', Ev(['get'])]);</PRE>
<PRE>
$toplevel->bind('all', '<Visibility>', [\&unobscure, Ev('s')]);</PRE>
<PRE>
$mw->bind($class, '<Down>', ['SetCursor', Ev('UpDownLine',1)]);</PRE>
<P>
<HR>
<H1><A NAME="see also">SEE ALSO</A></H1>
<P><A HREF="../../../site/lib/Tk/bind.html">Tk::bind</A>
<A HREF="../../../Tk/after.html">Tk::after</A>
<A HREF="../../../site/lib/Tk/options.html">Tk::options</A>
<A HREF="../../../site/lib/Tk/fileevent.html">Tk::fileevent</A></P>
<P>
<HR>
<H1><A NAME="keywords">KEYWORDS</A></H1>
<P>callback, closure, anonymous subroutine, bind</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Tk::callbacks - Specifying code for Tk to call.</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>