| 1 | # | ||||||||||||||||||||||||||||
| 2 | # $Id: SaveLater.pm,v 0.05 2020/08/05 18:26:03 dankogai Exp dankogai $ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | package Tie::SaveLater; | ||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.05 $ =~ /(\d+)/g; | |||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | our $DEBUG = 0; | |||||||||||||||||||||||||||
| 10 | 4 | my (%OBJ2FN, %FN2OBJ, %OPTIONS); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | sub make_subclasses{ | ||||||||||||||||||||||||||
|
Subroutine
make_subclasses: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 4 | my $pkg = shift; | |||||||||||||||||||||||||||
| 14 | 4 | for my $type (qw/SCALAR ARRAY HASH/){ | |||||||||||||||||||||||||||
| 15 | 12 | my $class = $pkg; my $Type = ucfirst(lc $type); | |||||||||||||||||||||||||||
| 16 | 12 | eval qq{ package $class\:\:$type; | |||||||||||||||||||||||||||
| 17 | require Tie\:\:$Type; | ||||||||||||||||||||||||||||
| 18 | push our \@ISA, qw($class Tie\:\:Std$Type); }; | ||||||||||||||||||||||||||||
| 19 | 12 | ▶ | $@ and croak $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub load { my $class = shift; croak "$class, please implement load()!" } | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub save { my $class = ref shift; croak "$class, please implement save()!" } | ||||||||||||||||||||||||||
|
Subroutine
save: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 6 | ▶ | sub options{ | ||||||||||||||||||||||||||
|
Subroutine
options: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 6 | ▶ | @_ and $OPTIONS{0+$self} = [ @_ ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 6 | ▶ | return $OPTIONS{0+$self} ? @{ $OPTIONS{0+$self} } : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | sub super_super{ | ||||||||||||||||||||||||||
|
Subroutine
super_super: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 1 | my $name = shift; | |||||||||||||||||||||||||||
| 35 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | &{ ${ref($self) . "::ISA"}[1] . "::$name"}($self, @_); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 6 | ▶ | sub TIEHASH { return shift->TIE('HASH' => @_) }; | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 6 | ▶ | sub TIEARRAY { return shift->TIE('ARRAY' => @_) }; | ||||||||||||||||||||||||||
|
Subroutine
TIEARRAY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 14 | ▶ | sub TIESCALAR{ return shift->TIE('SCALAR' => @_) }; | ||||||||||||||||||||||||||
|
Subroutine
TIESCALAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 4 | my %types2check = map { $_ => 1 } qw/HASH ARRAY/; | |||||||||||||||||||||||||||
| 44 | 26 | ▶ | sub TIE{ | ||||||||||||||||||||||||||
|
Subroutine
TIE: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 26 | my $class = shift; | |||||||||||||||||||||||||||
| 46 | 26 | my $type = shift; | |||||||||||||||||||||||||||
| 47 | 26 | ▶ | my $filename = shift or croak "filename missing"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 26 | my $self; | |||||||||||||||||||||||||||
| 49 | 26 | ▶ | if (-f $filename){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 17 | ▶ | $self = $class->load($filename) or croak "$filename : $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | croak "existing $filename does not store $type" | ||||||||||||||||||||||||||||
| 52 | 17 | ▶ | if $types2check{$type} and !$self->isa($type); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$types2check{$type} and not $self->isa($type)
MC/DC0%0/2
$types2check{$type} and not $self->isa($type)
$types2check{$type}
not $self->isa($type)
Truth table66%2/3
A and not(B)
A= $types2check{$type}B= not $self->isa($type)
| |||||||||||||||||||||||||||||
| 53 | }else{ | ||||||||||||||||||||||||||||
| 54 | $self = | ||||||||||||||||||||||||||||
| 55 | 9 | { HASH => {}, ARRAY => [], SCALAR => \do{ my $scalar }}->{$type}; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 26 | bless $self => $class.'::'.$type; | |||||||||||||||||||||||||||
| 58 | 26 | ▶ | $DEBUG and carp sprintf("tied $filename => 0x%x", 0+$self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 26 | ▶ | @_ and $self->options(@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 26 | $self->_regobj($filename); | |||||||||||||||||||||||||||
| 61 | 26 | $self; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 26 | ▶ | sub UNTIE{ | ||||||||||||||||||||||||||
|
Subroutine
UNTIE: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 65 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 66 | 26 | $self->save; | |||||||||||||||||||||||||||
| 67 | 26 | ▶ | $DEBUG and carp "untied ", $self->filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 26 | $self->_unregobj(); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 26 | ▶ | sub DESTROY{ shift->UNTIE } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 25 | ▶ | sub filename{ $OBJ2FN{ 0+shift } } | ||||||||||||||||||||||||||
|
Subroutine
filename: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 26 | ▶ | sub _regobj{ | ||||||||||||||||||||||||||
|
Subroutine
_regobj: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 26 | $OBJ2FN{0+$_[0]} = $_[1]; | |||||||||||||||||||||||||||
| 77 | 26 | $FN2OBJ{$_[1]} = 0+$_[0]; | |||||||||||||||||||||||||||
| 78 | 26 | return; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 26 | ▶ | sub _unregobj{ | ||||||||||||||||||||||||||
|
Subroutine
_unregobj: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 26 | delete $FN2OBJ{ $OBJ2FN{ 0+$_[0] } }; | |||||||||||||||||||||||||||
| 83 | 26 | delete $OPTIONS{ 0+$_[0] }; | |||||||||||||||||||||||||||
| 84 | 26 | delete $OBJ2FN{ 0+$_[0] }; | |||||||||||||||||||||||||||
| 85 | 26 | return; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | 1; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||