Cataly...Closure(3pm)

πŸ“¦
version 5.90132 Β· 6 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– Cataly...Closure(3pm) β€” bestie edition πŸ’–

okay so this is the manpage for Catalyst::Component::ContextClosure, aka Cataly...Closure(3pm) if you're squinting at the section-3pm manual listing πŸ’… (the name got truncated in the header, she's shy, it happens).

It's a Moose Role for components that need to close over the $ctx β€” without leaking. No cap.


✨ NAME ✨

Catalyst::Component::ContextClosure β€” a Moose Role for components which need to close over the $ctx, without leaking 🫢


🌸 SYNOPSIS 🌸

package MyApp::Controller::Foo;
use Moose;
use namespace::clean -except => 'meta';
BEGIN {
    extends 'Catalyst::Controller';
    with 'Catalyst::Component::ContextClosure';
}

sub some_action : Local {
    my ($self, $ctx) = @_;
    $ctx->stash(a_closure => $self->make_context_closure(sub {
        my ($ctx) = @_;
        $ctx->response->body('body set from closure');
    }, $ctx));
}

like, that's the whole vibe. you with the role, you call make_context_closure, you stash the thing, you're iconic ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea, bestie πŸ“ β€” there's a super common problem with stashing a closure that closes over the Catalyst context (you know her, she goes by $ctx or $c). The issue? the circular reference it creates 😭 because the closure holds onto a reference to context, and the context holds a reference to the closure in its stash. Two queens holding hands and never letting go. Cute in theory, a memory leak in practice πŸ’€

unless you always carefully weaken the closure's context reference. every single time. no skipping. not even once πŸ™ˆ

This role provides a convenience method to create closures that close over $ctx β€” so you get the hand-holding without the leak. it's giving responsible queen energy πŸ‘‘


πŸ’… METHODS πŸ’…

make_context_closure ($closure, $ctx)

Returns a code reference that will invoke $closure with a weakened reference to $ctx ✨

And the best part? All other parameters passed to the returned code reference get passed along to $closure too. Extra args = fully welcome, no gatekeeping 🫢


πŸ”₯ SEE ALSO πŸ”₯

  • Catalyst::Component
  • Catalyst::Controller
  • CatalystX::LeakChecker

go read these too, they're all part of the lore πŸ“š


This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself. πŸŽ€


and that's the page, babes 🌈 β€” brought to you by perl v5.40.0, dated 2024-11-15, filed under Cataly...Closure(3pm) πŸ’œ go forth and weaken your refs, mwah πŸ’–

πŸ“¦ where this page comes from