NAME

App::Gitc::Reversible - Simple reversible computation for gitc

VERSION

version 0.60

SYNOPSIS

use App::Gitc::Reversible;
reversibly {
    # do something with a side effect
    open my $fh, '>', '/tmp/file' or die;

    # specify how that side effect can be undone
    # (assuming '/tmp/file' did not exist before)
    to_undo { close $fh; unlink '/tmp/file' };

    operation_that_might_die($fh);
    operation_that_might_get_SIGINTed($fh);
});

DESCRIPTION

Perform computations and automatically reverse their side effects if the computations fail. One often wants to perform a series of operations, some of which have side effects, and properly "undo" all side effects if something goes wrong with one of the operations. By invoking your code "reversibly", the undos are handled for you.

SUBROUTINES

failure_warning($message)

Call this sub from inside the coderef argument of "reversibly" to produce a warning if the coderef fails. Only one message is active at a time. In other words, subsequent calls to "failure_warning" change the warning that would be produced.

to_undo

Call this sub from inside the coderef argument of "reversibly" to provide a coderef which should be executed on failure. It can accept a bare code block like:

to_undo { print "undo something\n" };

See "reversibly" for further information.

reversibly

Executes a code reference ($code) allowing operations with side effects to be automatically reversed if $code fails or is interrupted. For example:

reversibly {
    print "hello\n";
    to_undo { print "goodbye\n" };
    die "uh oh\n" if $something_bad;
};

just prints "hello" if $something_bad is false. If it's true, then both "hello" and "goodbye" are printed and the exception "uh oh" is rethrown.

Upon failure, any code refs provided by calling "to_undo" are executed in reverse order. Conceptually, we're unwinding the stack of side effects that $code performed up to the point of failure.

If $code is interrupted with SIGINT, the side effects are undone and an exception "SIGINT\n" is thrown.

Nested calls to reversibly are handled correctly.

SEE ALSO

Data::Transaactional, Object::Transaction.

AUTHOR

Grant Street Group <developers@grantstreet.com>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2013 by Grant Street Group.

This is free software, licensed under:

The GNU Affero General Public License, Version 3, November 2007