Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

tk::callbacks(3pm) [debian man page]

callbacks(3pm)						User Contributed Perl Documentation					    callbacks(3pm)

NAME
Tk::callbacks - Specifying code for Tk to call. SYNOPSIS
One can specify a callback in one of the following ways: Without arguments: ... => &subname, ... ... => sub { ... }, ... ... => 'methodname', ... or with arguments: ... => [ &subname, args ... ], ... ... => [ sub { ... }, args... ], ... ... => [ 'methodname', args... ], ... 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: o Reference to a subroutine "&subname" o Anonymous subroutine (closure) "sub { ... }" o 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 the "Ev(...)" "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 perl v5.14.2 2010-05-29 callbacks(3pm)

Check Out this Related Man Page

README(3pm)						User Contributed Perl Documentation					       README(3pm)

NAME
AnyEvent::Callback - callback aggregator for AnyEvent watchers. SYNOPSIS
use AnyEvent::Callback; # usually watchers are looked as: AE::something @args, sub { ... }; AE::something @args, sub { ... }, # result sub { ... }; # error use AnyEvent::Callback; AE::something @args, CB { ... }; AE::something @args, CB sub { ... }, # result sub { ... }; # error AE::something @args, CB sub { ... }, # result sub { ... }, # error sub { ... }; # anyway callback Callback hierarchy my $cbchild = $cb->CB(sub { ... }); ... $cbchild->error('error'); # will call $cb->error('error'); Inside Your callback You can: sub my_watcher { my $cb = pop; my @args = @_; # ... $cb->error( @error ); # error callback will be called # or: $cb->( $value ); # result callback will be called } Callbacks stack my $cbs = CBS; for (1 .. $n) { AE::something @args, $cbs->cb; } $cbs->wait(sub { for (@_) { if ($_->is_error) { # handle one error my @err = $_->errors; # or: my $errstr = $_->errstr; } else { # results my @res = $_->results; } } }); DESCRIPTION
The module allows You to create callback's hierarchy. Also the module groups error and result callbacks into one object. Also the module checks if one callback was called by watcher or not. If a watcher doesn't call result or error callback, error callback will be called automatically. Also the module checks if a callback was called reentrant. In the case the module will complain (using "carp" in Carp). If a watcher touches error callback and if superior didn't define error callback, the module will call error callback upwards hierarchy. Example: AE::something @args, CB &my_watcher, &on_error; sub on_error { } sub my_watcher { my $cb = pop; ... the_other_watcher $cb->CB( sub { # error callback wasn't defined my $cb = pop; ... yet_another_watcher1 $cb->CB( sub { my $cb = pop; ... $cb->( 123 ); # upwards callback }); yet_another_watcher2 $cb->CB( sub { my $cb = pop; ... $cb->error( 456 ); # on_error will be called }); }); } METHODS
'CODE' (overloaded fake method) $cb->( ... ); You can use the object as usually CODEREF. CB Creates new callback object that have binding on parent callback. my $new_cb = $cb->CB(sub { ... }); # the cb doesn't catch errors my $new_cb = CB(sub { ... }, sub { ... }); # the cb catches errors my $new_cb = $cb->CB(sub { ... }, sub { ... }); # the same error Calls error callback. If the object has no registered error callbacks, parent object's error callback will be called. $cb->error('WTF?'); COPYRIGHT AND LICENCE
Copyright (C) 2012 by Dmitry E. Oboukhov This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-06-30 README(3pm)
Man Page