Data-Transactional
view release on metacpan or search on metacpan
lib/Data/Transactional.pm view on Meta::CPAN
my $self = shift;
undef $@;
while(!$@) { eval { $self->commit(); }; }
}
=item rollback
Revert the data structure to the last checkpoint. To roll back beyond the
first checkpoint is a fatal error.
=cut
sub rollback {
my $self = shift;
(tied %{$self})->rollback();
}
=item rollback_all
Roll back all changes.
=cut
sub rollback_all {
my $self = shift;
undef $@;
while(!$@) { eval { $self->rollback(); }; }
}
=item current_state
Return a reference to the current state of the underlying object.
=cut
sub current_state {
my $self = shift;
return $self->isa('HASH') ?
tied(%{$self})->current_state() :
tied(@{$self})->current_state();
}
=back
=head1 IMPLEMENTATION NOTES
This module relies on two other packages which are included in the same
file - Data::Transactional::Hash and Data::Transactional::Array. These
are where the magic really happens. These implement everything needed
for C<tie()>ing those structures, plus their own C<checkpoint()>,
C<commit()> and C<rollback()> methods. When you create a
Data::Transactional object, what you really get is one of these tied
structures, reblessed into the Data::Transactional class. The
transactional methods simply call through to the same method on the
underlying tied structure.
This is loosely inspired by L<DBM::Deep>.
=head1 BUGS/WARNINGS
I assume that C<$[> is zero.
Storing blessed objects in a C<Data::Transactional> structure is not
supported. I suppose it could be, but there's no sane way that they
could be transactionalised. This also applies to tie()d objects.
Please note that in the case of tie()d objects, we don't do a great deal
of checking, so things may break in subtle and hard-to-debug ways.
The precise details of how the transactional methods affect sub-structures
in your data may change before a 1.0 release. If you have suggestions for
how it could be improved, do please let me know.
The SPLICE() operation is *not defined* for transactionalised arrays,
because it makes my brane hurt. If you want to implement this please
do! Remember that you should use STORE() to put each new entry in the
array, as that will properly handle adding complex data structures.
No doubt there are others. When submitting a bug report please please
please include a test case, in the form of a .t file, which will fail
with my version of the module and pass once the bug is fixed. If you
include a patch as well, that's even better!
=head1 FEEDBACK
I welcome all comments - both praise and constructive criticism - about
my code. If you have a question about how to use it please read *all*
of the documentation first, and let me know what you have tried and how
the results differ from what you wanted or expected.
I do not consider blind, automatically generated and automatically sent
error reports to be constructive.
Don't send them, you'll only get flamed.
=head1 AUTHOR
David Cantrell E<lt>david@cantrell.org.ukE<gt>
=head1 LICENCE
This software is Copyright 2004 David Cantrell. You may use, modify and
distribute it under the same terms as perl itself.
=cut
package Data::Transactional::Hash;
use Storable qw(dclone);
use strict;use warnings;
sub TIEHASH {
my $class = shift;
my $self = {
STACK => [],
CURRENT_STATE => {},
};
return bless $self, $class;
}
sub CLEAR {
my $self=shift;
$self->{CURRENT_STATE}={};
( run in 1.346 second using v1.01-cache-2.11-cpan-5735350b133 )