| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## Module Generic - ~/lib/Module/Generic/Boolean.pm | ||||||||||||||||||||||||||||
| 3 | ## Version v1.2.2 | ||||||||||||||||||||||||||||
| 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 | package Module::Generic::Boolean; | ||||||||||||||||||||||||||||
| 14 | BEGIN | ||||||||||||||||||||||||||||
| 15 | 33 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||
| 16 | 33 | ▶ | use v5.16.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 33 | ▶ | use common::sense; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 33 | ▶ | use vars qw( $true $false ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | use overload | ||||||||||||||||||||||||||||
| 20 | 1358 | ▶ | "0+" => sub{ ${$_[0]} }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.8
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | "++" => sub{ $_[0] = ${$_[0]} + 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | "--" => sub{ $_[0] = ${$_[0]} - 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 33 | fallback => 1; | |||||||||||||||||||||||||||
| 24 | 33 | $true = do{ bless( \( my $dummy = 1 ) => 'Module::Generic::Boolean' ) }; | |||||||||||||||||||||||||||
| 25 | 33 | $false = do{ bless( \( my $dummy = 0 ) => 'Module::Generic::Boolean' ) }; | |||||||||||||||||||||||||||
| 26 | 33 | our( $VERSION ) = 'v1.2.2'; | |||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 33 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 15 | ▶ | sub new { return( $_[1] ? $true : $false ); } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # sub as_array { return( Module::Generic::Array->new( [ ${$_[0]} ] ) ); } | ||||||||||||||||||||||||||||
| 34 | sub as_array | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_array: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | unless( $self->_is_class_loaded( 'Module::Generic::Array' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | # try-catch | ||||||||||||||||||||||||||||
| 40 | 0 | local $@; | |||||||||||||||||||||||||||
| 41 | 0 | eval( 'Module::Generic::Array' ); | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | if( $@ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 0 | die( "Unable to load Module::Generic::Array" ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 0 | return( Module::Generic::Array->new( [ $$self ] ) ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # sub as_number { return( Module::Generic::Number->new( ${$_[0]} ) ); } | ||||||||||||||||||||||||||||
| 51 | sub as_number | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_number: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | unless( $self->_is_class_loaded( 'Module::Generic::Number' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | # try-catch | ||||||||||||||||||||||||||||
| 57 | 0 | local $@; | |||||||||||||||||||||||||||
| 58 | 0 | eval( 'Module::Generic::Number' ); | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | if( $@ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||
| 61 | 0 | die( "Unable to load Module::Generic::Number" ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | return( Module::Generic::Number->new( $$self ) ); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # sub as_scalar { return( Module::Generic::Scalar->new( ${$_[0]} ) ); } | ||||||||||||||||||||||||||||
| 68 | sub as_scalar | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
as_scalar: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | unless( $self->_is_class_loaded( 'Module::Generic::Scalar' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | # try-catch | ||||||||||||||||||||||||||||
| 74 | 0 | local $@; | |||||||||||||||||||||||||||
| 75 | 0 | eval( 'Module::Generic::Scalar' ); | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | if( $@ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | 0 | die( "Unable to load Module::Generic::Scalar" ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 0 | return( Module::Generic::Scalar->new( $$self ) ); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub defined { return(1); } | ||||||||||||||||||||||||||
|
Subroutine
defined: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 435 | ▶ | sub true () { $true } | ||||||||||||||||||||||||||
|
Subroutine
true: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 409 | ▶ | sub false () { $false } | ||||||||||||||||||||||||||
|
Subroutine
false: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub is_bool ($) { UNIVERSAL::isa( $_[0], 'Module::Generic::Boolean' ) } | ||||||||||||||||||||||||||
|
Subroutine
is_bool: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub is_true ($) { $_[0] && UNIVERSAL::isa( $_[0], 'Module::Generic::Boolean' ) } | ||||||||||||||||||||||||||
|
Subroutine
is_true: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub is_false ($) { !$_[0] && UNIVERSAL::isa( $_[0], 'Module::Generic::Boolean' ) } | ||||||||||||||||||||||||||
|
Subroutine
is_false: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub _is_class_loaded | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_is_class_loaded: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 95 | 0 | my $self = CORE::shift( @_ ); | |||||||||||||||||||||||||||
| 96 | 0 | my $class = CORE::shift( @_ ); | |||||||||||||||||||||||||||
| 97 | 0 | ( my $pm = $class ) =~ s{::}{/}gs; | |||||||||||||||||||||||||||
| 98 | 0 | $pm .= '.pm'; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | return(1) if( CORE::exists( $INC{ $pm } ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | return(0); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub FREEZE | ||||||||||||||||||||||||||||
| 104 | 24 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FREEZE: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 24 | my $self = CORE::shift( @_ ); | |||||||||||||||||||||||||||
| 106 | 24 | ▶ | my $serialiser = CORE::shift( @_ ) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift @_ // ""
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table50%1/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 107 | 24 | my $class = CORE::ref( $self ); | |||||||||||||||||||||||||||
| 108 | # Return an array reference rather than a list so this works with Sereal and CBOR | ||||||||||||||||||||||||||||
| 109 | # On or before Sereal version 4.023, Sereal did not support multiple values returned | ||||||||||||||||||||||||||||
| 110 | 24 | ▶ | if( $serialiser eq 'Sereal' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 4 | require Sereal::Encoder; | |||||||||||||||||||||||||||
| 113 | 4 | require version; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | if( version->parse( Sereal::Encoder->VERSION ) <= version->parse( '4.023' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||
| 117 | 0 | CORE::return( [$class, $$self] ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | # But Storable want a list with the first element being the serialised element | ||||||||||||||||||||||||||||
| 121 | 24 | CORE::return( $$self ); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 20 | ▶ | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_freeze: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 7 | ▶ | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_thaw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # 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. | ||||||||||||||||||||||||||||
| 129 | # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze | ||||||||||||||||||||||||||||
| 130 | sub THAW | ||||||||||||||||||||||||||||
| 131 | 11 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
THAW: called CC=5 SCAR=17.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | 11 | my( $self, undef, @args ) = @_; | |||||||||||||||||||||||||||
| 133 | 11 | my( $class, $str ); | |||||||||||||||||||||||||||
| 134 | 11 | ▶ | if( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||
| 136 | 0 | ( $class, $str ) = @{$args[0]}; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | else | ||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | 11 | ▶ | $class = CORE::ref( $self ) || $self; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 141 | 11 | $str = CORE::shift( @args ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | # Storable pattern requires to modify the object it created rather than returning a new one | ||||||||||||||||||||||||||||
| 144 | 11 | ▶ | if( CORE::ref( $self ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||
| 146 | 7 | $$self = $str; | |||||||||||||||||||||||||||
| 147 | 7 | CORE::return( $self ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | else | ||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 4 | CORE::return( $class->new( $str ) ); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub TO_JSON | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | # JSON does not check that the value is a proper true or false. It stupidly assumes this is a string | ||||||||||||||||||||||||||||
| 158 | # The only way to make it understand is to return a scalar ref of 1 or 0 | ||||||||||||||||||||||||||||
| 159 | # return( $_[0] ? 'true' : 'false' ); | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | return( $_[0] ? \1 : \0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 33 | 1; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||