home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Callback.pm < prev    next >
Encoding:
Perl POD Document  |  2006-02-19  |  3.7 KB  |  143 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2005 Daniel P. Berrange
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. #
  19. # $Id: Callback.pm,v 1.7 2006/02/02 16:58:26 dan Exp $
  20.  
  21. =pod
  22.  
  23. =head1 NAME
  24.  
  25. Net::DBus::Callback - a callback for receiving reactor events
  26.  
  27. =head1 SYNOPSIS
  28.  
  29.   use Net::DBus::Callback;
  30.  
  31.   # Assume we have a 'terminal' object and its got a method
  32.   # to be invoked everytime there is input on its terminal.
  33.   #
  34.   # To create a callback to invoke this method one might use
  35.   my $cb = Net::DBus::Callback->new(object => $terminal,
  36.                                     method => "handle_stdio");
  37.  
  38.  
  39.   # Whatever is monitoring the stdio channel, would then
  40.   # invoke the callback, perhaps passing in a parameter with
  41.   # some 'interesting' data, such as number of bytes available
  42.   $cb->invoke($nbytes)
  43.  
  44.   #... which results in a call to
  45.   #  $terminal->handle_stdio($nbytes)
  46.  
  47. =head1 DESCRIPTION
  48.  
  49. This module provides a simple container for storing details
  50. about a callback to be invoked at a later date. It is used
  51. when registering to receive events from the L<Net::DBus::Reactor>
  52. class
  53.  
  54. =head1 METHODS
  55.  
  56. =over 4
  57.  
  58. =cut
  59.  
  60. package Net::DBus::Callback;
  61.  
  62. use 5.006;
  63. use strict;
  64. use warnings;
  65. use Carp qw(confess);
  66.  
  67. =item my $cb = Net::DBus::Callback->new(method => $name, [args => \@args])
  68.  
  69. Creates a new callback object, for invoking a plain old function. The C<method>
  70. parameter should be the fully qualified function name to invoke, including the
  71. package name. The optional C<args> parameter is an array reference of parameters
  72. to be pass to the callback, in addition to those passed into the C<invoke> method.
  73.  
  74. =item my $cb = Net::DBus::Callback->new(object => $object, method => $name, [args => \@args])
  75.  
  76. Creates a new callback object, for invoking a method on an object. The C<method>
  77. parameter should be the name of the method to invoke, while the C<object> parameter
  78. should be a blessed object on which the method will be invoked. The optional C<args> 
  79. parameter is an array reference of parameters to be pass to the callback, in addition 
  80. to those passed into the C<invoke> method.
  81.  
  82. =cut
  83.  
  84. sub new {
  85.     my $proto = shift;
  86.     my $class = ref($proto) || $proto;
  87.     my %params = @_;
  88.     my $self = {};
  89.  
  90.     $self->{object} = $params{object} ? $params{object} : undef;
  91.     $self->{method} = $params{method} ? $params{method} : confess "method parameter is required";
  92.     $self->{args} = $params{args} ? $params{args} : [];
  93.  
  94.     bless $self, $class;
  95.  
  96.     return $self;
  97. }
  98.  
  99. =item $cb->invoke(@args)
  100.  
  101. Invokes the callback. The argument list passed to the callback
  102. is a combination of the arguments supplied in the callback
  103. constructor, followed by the arguments supplied in the C<invoke>
  104. method.
  105.  
  106. =cut
  107.  
  108. sub invoke {
  109.     my $self = shift;
  110.     
  111.     if ($self->{object}) {
  112.     my $obj = $self->{object};
  113.     my $method = $self->{method};
  114.  
  115.     $obj->$method(@{$self->{args}}, @_);
  116.     } else {
  117.     my $method = $self->{method};
  118.  
  119.     &$method(@{$self->{args}}, @_);
  120.     }
  121. }
  122.  
  123. 1;
  124.  
  125. __END__
  126.  
  127. =back
  128.  
  129. =head1 AUTHOR
  130.  
  131. Daniel P. Berrange.
  132.  
  133. =head1 COPYRIGHT
  134.  
  135. Copyright (C) 2004-2006 Daniel P. Berrange
  136.  
  137. =head1 SEE ALSO
  138.  
  139. L<Net::DBus::Reactor>
  140.  
  141. =cut
  142.  
  143.