| 1 | # ----------------------------------------------------------------------------# | ||||||||||||||||||||
| 2 | # Tie::Scalar::Transactional # | ||||||||||||||||||||
| 3 | # # | ||||||||||||||||||||
| 4 | # Copyright (c) 2002-03 Arun Kumar U <u_arunkumar@yahoo.com>. # | ||||||||||||||||||||
| 5 | # All rights reserved. # | ||||||||||||||||||||
| 6 | # # | ||||||||||||||||||||
| 7 | # This program is free software; you can redistribute it and/or # | ||||||||||||||||||||
| 8 | # modify it under the same terms as Perl itself. # | ||||||||||||||||||||
| 9 | # ----------------------------------------------------------------------------# | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | package Tie::Scalar::Transactional; | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | ▶ | use Exporter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use vars qw($VERSION @EXPORT_OK @ISA %EXPORT_TAGS); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | $VERSION = '0.01'; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | @EXPORT_OK = qw( commit rollback ); | |||||||||||||||||||
| 22 | 1 | %EXPORT_TAGS = ( commit => \@EXPORT_OK, rollback => \@EXPORT_OK ); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | sub new | ||||||||||||||||||||
| 27 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=13.0
Pod
documented
| |||||||||||||||||||||
| 28 | 1 | my $class = $_[0]; | |||||||||||||||||||
| 29 | 1 | my $var = $_[1]; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | ▶ | if (!defined($var)) { croak('Usage: ' . __PACKAGE__ . '->new($var)'); } | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 32 | elsif (ref($var)) { | ||||||||||||||||||||
| 33 | 0 | my $message = "References are not allowed\n"; | |||||||||||||||||||
| 34 | 0 | croak("$message\nUsage: " . __PACKAGE__ . '->new($var)'); | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 1 | my $self = tie $_[1], $class, $_[1]; | |||||||||||||||||||
| 38 | 1 | return $self; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | sub TIESCALAR | ||||||||||||||||||||
| 42 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
TIESCALAR: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 43 | 2 | my ($proto, $var) = @_; | |||||||||||||||||||
| 44 | 2 | my ($self, $class); | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 2 | $self = {}; | |||||||||||||||||||
| 47 | 2 | ▶ | $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 48 | 2 | bless $self, $class; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | $self->_initialize($var); | |||||||||||||||||||
| 51 | 2 | return $self; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | sub _initialize | ||||||||||||||||||||
| 55 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
_initialize: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | 2 | my ($self, $value) = @_; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 2 | $self->{'_committedValue'} = $value; | |||||||||||||||||||
| 59 | 2 | $self->{'_currentValue'} = $value; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | sub FETCH | ||||||||||||||||||||
| 63 | 15 | ▶ | { | ||||||||||||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | 15 | my ($self) = @_; | |||||||||||||||||||
| 65 | 15 | return $self->{'_currentValue'}; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | sub STORE | ||||||||||||||||||||
| 69 | 5 | ▶ | { | ||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 5 | my ($self, $value) = @_; | |||||||||||||||||||
| 71 | 5 | $self->{'_currentValue'} = $value; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | sub DESTROY | ||||||||||||||||||||
| 75 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 76 | ## Nothing to be done (atleast in this version !!) | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | sub commit | ||||||||||||||||||||
| 80 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
commit: called CC=5 SCAR=22.3
Pod
documented
| |||||||||||||||||||||
| 81 | 2 | my $self; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 2 | ▶ | if (@_ == 1) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 84 | 2 | ▶ | if (UNIVERSAL::isa($_[0], __PACKAGE__)) { $self = $_[0]; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||
| 86 | 0 | ▶ | $self = tied($_[0]) if (!ref($_[0])); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | 0 | elsif (@_ == 2) { $self = tied($_[1]); } | |||||||||||||||||||
| 90 | else { | ||||||||||||||||||||
| 91 | 0 | my $package = __PACKAGE__; | |||||||||||||||||||
| 92 | 0 | croak(<<"USAGE"); | |||||||||||||||||||
| 93 | Usage: $package->commit(\$var); | ||||||||||||||||||||
| 94 | tied(\$var)->commit(); | ||||||||||||||||||||
| 95 | commit(\$a); # (if explicitely imported) | ||||||||||||||||||||
| 96 | USAGE | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | $self->{'_committedValue'} = $self->{'_currentValue'}; | |||||||||||||||||||
| 100 | 2 | return 1; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | sub rollback | ||||||||||||||||||||
| 104 | 5 | ▶ | { | ||||||||||||||||||
|
Subroutine
rollback: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||
| 105 | 5 | my $self; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 5 | ▶ | if (@_ == 1) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 108 | 4 | ▶ | if (UNIVERSAL::isa($_[0], __PACKAGE__)) { $self = $_[0]; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||
| 110 | 1 | ▶ | $self = tied($_[0]) if (!ref($_[0])); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | 1 | elsif (@_ == 2) { $self = tied($_[1]); } | |||||||||||||||||||
| 114 | else { | ||||||||||||||||||||
| 115 | 0 | my $package = __PACKAGE__; | |||||||||||||||||||
| 116 | 0 | croak(<<"USAGE"); | |||||||||||||||||||
| 117 | Usage: $package->rollback(\$var); | ||||||||||||||||||||
| 118 | tied(\$var)->rollback(); | ||||||||||||||||||||
| 119 | rollback(\$a); # (if explicitely imported) | ||||||||||||||||||||
| 120 | USAGE | ||||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 5 | $self->{'_currentValue'} = $self->{'_committedValue'}; | |||||||||||||||||||
| 124 | 5 | return 1; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 1 | 1; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||