File Coverage

blib/lib/POE/Future.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2014 -- leonerd@leonerd.org.uk
5              
6             package POE::Future;
7              
8 1     1   834 use strict;
  1         3  
  1         36  
9 1     1   6 use warnings;
  1         2  
  1         47  
10              
11             our $VERSION = '0.03';
12              
13 1     1   17 use Carp;
  1         2  
  1         77  
14              
15 1     1   6 use base qw( Future );
  1         2  
  1         1066  
16             Future->VERSION( '0.05' ); # to respect subclassing
17              
18 1     1   14521 use POE;
  0            
  0            
19              
20             =head1 NAME
21              
22             C - use L with L
23              
24             =head1 SYNOPSIS
25              
26             use POE::Future;
27              
28             my $future = POE::Future->new_delay( 10 )
29             ->then_done( "Hello, world!" );
30              
31             say $future->get;
32              
33             =head1 DESCRIPTION
34              
35             This subclass of L integrates with L, allowing the C
36             method to block until the future is ready. It allows C-using code to be
37             written that returns C instances, so that it can make full use of
38             C's abilities, including L, and also that modules using
39             it can provide a C-based asynchronous interface of their own.
40              
41             For a full description on how to use Futures, see the L documentation.
42              
43             =cut
44              
45             =head1 CONSTRUCTORS
46              
47             =cut
48              
49             =head2 $f = POE::Future->new
50              
51             Returns a new leaf future instance, which will allow waiting for its result to
52             be made available, using the C method.
53              
54             =cut
55              
56             =head2 $f = POE::Future->new_delay( $after )
57              
58             Returns a new leaf future instance which will become ready (with an empty
59             result) after the specified delay time.
60              
61             =cut
62              
63             sub new_delay
64             {
65             my $self = shift->new;
66             my ( $after ) = @_;
67              
68             $self->{session} = POE::Session->create(
69             inline_states => {
70             _start => sub { $_[KERNEL]->delay( done => $after ) },
71             cancel => sub { $_[KERNEL]->delay( done => ) },
72             done => sub { $self->done },
73             },
74             );
75              
76             $self->on_cancel( sub {
77             my ( $self ) = @_;
78             POE::Kernel->post( $self->{session}, cancel => );
79             });
80              
81             return $self;
82             }
83              
84             =head2 $f = POE::Future->new_alarm( $at )
85              
86             Returns a new leaf future instance which will become ready (with an empty
87             result) at the specified alarm time.
88              
89             =cut
90              
91             sub new_alarm
92             {
93             my $self = shift->new;
94             my ( $at ) = @_;
95              
96             $self->{session} = POE::Session->create(
97             inline_states => {
98             _start => sub { $_[KERNEL]->alarm( done => $at ) },
99             cancel => sub { $_[KERNEL]->alarm( done => ) },
100             done => sub { $self->done },
101             },
102             );
103              
104             $self->on_cancel( sub {
105             my ( $self ) = @_;
106             POE::Kernel->post( $self->{session}, cancel => );
107             });
108              
109             return $self;
110             }
111              
112             =pod
113              
114             To create a delay or alarm timer that will fail instead of succeed, us the
115             C method:
116              
117             my $f = POE::Future->new_delay( 20 )
118             ->then_fail( "Timeout" );
119              
120             =cut
121              
122             sub await
123             {
124             my $self = shift;
125             POE::Kernel::run_one_timeslice until $self->is_ready;
126             }
127              
128             =head1 AUTHOR
129              
130             Paul Evans
131              
132             =cut
133              
134             0x55AA;