| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## Module Generic - ~/lib/Module/Generic/Tie.pm | ||||||||||||||||||||||||||||
| 3 | ## Version v1.2.0 | ||||||||||||||||||||||||||||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||
| 6 | ## Created 2021/03/20 | ||||||||||||||||||||||||||||
| 7 | ## Modified 2025/04/19 | ||||||||||||||||||||||||||||
| 8 | ## All rights reserved | ||||||||||||||||||||||||||||
| 9 | ## | ||||||||||||||||||||||||||||
| 10 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 11 | ## under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 12 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 13 | package Module::Generic::Tie; | ||||||||||||||||||||||||||||
| 14 | BEGIN | ||||||||||||||||||||||||||||
| 15 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Tie::Hash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | our @ISA = qw( Tie::Hash ); | |||||||||||||||||||||||||||
| 20 | 1 | our $VERSION = 'v1.2.0'; | |||||||||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub TIEHASH | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 27 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 28 | 0 | my $pkg = ( caller() )[0]; | |||||||||||||||||||||||||||
| 29 | 0 | my %arg = ( @_ ); | |||||||||||||||||||||||||||
| 30 | 0 | my $auth = [ $pkg, __PACKAGE__ ]; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | if( $arg{ 'pkg' } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | 0 | my $ok = delete( $arg{ 'pkg' } ); | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | push( @$auth, ref( $ok ) eq 'ARRAY' ? @$ok : $ok ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 0 | my $priv = { 'pkg' => $auth }; | |||||||||||||||||||||||||||
| 37 | 0 | my $data = { '__priv__' => $priv }; | |||||||||||||||||||||||||||
| 38 | 0 | my @keys = keys( %arg ); | |||||||||||||||||||||||||||
| 39 | 0 | @$priv{ @keys } = @arg{ @keys }; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | return( bless( $data, ref( $self ) || $self ) ); | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub CLEAR | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 45 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 46 | 0 | my $pkg = ( caller() )[0]; | |||||||||||||||||||||||||||
| 47 | 0 | my $data = $self->{ '__priv__' }; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | return() if( $data->{ 'readonly' } && $pkg ne __PACKAGE__ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$data->{"readonly"} and $pkg ne __PACKAGE__
MC/DC0%0/2
$data->{"readonly"} and $pkg ne __PACKAGE__
$data->{"readonly"}
$pkg ne __PACKAGE__
Truth table0%0/3
A and B
A= $data->{"readonly"}B= $pkg ne __PACKAGE__
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if( !( $data->{ 'perms' } & 2 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | return if( !grep( /^$pkg$/, @{ $data->{ 'pkg' } } ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 0 | my $key = $self->FIRSTKEY( @_ ); | |||||||||||||||||||||||||||
| 54 | 0 | my @keys = (); | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | while( defined( $key ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 0 | push( @keys, $key ); | |||||||||||||||||||||||||||
| 58 | 0 | $key = $self->NEXTKEY( @_, $key ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | foreach $key ( @keys ) | |||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | 0 | $self->DELETE( @_, $key ); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub DELETE | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 68 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 69 | 0 | my $pkg = ( caller() )[0]; | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | $pkg = ( caller(1) )[0] if( $pkg eq 'Module::Generic' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | my $data = $self->{ '__priv__' }; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | return if( $_[0] eq '__priv__' && $pkg ne __PACKAGE__ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_[0] eq "__priv__" and $pkg ne __PACKAGE__
MC/DC0%0/2
$_[0] eq "__priv__" and $pkg ne __PACKAGE__
$_[0] eq "__priv__"
$pkg ne __PACKAGE__
Truth table0%0/3
A and B
A= $_[0] eq "__priv__"B= $pkg ne __PACKAGE__
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if( !( $data->{ 'perms' } & 2 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return() if( !grep( /^$pkg$/, @{ $data->{ 'pkg' } } ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 0 | return( delete( $self->{ shift( @_ ) } ) ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub EXISTS | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 82 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 83 | 0 | my $pkg = ref( $self ); | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | return(0) if( $_[0] eq '__priv__' && $pkg ne __PACKAGE__ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_[0] eq "__priv__" and $pkg ne __PACKAGE__
MC/DC0%0/2
$_[0] eq "__priv__" and $pkg ne __PACKAGE__
$_[0] eq "__priv__"
$pkg ne __PACKAGE__
Truth table0%0/3
A and B
A= $_[0] eq "__priv__"B= $pkg ne __PACKAGE__
| |||||||||||||||||||||||||||||
| 85 | 0 | my $data = $self->{ '__priv__' }; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | if( !( $data->{ 'perms' } & 4 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||
| 88 | 0 | my $pkg = ( caller() )[0]; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | return(0) if( !grep( /^$pkg$/, @{$data->{ 'pkg' }} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | return( exists( $self->{ shift( @_ ) } ) ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub FETCH | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 96 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 97 | 0 | my $pkg = ref( $self ); | |||||||||||||||||||||||||||
| 98 | # This is a hidden entry, we return nothing | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return() if( $_[0] eq '__priv__' && $pkg ne __PACKAGE__ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_[0] eq "__priv__" and $pkg ne __PACKAGE__
MC/DC0%0/2
$_[0] eq "__priv__" and $pkg ne __PACKAGE__
$_[0] eq "__priv__"
$pkg ne __PACKAGE__
Truth table0%0/3
A and B
A= $_[0] eq "__priv__"B= $pkg ne __PACKAGE__
| |||||||||||||||||||||||||||||
| 100 | 0 | my $data = $self->{ '__priv__' }; | |||||||||||||||||||||||||||
| 101 | # If we have to protect our object, we hide its inner content if our caller is not our creator | ||||||||||||||||||||||||||||
| 102 | # if( $data->{ 'protect' } ) | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if( !( $data->{ 'perms' } & 4 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 0 | my $pkg = ( caller() )[0]; | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | return if( !grep( /^$pkg$/, @{$data->{ 'pkg' }} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | return( $self->{ shift( @_ ) } ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub FIRSTKEY | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 113 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 114 | # my $a = scalar( keys( %$hash ) ); | ||||||||||||||||||||||||||||
| 115 | # return( each( %$hash ) ); | ||||||||||||||||||||||||||||
| 116 | 0 | my $data = $self->{ '__priv__' }; | |||||||||||||||||||||||||||
| 117 | ## if( $data->{ 'protect' } ) | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if( !( $data->{ 'perms' } & 4 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||
| 120 | 0 | my $pkg = ( caller(0) )[0]; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | return if( !grep( /^$pkg$/, @{$data->{ 'pkg' }} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | my( @keys ) = grep( !/^__priv__$/, keys( %$self ) ); | |||||||||||||||||||||||||||
| 124 | 0 | $self->{ '__priv__' }->{ 'ITERATOR' } = \@keys; | |||||||||||||||||||||||||||
| 125 | 0 | return( shift( @keys ) ); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub NEXTKEY | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 130 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 131 | ## return( each( %$hash ) ); | ||||||||||||||||||||||||||||
| 132 | 0 | my $data = $self->{ '__priv__' }; | |||||||||||||||||||||||||||
| 133 | ## if( $data->{ 'protect' } ) | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if( !( $data->{ 'perms' } & 4 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||
| 136 | 0 | my $pkg = ( caller(0) )[0]; | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | return if( !grep( /^$pkg$/, @{$data->{ 'pkg' }} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 0 | my $keys = $self->{ '__priv__' }->{ 'ITERATOR' }; | |||||||||||||||||||||||||||
| 140 | 0 | return( shift( @$keys ) ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub STORE | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
STORE: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 145 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | return() if( $_[0] eq '__priv__' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | my $data = $self->{ '__priv__' }; | |||||||||||||||||||||||||||
| 148 | 0 | ▶ | if( !( $data->{ 'perms' } & 2 ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | 0 | my $pkg = ( caller() )[0]; | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | $pkg = ( caller(1) )[0] if( $pkg eq 'Module::Generic' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | return if( !grep( /^$pkg$/, @{ $data->{ 'pkg' } } ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 0 | $self->{ $_[0] } = $_[1]; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 1 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||