Object/Simple.pm

filter: Statementstmt 14.6% 19 / 130 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 33.3% 5 / 15 Podpod 80.0% 8 / 10 totaltotal 11.9% 32 / 268 CC 46 SCAR 53.9
CC
46
Cov
8%
commit
506.0
new
132.0
load
56.0
CRAP 218.9
↑ Summary Database.pm »
1 #!/usr/bin/perl -s
2 ##
3 ## Persistence::Object::Simple -- Persistence For Perl5 Objects.
4 ##
5 ## $Date: 1999/10/13 23:08:43 $
6 ## $Revision: 0.47 $
7 ## $State: Exp $
8 ## $Author: root $
9 ##
10 ## Copyright (c) 1998, Vipul Ved Prakash. All rights reserved.
11 ## This code is free software; you can redistribute it and/or modify
12 ## it under the same terms as Perl itself.
13  
14 package Persistence::Object::Simple;
15 1 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ( $VERSION ) = '$Revision: 0.92 $' =~ /\s+(\d+\.\d+)\s+/; #-- Module Version
22  
23 1 my $DOPE = "/tmp"; #-- The default Directory Of Persistent Entities
24 1 my $MAXTRIES = 250; #-- TTL counter for generating a unique file
25  
26 0 ▶sub dope { #-- Default DOPE access method
Subroutine dope: not called CC=2 SCAR=17.9
Pod undocumented
27  
28 0 my ( $self, $dope ) = @_;
29 0 ▶ ${ $self->{ __DOPE } } = $dope if $dope;
Branch: 1 branch
BranchTrueFalseTotal
if $dope 0 0 0
30 0 ${ $self->{ __DOPE } };
31  
32 }
33
34 0 ▶sub new { #-- Constructor. Creates and inits a P::O::S
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
35 #-- object instance. Binds class data.
36  
37 0 my ( $class, %args ) = @_;
38 0 my $self = {};
39 0 my $fn = $args{ __Fn };
40 0 my $exists;
41 0 ▶ $args{ __Create } ||= "";
Condition0%0/2
$args{"__Create"} ||= ""
l!l
00
MC/DC0%0/1
$args{"__Create"} ||= ""
$args{"__Create"}
Truth table0%0/2
A
A= $args{"__Create"}
Aresult
11
00
42 0 ▶ if ($args{ __Create } and lc($args{ __Create }) eq "no") {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"__Create"} and lc $args{"__Create"} eq "no") 0 0 0
Condition0%0/3
$args{"__Create"} and lc $args{"__Create"} eq "no"
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"__Create"} and lc $args{"__Create"} eq "no"
$args{"__Create"} lc $args{"__Create"} eq "no"
Truth table0%0/3
A and B
A= $args{"__Create"}B= lc $args{"__Create"} eq "no"
ABresult
0X0
100
111
43 0 $exists = 1;
44 }
45  
46 0 ▶ return undef if !(-e $fn) && $exists;
Branch: 1 branch
BranchTrueFalseTotal
if not -e $fn and $exists 0 0 0
Condition0%0/3
not -e $fn and $exists
!ll&&!rl&&r
000
MC/DC0%0/2
not -e $fn and $exists
not -e $fn $exists
Truth table0%0/3
not(A) and B
A= not -e $fnB= $exists
ABresult
0X0
100
111
47 0 ▶ unless ( $fn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fn) 0 0 0
48 0 ▶ $fn = $class->uniqfile ( $args{ __Dope } || $DOPE, $args{ __Random } );
Condition0%0/3
$args{'__Dope'} || $DOPE
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'__Dope'} || $DOPE
$args{'__Dope'} $DOPE
Truth table0%0/3
A || B
A= $args{'__Dope'}B= $DOPE
ABresult
1X1
011
000
49 0 ▶ return undef unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 0 0
50 }
51  
52 0 $self->{ __Fn } = $fn;
53 0 $self->{ __DOPE } = \$DOPE;
54  
55
56 0 my $existing = $class->load ( __Fn => $fn );
57 0 ▶ $self = $existing if $existing;
Branch: 1 branch
BranchTrueFalseTotal
if $existing 0 0 0
58 0 for ( keys %args ) { $self->{ $_ } = $args{ $_ } }
59  
60 0 bless $self, $class;
61  
62 }
63  
64 0 ▶sub dumper { #-- Returns the Data::Dumper object associated
Subroutine dumper: not called CC=1 SCAR=6.9
Pod documented
65 #-- with the instance
66 0 my $self = shift;
67  
68 0 $self->{ __Dumper } = new Data::Dumper ( [ $self ] );
69 0 return $self->{ __Dumper };
70  
71 }
72  
73 0 ▶sub commit { #-- Commits the object to disk. Works as a class
Subroutine commit: not called CC=22 SCAR=62.3
Pod documented
74 #-- method as well.
75 0 my ( $self, %args ) = @_;
76 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
77 0 my ( $d, $fn );
78 0 ▶ $fn = $args{ __Fn } || $self->{ __Fn };
Condition0%0/3
$args{'__Fn'} || $self->{'__Fn'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'__Fn'} || $self->{'__Fn'}
$args{'__Fn'} $self->{'__Fn'}
Truth table0%0/3
A || B
A= $args{'__Fn'}B= $self->{'__Fn'}
ABresult
1X1
011
000
79  
80 0 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
81 0 ▶ $d = $self->{ __Dumper } || $self->dumper () ;
Condition0%0/3
$self->{'__Dumper'} || $self->dumper
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'__Dumper'} || $self->dumper
$self->{'__Dumper'} $self->dumper
Truth table0%0/3
A || B
A= $self->{'__Dumper'}B= $self->dumper
ABresult
1X1
011
000
82 } else { # -- Whoa! It's a class method!
83 0 $d = new Data::Dumper ( [ $args{ Data } ] );
84 }
85  
86 0 ▶ if ( $args{ __Dope } && $fn ) { # -- change to a new dope
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"__Dope"} and $fn) 0 0 0
Condition0%0/3
$args{"__Dope"} and $fn
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"__Dope"} and $fn
$args{"__Dope"} $fn
Truth table0%0/3
A and B
A= $args{"__Dope"}B= $fn
ABresult
0X0
100
111
87 0 $fn =~ s:.*/::;
88 0 $args{ __Dope } =~ s:/$::;
89 0 $fn = $args{ __Dope } . "/$fn";
90 0 ▶ croak "$fn exists. Can't overwrite." if -e $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -e $fn 0 0 0
91 }
92
93 0 ▶ unless ( $fn ) { # -- generate a uniq filename in the
Branch: 1 branch
BranchTrueFalseTotal
unless ($fn) 0 0 0
94 0 ▶ $args{ __Dope } = $DOPE unless $args{ __Dope }; # -- new dope
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"__Dope"} 0 0 0
95 0 $fn = $class->uniqfile ( $args{ __Dope } );
96 }
97  
98
99 0 my $locked_fh = $self->{ __Lock };
100 0 ▶ seek $locked_fh, 0, 0 if $locked_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $locked_fh 0 0 0
101 0 my $fh;
102  
103 # -- delete extra object data and class data-refs if this looks like
104 # -- an object.
105 0 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
106 0 ▶ for ( keys %$self ) { delete $self->{ $_ }
Branch: 1 branch
BranchTrueFalseTotal
if /^__(?:Dumper|DOPE|Fn|Lock|Create)/ 0 0 0
107 if /^__(?:Dumper|DOPE|Fn|Lock|Create)/ };
108 }
109  
110 0 ▶ unless ( $locked_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($locked_fh) 0 0 0
111 # guard against disallowed characters in filename (basically those
112 # which might mess up the open() call)
113 0 ▶ if (($fn) = ($fn =~ /^([^<>|+]+)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($fn) = $fn =~ /^([^<>|+]+)$/) { } 0 0 0
114 0 ▶ open C, ">$fn" || croak "Can't open $fn for writing.";
Condition0%0/2
">$fn" || croak("Can't open $fn for writing.")
l!l
00
MC/DC0%0/1
">$fn" || croak("Can't open $fn for writing.")
">$fn"
Truth table0%0/2
A
A= ">$fn"
Aresult
11
00
115 0 eval { flock C, 2 }; undef $@;
116 0 $fh = *C{ IO };
117 } else {
118 0 die "Filename '$fn' contains inappropriate characters";
119 }
120 }
121  
122 0 ▶ print { $locked_fh ? $locked_fh : $fh }
Branch: 2 branches
BranchTrueFalseTotal
$locked_fh ? : 0 0 0
defined &Data::Dumper::Dumpxs ? : 0 0 0
123 defined &Data::Dumper::Dumpxs ? $d->Dumpxs() : $d->Dump();
124 0 ▶ close $fh if $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 0 0 0
125  
126 0 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 0 0 0
127 0 $self->{ __Fn } = $fn;
128 0 ▶ $self->{ __Lock } = $locked_fh if $locked_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $locked_fh 0 0 0
129 }
130  
131 0 return $fn;
132  
133 }
134  
135 0 ▶sub load {
Subroutine load: not called CC=7 SCAR=40.3
Pod documented
136  
137 0 my ( $class, %args ) = @_;
138  
139 0 ▶ return undef unless -e $args{ __Fn };
Branch: 1 branch
BranchTrueFalseTotal
unless -e $args{"__Fn"} 0 0 0
140
141 0 ▶ open C, $args{ __Fn } || croak "Couldn't open $args{ __Fn }.";
Condition0%0/2
$args{'__Fn'} || croak("Couldn't open $args{'__Fn'}.")
l!l
00
MC/DC0%0/1
$args{'__Fn'} || croak("Couldn't open $args{'__Fn'}.")
$args{'__Fn'}
Truth table0%0/2
A
A= $args{'__Fn'}
Aresult
11
00
142 0 eval { flock C, 2 }; undef $@;
143  
144 0 local $/ = undef; # slurp mode
145 0 my $objectfile = <C>;
146 0 close C;
147  
148 # untaint the input meaningfully
149 0 ▶ if ($objectfile =~ /^(\$VAR1 = bless[^;]+;)$/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($objectfile =~ /^(\$VAR1 = bless[^;]+;)$/s) { } 0 0 0
elsif ($objectfile =~ /^$/) { } 0 0 0
150 0 my $object = eval "$1";
151 0 ▶ croak "$args{ __Fn } is corrupt. Object loading aborted." if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
152  
153 0 ▶ $object->{ __Fn } = $args{ __Fn } if ref $object eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $object eq "HASH" 0 0 0
154 0 return $object;
155 } elsif ($objectfile =~ /^$/) {
156 0 return undef;
157 } else {
158 0 croak "Tainted data from $args{__Fn} looks unsafe. Object loading aborted.";
159 }
160 }
161  
162 0 ▶sub expire {
Subroutine expire: not called CC=2 SCAR=17.9
Pod documented
163  
164 0 my ( $self ) = @_;
165 0 my $fn = $self->{ __Fn };
166 0 ▶ return 1 if unlink $fn;
Branch: 1 branch
BranchTrueFalseTotal
if unlink $fn 0 0 0
167  
168 }
169  
170 0 ▶sub move {
Subroutine move: not called CC=2 SCAR=17.9
Pod documented
171  
172 0 my ( $self, %args ) = @_;
173 0 my $class = ref $self;
174  
175 0 $self->expire ();
176 0 ▶ $self->{ __Fn } = undef if $args{ __Fnalter };
Branch: 1 branch
BranchTrueFalseTotal
if $args{"__Fnalter"} 0 0 0
177 0 my $fn = $self->commit ( %args );
178  
179 0 my $moved = $class->new ( __Fn => $fn );
180 0 $self = $moved;
181  
182 }
183  
184 0 ▶sub lock {
Subroutine lock: not called CC=3 SCAR=24.8
Pod documented
185  
186 0 my ( $self ) = @_;
187  
188 0 my $fn = $self->{ __Fn };
189 0 ▶ $self->commit unless -e $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $fn 0 0 0
190 0 ▶ open ( F, "+<$fn" ) || croak "Couldn't open $fn for locking. Commit first!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "+<$fn" or croak("Couldn't open $fn for locking. Commit first!") 0 0 0
191 0 eval { flock F, 2 }; undef $@;
192 0 $self->{ __Lock } = *F{ IO };
193  
194 }
195  
196 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
Pod documented
197  
198 0 my ( $self ) = @_;
199 0 my $F = $self->{ __Lock }; close $F;
200 0 undef $self->{ __Lock };
201
202 }
203  
204 0 ▶sub uniqfile {
Subroutine uniqfile: not called CC=4 SCAR=30.0
Pod undocumented
205  
206 0 my ( $class, $dir, $random ) = @_;
207 0 my $fn; my $counter;
208  
209 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
(sysopen C, "$dir/$fn", 194 or $counter > $MAXTRIES) or do { do { $fn = Digest::MD5::md5_hex("@{[time];}.@{[int rand 256];}.$random"); ($fn) = $fn =~ m[([^/<>|;]+)]; ++$counter } } 0 0 0
Condition0%0/3
sysopen C, "$dir/$fn", 194 or $counter > $MAXTRIES
l!l&&r!l&&!r
000
MC/DC0%0/2
sysopen C, "$dir/$fn", 194 or $counter > $MAXTRIES
sysopen C, "$dir/$fn", 194 $counter > $MAXTRIES
Truth table0%0/3
A or B
A= sysopen C, "$dir/$fn", 194B= $counter > $MAXTRIES
ABresult
1X1
011
000
210 0 $fn = Digest::MD5::md5_hex( "@{[time]}.@{[int rand 2**8]}.$random" );
211 0 ($fn) = ($fn =~ m!([^/<>|;]+)!);
212 0 $counter++ ;
213 }
214 until sysopen ( C, "$dir/$fn" , O_RDWR|O_EXCL|O_CREAT )
215 or $counter > $MAXTRIES;
216  
217 0 close C;
218 0 ▶ return undef if $counter > $MAXTRIES;
Branch: 1 branch
BranchTrueFalseTotal
if $counter > $MAXTRIES 0 0 0
219 0 return "$dir/$fn";
220 }
221  
222 1 'True Value';
223  
224 __END__
↑ Summary Database.pm »