File Coverage

blib/lib/Catalyst/Component/ContextClosure.pm
Criterion Covered Total %
statement 9 13 69.2
branch n/a
condition n/a
subroutine 3 5 60.0
pod 1 1 100.0
total 13 19 68.4


line stmt bran cond sub pod time code
1              
2             use Moose::Role;
3 80     80   928102 use Scalar::Util 'weaken';
  80         235  
  80         776  
4 80     80   366145 use namespace::clean -except => [ 'meta' ];
  80         240  
  80         5553  
5 80     80   613  
  80         224  
  80         1027  
6             my ($self, $closure, $ctx) = @_;
7             weaken $ctx;
8 0     0 1   return sub { $closure->($ctx, @_) };
9 0           }
10 0     0      
  0            
11             1;
12              
13              
14             =head1 NAME
15              
16             Catalyst::Component::ContextClosure - Moose Role for components which need to close over the $ctx, without leaking
17              
18             =head1 SYNOPSIS
19              
20             package MyApp::Controller::Foo;
21             use Moose;
22             use namespace::clean -except => 'meta';
23             BEGIN {
24             extends 'Catalyst::Controller';
25             with 'Catalyst::Component::ContextClosure';
26             }
27              
28             sub some_action : Local {
29             my ($self, $ctx) = @_;
30             $ctx->stash(a_closure => $self->make_context_closure(sub {
31             my ($ctx) = @_;
32             $ctx->response->body('body set from closure');
33             }, $ctx));
34             }
35              
36             =head1 DESCRIPTION
37              
38             A common problem with stashing a closure, that closes over the Catalyst context
39             (often called C<$ctx> or C<$c>), is the circular reference it creates, as the
40             closure holds onto a reference to context, and the context holds a reference to
41             the closure in its stash. This creates a memory leak, unless you always
42             carefully weaken the closures context reference.
43              
44             This role provides a convenience method to create closures, that closes over
45             C<$ctx>.
46              
47             =head1 METHODS
48              
49             =head2 make_context_closure ($closure, $ctx)
50              
51             Returns a code reference, that will invoke C<$closure> with a weakened
52             reference to C<$ctx>. All other parameters to the returned code reference will
53             be passed along to C<$closure>.
54              
55             =head1 SEE ALSO
56              
57             L<Catalyst::Component>
58              
59             L<Catalyst::Controller>
60              
61             L<CatalystX::LeakChecker>
62              
63             =begin stopwords
64              
65             =head1 AUTHOR
66              
67             Florian Ragwitz <rafl@debian.org>
68              
69             =end stopwords
70              
71             =head1 COPYRIGHT
72              
73             This library is free software. You can redistribute it and/or modify it under
74             the same terms as Perl itself.
75              
76             =cut