Callback-Frame

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

          $foo = 2;
          $cb = fub {
            return $foo;
          };
        };

        say $foo;     # 1
        say $cb->();  # 2  <- hooray!
        say $foo;     # 1

    Don't be fooled into thinking that this is a lexical binding though.
    While the callback $cb is executing, all parts of the program will see
    the binding containing 2:

        our $foo = 1;
        my $cb;

        sub global_foo_getter {
          return $foo;
        }

        frame_local __PACKAGE__.'::foo', sub {
          $foo = 2;
          $cb = fub {
            return global_foo_getter();
          };
        };

        say $foo;     # 1
        say $cb->();  # 2  <- still 2
        say $foo;     # 1

    You can install multiple local variables in the same frame with the
    "frame" interface:

        frame(local => __PACKAGE__.'::foo',
              local => 'main::bar',
              code => { })->();

    Note that if you have both "catch" and "local" elements in a frame, in
    the event of an error the local bindings will not be present inside the
    "catch" handler (use a nested frame if you need this).

    Variable names must be fully package qualified. The best way to do this
    for variables in your current package is to use the ugly "__PACKAGE__"
    technique.

    Objects stored in local bindings managed by Callback::Frame will not be
    destroyed until all references to the frame-wrapped callback that
    contains the binding are destroyed, along with all references to any
    deeper frames.

SEE ALSO
    The Callback::Frame github repo
    <https://github.com/hoytech/Callback-Frame>

    AnyEvent::Task uses Callback::Frame and its docs have more discussion on
    exception handling in async apps.

    This module's "catch" syntax is of course modeled after "normal
    language" style exception handling as implemented by Try::Tiny and
    similar.

    This module depends on Guard to maintain the
    $Callback::Frame::active_frames datastructure and to ensure that "local"
    binding updates aren't lost even when exceptions or other non-local
    returns occur.

    AnyEvent::Debug provides an interactive debugger for AnyEvent
    applications and uses some of the same techniques that Callback::Frame
    does. AnyEvent::Callback and AnyEvent::CallbackStack sort of solve the
    dynamic error handler problem. Unlike these modules, Callback::Frame is
    not related at all to AnyEvent, except that it happens to be useful in
    AnyEvent libraries and applications (among other things).

    Promises and Future are similar modules but they solve a slightly
    different problem. In the area of exception handling they require a more
    drastic restructuring of your async code because you need to pass
    "promise/future" objects around to maintain context. Callback::Frame is
    context-less (or rather the context is implicit in the dynamic state).
    That said, both of these modules should be compatible with
    Callback::Frame.

    Miscellaneous other modules: IO::Lambda::Backtrace,
    POE::Filter::ErrorProof

    Python Tornado's StackContext
    <http://www.tornadoweb.org/en/branch2.3/stack_context.html> and
    "async_callback"

    Let Over Lambda, Chapter 2
    <http://letoverlambda.com/index.cl/guest/chap2.html>

    UNWIND-PROTECT vs. Continuations
    <http://www.nhplace.com/kent/PFAQ/unwind-protect-vs-continuations-origin
    al.html>

BUGS
    For now, "local" bindings can only be created in the scalar namespace.
    Also, none of the other nifty things that local can do (like localising
    a hash table value) are supported yet.

AUTHOR
    Doug Hoyte, "<doug@hcsw.org>"

COPYRIGHT & LICENSE
    Copyright 2012-2016 Doug Hoyte.

    This module is licensed under the same terms as perl itself.



( run in 1.210 second using v1.01-cache-2.11-cpan-39bf76dae61 )