lib/Module/Generic/Null.pm

filter: Statementstmt 93.7% 75 / 80 Branchbran 76.4% 26 / 34 Conditioncond 38.4% 10 / 26 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 95.4% 21 / 22 Podpod 20.0% 1 / 5 totaltotal 73.7% 135 / 183 CC 28 SCAR 18.8
CC
28
Cov
79%
THAW
15.8
AUTOLOAD
9.3
new
6.1
CRAP 6.6
« lib/Module/Generic/TieHash.pm ↑ Summary lib/Module/Generic/Boolean.pm »
1 ##----------------------------------------------------------------------------
2 ## Module Generic - ~/lib/Module/Generic/Null.pm
3 ## Version v1.1.5
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/03/20
7 ## Modified 2026/01/22
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 ## Purpose of this package is to provide an object that will be invoked in chain without breaking and then return undef at the end
14 ## Normally if a method in the chain returns undef, perl will then complain that the following method in the chain was called on an undefined value. This Null package alleviate this problem.
15 ## This is an original idea from https://stackoverflow.com/users/2766176/brian-d-foy as documented in this Stackoverflow thread here: https://stackoverflow.com/a/7068271/4814971
16 ## And also by user "particle" in this perl monks discussion here: https://www.perlmonks.org/?node_id=265214
17 package Module::Generic::Null;
18 BEGIN
19 22 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
20 22 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 22 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 22 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶ '""' => sub{ return },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 2 ▶ 'eq' => sub{ _obj_eq(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 2 ▶ 'ne' => sub{ !_obj_eq(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 ▶ 'bool' => sub{0},
Subroutine __ANON__: called CC=1 SCAR=0.0
27 22 fallback => 1,
28 22 );
29 22 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 22 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 22 our( $VERSION ) = 'v1.1.5';
32 };
33  
34 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 22 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 sub new
38 12 ▶{
Subroutine new: called CC=5 SCAR=18.1
Pod documented
39 12 my $this = shift( @_ );
40 12 ▶ my $class = ref( $this ) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
41 12 my $error_object;
42 12 ▶ $error_object = shift( @_ ) if( Scalar::Util::blessed( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($_[0]) 0 12 12
43 12 ▶ my $hash = ( @_ == 1 && ref( $_[0] ) ? shift( @_ ) : { @_ } );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] ? : 0 12 12
Condition33%1/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
1200
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
44 12 $hash->{has_error} = $error_object;
45 12 return( bless( $hash => $class ) );
46 }
47  
48 sub _obj_eq
49 4 ▶{
Subroutine _obj_eq: called CC=4 SCAR=14.1
50 22 ▶ no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 my $self = shift( @_ );
52 4 my $other = shift( @_ );
53 4 ▶ if( Scalar::Util::blessed( $other ) && $other->isa( 'Module::Generic::Null' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($other) and $other->isa('Module::Generic::Null')) { } 1 3 4
elsif (not ref $other) { } 3 0 3
Condition66%2/3
builtin::blessed($other) and $other->isa("Module::Generic::Null")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($other) and $other->isa("Module::Generic::Null")
builtin::blessed($other) $other->isa("Module::Generic::Null")
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa("Module::Generic::Null")
ABresult
0X0
100
111
54 {
55 1 return( $self eq $other );
56 }
57 # Compare error message
58 elsif( !ref( $other ) )
59 {
60 3 return( '' eq $other );
61 }
62 # Otherwise some reference data to which we cannot compare
63 0 return(0) ;
64 }
65  
66 # NOTE: AUTOLOAD
67 AUTOLOAD
68 20 ▶{
Subroutine AUTOLOAD: called CC=9 SCAR=22.3
69 20 my( $method ) = our $AUTOLOAD =~ /([^:]+)$/;
70 20 my $self = shift( @_ );
71 my $what = ( defined( $self->{wants} ) && length( $self->{wants} ) )
72 ? uc( $self->{wants} )
73 20 ▶ : Wanted::context();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'wants'} && length $self->{'wants'} ? : 7 13 20
Condition66%2/3
defined $self->{"wants"} and length $self->{"wants"}
!ll&&!rl&&r
1307
MC/DC50%1/2
defined $self->{"wants"} and length $self->{"wants"}
defined $self->{"wants"} length $self->{"wants"}
Truth table66%2/3
A and B
A= defined $self->{"wants"}B= length $self->{"wants"}
ABresult
0X0
100
111
74 # If we are chained, return our null object, so the chain continues to work
75 20 ▶ if( $what eq 'OBJECT' )
Branch: 6 branches
BranchTrueFalseTotal
if ($what eq 'OBJECT') { } 4 16 20
elsif ($what eq 'CODE') { } 2 14 16
elsif ($what eq 'ARRAY') { } 2 12 14
elsif ($what eq 'HASH') { } 2 10 12
elsif ($what eq 'REFSCALAR') { } 1 9 10
elsif ($what eq 'GLOB') { } 0 9 9
76 {
77 # No, this is NOT a typo. rreturn() is a function of module Wanted
78 # rreturn( $_[0] );
79 4 rreturn( $self );
80 }
81 elsif( $what eq 'CODE' )
82 {
83 2 ▶ rreturn( sub{ return; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
84 }
85 elsif( $what eq 'ARRAY' )
86 {
87 2 rreturn( [] );
88 }
89 elsif( $what eq 'HASH' )
90 {
91 2 rreturn( {} );
92 }
93 elsif( $what eq 'REFSCALAR' )
94 {
95 1 rreturn( \undef );
96 }
97 elsif( $what eq 'GLOB' )
98 {
99 0 my $ref;
100 0 open( my $fh, '>', \$ref );
101 0 rreturn( $fh );
102 }
103 # Otherwise, we return undef; Empty return returns undef in scalar context and empty list in list context
104 9 return;
105 };
106  
107 # NOTE: DESTROY
108 0 ▶DESTROY {};
Subroutine DESTROY: not called CC=1 SCAR=0.0
109  
110 sub FREEZE
111 2 ▶{
Subroutine FREEZE: called CC=4 SCAR=14.1
Pod undocumented
112 2 my $self = CORE::shift( @_ );
113 2 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition50%1/2
shift @_ // ""
l!l
20
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
114 2 my $class = CORE::ref( $self );
115 2 my %hash = %$self;
116 # Return an array reference rather than a list so this works with Sereal and CBOR
117 # On or before Sereal version 4.023, Sereal did not support multiple values returned
118 2 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 1 1 2
119 {
120 1 require Sereal::Encoder;
121 1 require version;
122
123 1 ▶ if( version->parse( Sereal::Encoder->VERSION ) <= version->parse( '4.023' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse("Sereal::Encoder"->VERSION) <= "version"->parse("4.023")) 0 1 1
124 {
125 0 CORE::return( [$class, \%hash] );
126 }
127 }
128 # But Storable want a list with the first element being the serialised element
129 2 CORE::return( $class, \%hash );
130 }
131  
132 1 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
133  
134 1 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
135  
136 # NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
137 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
138 sub THAW
139 2 ▶{
Subroutine THAW: called CC=10 SCAR=27.6
Pod undocumented
140 2 my( $self, undef, @args ) = @_;
141 2 ▶ my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 && ref $args[0] eq 'ARRAY' ? : 0 2 2
Condition33%1/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table33%1/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
142 2 ▶ my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && ref $ref eq 'ARRAY' && scalar @$ref > 1 ? : 2 0 2
Condition22%2/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
002
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
002
ref $self or $self
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
defined $ref ref $ref eq "ARRAY" scalar @$ref > 1
MC/DC0%0/2
ref $self or $self
ref $self $self
Truth table25%1/4
A and B and C
A= defined $refB= ref $ref eq "ARRAY"C= scalar @$ref > 1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
143 2 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 2 0 2
144 2 my $new;
145 # Storable pattern requires to modify the object it created rather than returning a new one
146 2 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 1 1 2
147 {
148 1 foreach( CORE::keys( %$hash ) )
149 {
150 2 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
151 }
152 1 $new = $self;
153 }
154 else
155 {
156 1 $new = CORE::bless( $hash => $class );
157 }
158 2 CORE::return( $new );
159 }
160  
161 22 1;
162  
163 __END__
« lib/Module/Generic/TieHash.pm ↑ Summary lib/Module/Generic/Boolean.pm »