Async-ContextSwitcher

 view release on metacpan or  search on metacpan

lib/Async/ContextSwitcher.pm  view on Meta::CPAN


=head2 context

Returns the current context. Function is exported. Always returns context.

    my $ct = context->{request}{HTTP_CONTENT_TYPE};
    context->{user} = $user;

=cut

sub context() {
    return $CTX if $CTX;
    return $CTX = __PACKAGE__->new;
}

=head2 cb_w_context

Wrapper for callbacks. Function is exported. Wraps a callback with code
that stores and restores context to make sure correct context travels
with your code.

    async_call( callback => cb_w_context { context->{good} = shift } );

Make sure that all callbacks in your code are created with this function
or you can loose track of your context.

=cut

sub cb_w_context(&) {
    my $cb = $_[0];
    my $ctx = $CTX;
    return sub {
        $CTX = $ctx;
        goto &$cb;
    };
}


=head1 AUTHOR



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