line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
58701
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
2
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
19
|
|
3
|
1
|
|
|
1
|
|
10
|
use v5.10; |
|
1
|
|
|
|
|
3
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Async::ContextSwitcher; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
4
|
use base "Exporter::Tiny"; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
424
|
|
10
|
|
|
|
|
|
|
our @EXPORT = qw(context cb_w_context); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Async::ContextSwitcher - helps track execution context in async programs |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 DESCRIPTION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
This is a very simple module that helps you carry around execution context |
19
|
|
|
|
|
|
|
in async programs. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Idea is simple: |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=over 4 |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=item * you create a L context for an entry point |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
It can be a new web request, a new message from a queue to process |
28
|
|
|
|
|
|
|
or command line script command |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item * use L to create all callbacks in your application |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=item * correct context restored when your callbacks are called |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=item * use L to access data |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=back |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our $CTX; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 METHODS and FUNCTIONS |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 new |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Creates a new context and makes it the current one. Takes named pairs and stores |
48
|
|
|
|
|
|
|
them in the context. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Async::ContextSwitcher->new( request => $psgi_env ); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub new { |
55
|
10
|
|
|
10
|
1
|
4922
|
my $self = shift; |
56
|
|
|
|
|
|
|
|
57
|
10
|
|
33
|
|
|
37
|
return $CTX = bless {@_}, ref( $self ) || $self; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 context |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Returns the current context. Function is exported. Always returns context. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $ct = context->{request}{HTTP_CONTENT_TYPE}; |
65
|
|
|
|
|
|
|
context->{user} = $user; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub context() { |
70
|
10
|
50
|
|
10
|
1
|
86
|
return $CTX if $CTX; |
71
|
0
|
|
|
|
|
0
|
return $CTX = __PACKAGE__->new; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 cb_w_context |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Wrapper for callbacks. Function is exported. Wraps a callback with code |
77
|
|
|
|
|
|
|
that stores and restores context to make sure correct context travels |
78
|
|
|
|
|
|
|
with your code. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
async_call( callback => cb_w_context { context->{good} = shift } ); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Make sure that all callbacks in your code are created with this function |
83
|
|
|
|
|
|
|
or you can loose track of your context. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub cb_w_context(&) { |
88
|
20
|
|
|
20
|
1
|
149
|
my $cb = $_[0]; |
89
|
20
|
|
|
|
|
25
|
my $ctx = $CTX; |
90
|
|
|
|
|
|
|
return sub { |
91
|
20
|
|
|
20
|
|
83562
|
$CTX = $ctx; |
92
|
20
|
|
|
|
|
85
|
goto &$cb; |
93
|
20
|
|
|
|
|
59
|
}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 AUTHOR |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Ruslan Zakirov ERuslan.Zakirov@gmail.comE |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 LICENSE |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Under the same terms as perl itself. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |