1Catalyst::Component::CoUnsteerxtCColnotsruirbeu(t3e)d PeCraltaDloycsutm:e:nCtoamtpioonnent::ContextClosure(3)
2
3
4
6 Catalyst::Component::ContextClosure - Moose Role for components which
7 need to close over the $ctx, without leaking
8
10 package MyApp::Controller::Foo;
11 use Moose;
12 use namespace::clean -except => 'meta';
13 BEGIN {
14 extends 'Catalyst::Controller';
15 with 'Catalyst::Component::ContextClosure';
16 }
17
18 sub some_action : Local {
19 my ($self, $ctx) = @_;
20 $ctx->stash(a_closure => $self->make_context_closure(sub {
21 my ($ctx) = @_;
22 $ctx->response->body('body set from closure');
23 }, $ctx));
24 }
25
27 A common problem with stashing a closure, that closes over the Catalyst
28 context (often called $ctx or $c), is the circular reference it
29 creates, as the closure holds onto a reference to context, and the
30 context holds a reference to the closure in its stash. This creates a
31 memory leak, unless you always carefully weaken the closures context
32 reference.
33
34 This role provides a convenience method to create closures, that closes
35 over $ctx.
36
38 make_context_closure ($closure, $ctx)
39 Returns a code reference, that will invoke $closure with a weakened
40 reference to $ctx. All other parameters to the returned code reference
41 will be passed along to $closure.
42
44 Catalyst::Component
45
46 Catalyst::Controller
47
48 CatalystX::LeakChecker
49
51 This library is free software. You can redistribute it and/or modify it
52 under the same terms as Perl itself.
53
54
55
56perl v5.34.0 2021-07C-a2t2alyst::Component::ContextClosure(3)