Catalyst-Runtime

 view release on metacpan or  search on metacpan

lib/Catalyst.pm  view on Meta::CPAN

            $c->response->status(400);
            $c->response->content_type('text/plain');
            $c->response->body('Bad Request');
            $c->finalize;
            die $_;
        }
    };

    $c->log_request;
    $c->{stash} = $c->stash;
    Scalar::Util::weaken($c->{stash});

    return $c;
}

=head2 $c->prepare_action

Prepares action. See L<Catalyst::Dispatcher>.

=cut

lib/Catalyst/Component/ContextClosure.pm  view on Meta::CPAN

package Catalyst::Component::ContextClosure;

use Moose::Role;
use Scalar::Util 'weaken';
use namespace::clean -except => [ 'meta' ];

sub make_context_closure {
    my ($self, $closure, $ctx) = @_;
    weaken $ctx;
    return sub { $closure->($ctx, @_) };
}

1;

__END__

=head1 NAME

Catalyst::Component::ContextClosure - Moose Role for components which need to close over the $ctx, without leaking

lib/Catalyst/Component/ContextClosure.pm  view on Meta::CPAN

            $ctx->response->body('body set from closure');
        }, $ctx));
    }

=head1 DESCRIPTION

A common problem with stashing a closure, that closes over the Catalyst context
(often called C<$ctx> or C<$c>), is the circular reference it creates, as the
closure holds onto a reference to context, and the context holds a reference to
the closure in its stash. This creates a memory leak, unless you always
carefully weaken the closures context reference.

This role provides a convenience method to create closures, that closes over
C<$ctx>.

=head1 METHODS

=head2 make_context_closure ($closure, $ctx)

Returns a code reference, that will invoke C<$closure> with a weakened
reference to C<$ctx>. All other parameters to the returned code reference will
be passed along to C<$closure>.

=head1 SEE ALSO

L<Catalyst::Component>

L<Catalyst::Controller>

L<CatalystX::LeakChecker>

t/lib/TestApp.pm  view on Meta::CPAN

    sub count_leaks {
        my ($ctx) = @_;
        return scalar @{ $ctx->leaks };
    }

    after finalize => sub {
        my ($ctx) = @_;
        my @leaks;

        my $weak_ctx = $ctx;
        Scalar::Util::weaken $weak_ctx;

        Devel::Cycle::find_cycle($ctx, sub {
            my ($path) = @_;
            push @leaks, $path
                if $path->[0]->[2] == $weak_ctx;
        });

        push @{ $ctx->leaks }, @leaks;
    };
}

t/lib/TestApp/View/Dump.pm  view on Meta::CPAN

package TestApp::View::Dump;

use strict;
use base 'Catalyst::View';

use Data::Dumper ();
use Scalar::Util qw(blessed weaken);

sub dump {
    my ( $self, $reference, $purity ) = @_;

    return unless $reference;

    $purity = defined $purity ? $purity : 1;

    my $dumper = Data::Dumper->new( [$reference] );
    $dumper->Indent(1);

t/lib/TestApp/View/Dump.pm  view on Meta::CPAN

    if ( my $output =
        $self->dump( $reference, $purity ) )
    {

        $c->res->headers->content_type('text/plain');
        $c->res->output($output);

        if ($context) {
            # Repair context
            $reference->{_context} = $context;
            weaken( $reference->{_context} );
        }

        if ($body) {
            # Repair body
            delete $reference->{__body_type};
            $reference->{_body} = $body;
        }

        if($env) { $reference->{env} = $env }



( run in 0.319 second using v1.01-cache-2.11-cpan-65fba6d93b7 )