NAME

App::Gitc::Reversible

VERSION

version 0.58

NAME

App::Gitc::Reversible - simple reversible computation

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

Copyright 2012 Grant Street Group, All Rights Reserved.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

AUTHOR

Grant Street Group

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