blib/lib/Async/ContextSwitcher.pm

filter: Statementstmt 95.8% 23 / 24 Branchbran 50.0% 1 / 2 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 85.7% 36 / 42 CC 3 SCAR 4.4
CC
3
Cov
86%
context
2.5
new
2.3
BEGIN
1.0
CRAP 1.6
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Async::ContextSwitcher;
6  
7 1 our $VERSION = '0.02';
8  
9 1 ▶use base "Exporter::Tiny";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 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</new> 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</cb_w_context> to create all callbacks in your application
31  
32 =item * correct context restored when your callbacks are called
33  
34 =item * use L</context> to access data
35  
36 =back
37  
38 =cut
39  
40  
41 1 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 10 ▶sub new {
Subroutine new: called CC=2 SCAR=8.1
Pod documented
55 10 my $self = shift;
56  
57 10 ▶ return $CTX = bless {@_}, ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
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 10 ▶sub context() {
Subroutine context: called CC=2 SCAR=9.2
Pod documented
70 10 ▶ return $CTX if $CTX;
Branch: 1 branch
BranchTrueFalseTotal
if $Async::ContextSwitcher::CTX 10 0 10
71 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 20 ▶sub cb_w_context(&) {
Subroutine cb_w_context: called CC=1 SCAR=0.0
Pod documented
88 20 my $cb = $_[0];
89 20 my $ctx = $CTX;
90 20 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 20 $CTX = $ctx;
92 20 goto &$cb;
93 20 };
94 }
95  
96  
97 =head1 AUTHOR
98  
99 Ruslan Zakirov E<lt>Ruslan.Zakirov@gmail.comE<gt>
100  
101 =head1 LICENSE
102  
103 Under the same terms as perl itself.
104  
105 =cut
106  
107 1 1;
↑ Summary