lib/Module/Generic/Finfo.pm

filter: Statementstmt 94.6% 264 / 279 Branchbran 39.4% 67 / 170 Conditioncond 29.4% 10 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 95.5% 64 / 67 Podpod 87.7% 43 / 49 totaltotal 72.4% 448 / 618 CC 101 SCAR 25.1
CC
101
Cov
71%
_datetime
53.0
filetype
22.6
mode_n2s
16.7
CRAP 12.3
« lib/Module/Generic/Hash.pm ↑ Summary lib/Module/Generic/Number.pm »
1 ##----------------------------------------------------------------------------
2 ## Module Generic - ~/lib/Module/Generic/Finfo.pm
3 ## Version v0.5.7
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/05/20
7 ## Modified 2026/06/05
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::Finfo;
14 BEGIN
15 14 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶ use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 warnings::register_categories( 'Module::Generic' );
20 14 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 14 ▶ use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 14 ▶ use Module::Generic::Global ':const';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 14 ▶ use Module::Generic::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 14 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 14 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.2
27 0 ▶ q{""} => sub { $_[0]->{filepath} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 bool => sub () {1},
29 14 fallback => 1,
30 14 );
31 14 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 14 FINFO_DEV => 0,
33 FINFO_INODE => 1,
34 FINFO_MODE => 2,
35 FINFO_NLINK => 3,
36 FINFO_UID => 4,
37 FINFO_GID => 5,
38 FINFO_RDEV => 6,
39 FINFO_SIZE => 7,
40 FINFO_ATIME => 8,
41 FINFO_MTIME => 9,
42 FINFO_CTIME => 10,
43 FINFO_BLOCK_SIZE => 11,
44 FINFO_BLOCKS => 12,
45 # the file type is undetermined.
46 FILETYPE_NOFILE => 0,
47 # a file is a regular file.
48 FILETYPE_REG => 1,
49 # a file is a directory
50 FILETYPE_DIR => 2,
51 # a file is a character device
52 FILETYPE_CHR => 3,
53 # a file is a block device
54 FILETYPE_BLK => 4,
55 # a file is a FIFO or a pipe.
56 FILETYPE_PIPE => 5,
57 # a file is a symbolic link
58 FILETYPE_LNK => 6,
59 # a file is a [unix domain] socket.
60 FILETYPE_SOCK => 7,
61 # a file is of some other unknown type or the type cannot be determined.
62 FILETYPE_UNKFILE => 127,
63 14 };
64 14 our %EXPORT_TAGS = ( all => [qw( FILETYPE_NOFILE FILETYPE_REG FILETYPE_DIR FILETYPE_CHR FILETYPE_BLK FILETYPE_PIPE FILETYPE_LNK FILETYPE_SOCK FILETYPE_UNKFILE )] );
65 14 our @EXPORT_OK = qw( FILETYPE_NOFILE FILETYPE_REG FILETYPE_DIR FILETYPE_CHR FILETYPE_BLK FILETYPE_PIPE FILETYPE_LNK FILETYPE_SOCK FILETYPE_UNKFILE );
66 14 our $VERSION = 'v0.5.7';
67 };
68  
69 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 14 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 sub init
73 218 ▶{
Subroutine init: called CC=2 SCAR=7.0
Pod documented
74 218 my $self = shift( @_ );
75 218 ▶ my $file = shift( @_ ) || return( $self->error( "No file provided to instantiate a ", ref( $self ), " object." ) );
Condition50%1/2
shift @_ || (return $self->error('No file provided to instantiate a ', ref $self, ' object.'))
l!l
2180
MC/DC0%0/1
shift @_ || (return $self->error('No file provided to instantiate a ', ref $self, ' object.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
76 218 $self->{_init_strict_use_sub} = 1;
77 218 $self->SUPER::init( @_ );
78 218 $self->{filepath} = $file;
79 218 $self->{_data} = [CORE::stat( $file )];
80 218 return( $self );
81 }
82  
83 # This class does not convert to an HASH, but the TO_JSON method will convert to a string
84 0 ▶sub as_hash { return( $_[0] ); }
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
85  
86 sub atime
87 5 ▶{
Subroutine atime: called CC=3 SCAR=11.8
Pod documented
88 5 my $self = shift( @_ );
89 5 my $t;
90 5 my $data = $self->{_data};
91 5 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 5 5
want('OBJECT') ? : 0 0 0
92 5 $t = $data->[ FINFO_ATIME ];
93 5 return( $self->_datetime( $t ) );
94 }
95  
96 4 ▶sub blksize { return( shift->block_size( @_ ) ); }
Subroutine blksize: called CC=1 SCAR=0.0
Pod documented
97  
98 sub block_size
99 5 ▶{
Subroutine block_size: called CC=3 SCAR=12.5
Pod documented
100 5 my $self = shift( @_ );
101 5 my $data = $self->{_data};
102 5 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 5 5
want('OBJECT') ? : 0 0 0
103 5 return( $self->new_number( $data->[ FINFO_BLOCK_SIZE ] ) );
104 }
105  
106 sub blocks
107 4 ▶{
Subroutine blocks: called CC=3 SCAR=12.5
Pod documented
108 4 my $self = shift( @_ );
109 4 my $data = $self->{_data};
110 4 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 4 4
want('OBJECT') ? : 0 0 0
111 4 return( $self->new_number( $data->[ FINFO_BLOCKS ] ) );
112 }
113  
114 1 ▶sub can_exec { return( -x( shift->filepath ) ); }
Subroutine can_exec: called CC=1 SCAR=0.0
Pod documented
115  
116 1 ▶sub can_execute { return( -x( shift->filepath ) ); }
Subroutine can_execute: called CC=1 SCAR=0.0
Pod documented
117  
118 6 ▶sub can_read { return( -r( shift->filepath ) ); }
Subroutine can_read: called CC=1 SCAR=0.0
Pod documented
119  
120 2 ▶sub can_write { return( -w( shift->filepath ) ); }
Subroutine can_write: called CC=1 SCAR=0.0
Pod documented
121  
122 6 ▶sub csize { return( shift->size ); }
Subroutine csize: called CC=1 SCAR=0.0
Pod documented
123  
124 sub ctime
125 5 ▶{
Subroutine ctime: called CC=3 SCAR=11.8
Pod documented
126 5 my $self = shift( @_ );
127 5 my $t;
128 5 my $data = $self->{_data};
129 5 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 5 5
want('OBJECT') ? : 0 0 0
130 5 $t = $data->[ FINFO_CTIME ];
131 5 return( $self->_datetime( $t ) );
132 }
133  
134 4 ▶sub dev { return( shift->device( @_ ) ); }
Subroutine dev: called CC=1 SCAR=0.0
Pod documented
135  
136 sub device
137 16 ▶{
Subroutine device: called CC=3 SCAR=12.5
Pod documented
138 16 my $self = shift( @_ );
139 16 my $data = $self->{_data};
140 16 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 16 16
want('OBJECT') ? : 0 0 0
141 16 return( $self->new_number( $data->[ FINFO_DEV ] ) );
142 }
143  
144 306 ▶sub exists { return( shift->filetype == FILETYPE_NOFILE ? 0 : 1 ); }
Subroutine exists: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->filetype == 0 ? : 136 170 306
145  
146 ## Read-only
147 103 ▶sub filepath { return( shift->_set_get_scalar( 'filepath' ) ); }
Subroutine filepath: called CC=1 SCAR=0.0
Pod documented
148  
149 sub filetype
150 2934 ▶{
Subroutine filetype: called CC=9 SCAR=31.2
Pod documented
151 2934 my $self = shift( @_ );
152 2934 my $file = $self->{filepath};
153 2934 CORE::stat( $file );
154 2934 ▶ if( !-e( _ ) )
Branch: 8 branches
BranchTrueFalseTotal
if (not -e _) { } 170 2764 2934
elsif (-l $file) { } 0 2764 2764
elsif (-f _) { } 2593 171 2764
elsif (-d _) { } 171 0 171
elsif (-p _) { } 0 0 0
elsif (-S _) { } 0 0 0
elsif (-b _) { } 0 0 0
elsif (-c _) { } 0 0 0
155 {
156 170 return( FILETYPE_NOFILE );
157 }
158 elsif( -l( $file ) )
159 {
160 0 return( FILETYPE_LNK );
161 }
162 elsif( -f( _ ) )
163 {
164 2593 return( FILETYPE_REG );
165 }
166 elsif( -d( _ ) )
167 {
168 171 return( FILETYPE_DIR );
169 }
170 elsif( -p( _ ) )
171 {
172 0 return( FILETYPE_PIPE );
173 }
174 elsif( -S( _ ) )
175 {
176 0 return( FILETYPE_SOCK );
177 }
178 elsif( -b( _ ) )
179 {
180 0 return( FILETYPE_BLK );
181 }
182 elsif( -c( _ ) )
183 {
184 0 return( FILETYPE_CHR );
185 }
186 else
187 {
188 0 return( FILETYPE_UNKFILE );
189 }
190 }
191  
192 sub fname
193 2 ▶{
Subroutine fname: called CC=1 SCAR=0.0
Pod documented
194 2 my $self = shift( @_ );
195 2 return( $self->{filepath} );
196 }
197  
198 sub gid
199 5 ▶{
Subroutine gid: called CC=3 SCAR=12.5
Pod documented
200 5 my $self = shift( @_ );
201 5 my $data = $self->{_data};
202 5 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 5 5
want('OBJECT') ? : 0 0 0
203 5 return( $self->new_number( $data->[ FINFO_GID ] ) );
204 }
205  
206 sub group
207 1 ▶{
Subroutine group: called CC=4 SCAR=15.2
Pod documented
208 1 my $self = shift( @_ );
209 1 my $data = $self->{_data};
210 1 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 1 1
want('OBJECT') ? : 0 0 0
211 # perlport: "getgrgid: (Win32, VMS, RISC OS) Not implemented."
212 1 ▶ return( $self->gid ) if( $^O =~ /^(win32|vms|riscos)$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /^(win32|vms|riscos)$/i 0 1 1
213 1 my $name = scalar( getgrgid( $data->[ FINFO_GID ] ) );
214 1 return( $self->new_scalar( scalar( getgrgid( $data->[ FINFO_GID ] ) ) ) );
215 }
216  
217 4 ▶sub ino { return( shift->inode( @_ ) ); }
Subroutine ino: called CC=1 SCAR=0.0
Pod documented
218  
219 sub inode
220 16 ▶{
Subroutine inode: called CC=3 SCAR=12.5
Pod documented
221 16 my $self = shift( @_ );
222 16 my $data = $self->{_data};
223 16 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 16 16
want('OBJECT') ? : 0 0 0
224 16 return( $self->new_number( $data->[ FINFO_INODE ] ) );
225 }
226  
227 1 ▶sub is_block { return( shift->filetype == FILETYPE_BLK ); }
Subroutine is_block: called CC=1 SCAR=0.0
Pod documented
228  
229 1 ▶sub is_char { return( shift->filetype == FILETYPE_CHR ); }
Subroutine is_char: called CC=1 SCAR=0.0
Pod documented
230  
231 2611 ▶sub is_dir { return( shift->filetype == FILETYPE_DIR ); }
Subroutine is_dir: called CC=1 SCAR=0.0
Pod documented
232  
233 10 ▶sub is_file { return( shift->filetype == FILETYPE_REG ); }
Subroutine is_file: called CC=1 SCAR=0.0
Pod documented
234  
235 1 ▶sub is_link { return( shift->filetype == FILETYPE_LNK ); }
Subroutine is_link: called CC=1 SCAR=0.0
Pod documented
236  
237 1 ▶sub is_pipe { return( shift->filetype == FILETYPE_PIPE ); }
Subroutine is_pipe: called CC=1 SCAR=0.0
Pod documented
238  
239 1 ▶sub is_socket { return( shift->filetype == FILETYPE_SOCK ); }
Subroutine is_socket: called CC=1 SCAR=0.0
Pod documented
240  
241 sub mime_type
242 1 ▶{
Subroutine mime_type: called CC=3 SCAR=11.2
Pod documented
243 1 my $self = shift( @_ );
244 1 my $file = $self->filepath;
245 1 ▶ $self->_load_class( 'Module::Generic::File::Magic' ) || die( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Module::Generic::File::Magic") or die $self->pass_error 1 0 1
246 1 my $magic = Module::Generic::File::Magic->new( flags => Module::Generic::File::Magic::MAGIC_MIME_TYPE() );
247 1 ▶ my $type = $magic->from_file( "$file" ) ||
Condition50%1/2
$magic->from_file("$file") || (return $self->error("Error getting the file $file mime-type: ", $magic->error))
l!l
10
MC/DC0%0/1
$magic->from_file("$file") || (return $self->error("Error getting the file $file mime-type: ", $magic->error))
$magic->from_file("$file")
Truth table50%1/2
A
A= $magic->from_file("$file")
Aresult
11
00
248 return( $self->error( "Error getting the file $file mime-type: ", $magic->error ) );
249 1 return( $self->new_scalar( $type ) );
250 }
251  
252 sub mode
253 6 ▶{
Subroutine mode: called CC=3 SCAR=12.5
Pod documented
254 6 my $self = shift( @_ );
255 6 my $data = $self->{_data};
256 6 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 6 6
want('OBJECT') ? : 0 0 0
257 6 return( $self->new_number( $data->[ FINFO_MODE ] & 07777 ) );
258 }
259  
260 sub mtime
261 5 ▶{
Subroutine mtime: called CC=3 SCAR=11.8
Pod documented
262 5 my $self = shift( @_ );
263 5 my $t;
264 5 my $data = $self->{_data};
265 5 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 5 5
want('OBJECT') ? : 0 0 0
266 5 $t = $data->[ FINFO_MTIME ];
267 5 return( $self->_datetime( $t ) );
268 }
269  
270 1 ▶sub name { return( File::Basename::basename( shift->fname ) ); }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
271  
272 sub nlink
273 5 ▶{
Subroutine nlink: called CC=3 SCAR=12.5
Pod documented
274 5 my $self = shift( @_ );
275 5 my $data = $self->{_data};
276 5 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 5 5
want('OBJECT') ? : 0 0 0
277 5 return( $self->new_number( $data->[ FINFO_NLINK ] ) );
278 }
279  
280 sub permission
281 1 ▶{
Subroutine permission: called CC=3 SCAR=12.5
Pod documented
282 1 my $self = shift( @_ );
283 1 my $data = $self->{_data};
284 1 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 1 1
want('OBJECT') ? : 0 0 0
285 1 return( $self->new_number( sprintf( '%04o', $data->[ FINFO_MODE ] & 07777 ) ) );
286 }
287  
288 sub protection
289 1 ▶{
Subroutine protection: called CC=3 SCAR=12.5
Pod undocumented
290 1 my $self = shift( @_ );
291 1 my @stat = CORE::stat( $self->filepath );
292 1 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @stat ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @stat 0 1 1
want('OBJECT') ? : 0 0 0
293 1 return( $self->new_number( hex( sprintf( '%04o', $stat[2] & 07777 ) ) ) );
294 }
295  
296 sub rdev
297 1 ▶{
Subroutine rdev: called CC=3 SCAR=12.5
Pod documented
298 1 my $self = shift( @_ );
299 1 my $data = $self->{_data};
300 1 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 1 1
want('OBJECT') ? : 0 0 0
301 1 return( $self->new_number( $data->[ FINFO_RDEV ] ) );
302 }
303  
304 sub reset
305 87 ▶{
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
306 87 my $self = shift( @_ );
307 87 my $file = $self->filepath;
308 87 $self->{_data} = [CORE::stat( $file )];
309 87 return( $self );
310 }
311  
312 sub size
313 33 ▶{
Subroutine size: called CC=3 SCAR=12.5
Pod documented
314 33 my $self = shift( @_ );
315 33 my $data = $self->{_data};
316 33 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 33 33
want('OBJECT') ? : 0 0 0
317 33 return( $self->new_number( $data->[ FINFO_SIZE ] ) );
318 }
319  
320 sub stat
321 1 ▶{
Subroutine stat: called CC=2 SCAR=7.0
Pod documented
322 1 my $self = shift( @_ );
323 1 my $file = shift( @_ );
324 1 ▶ my $p = scalar( @_ ) ? { @_ } : {};
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 0 1 1
325 1 return( $self->new( $file, $p ) );
326 }
327  
328 sub uid
329 6 ▶{
Subroutine uid: called CC=3 SCAR=12.5
Pod documented
330 6 my $self = shift( @_ );
331 6 my $data = $self->{_data};
332 6 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 6 6
want('OBJECT') ? : 0 0 0
333 6 return( $self->new_number( $data->[ FINFO_UID ] ) );
334 }
335  
336 sub user
337 1 ▶{
Subroutine user: called CC=4 SCAR=15.6
Pod documented
338 1 my $self = shift( @_ );
339 1 my $data = $self->{_data};
340 1 ▶ return( want( 'OBJECT' ) ? Module::Generic::Null->new : '' ) if( !scalar( @$data ) );
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @$data 0 1 1
want('OBJECT') ? : 0 0 0
341 # perlport: "getpwuid: (Win32) Not implemented. (RISC OS) Not useful.
342 1 ▶ return( $self->uid ) if( $^O =~ /^(win32|riscok)/i );
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /^(win32|riscok)/i 0 1 1
343 1 return( $self->new_scalar( scalar( getpwuid( $data->[ FINFO_UID ] ) ) ) );
344 }
345  
346 sub DESTROY
347 212 ▶{
Subroutine DESTROY: called CC=3 SCAR=11.2
348 # <https://perldoc.perl.org/perlobj#Destructors>
349 212 CORE::local( $., $@, $!, $^E, $? );
350 212 ▶ CORE::return if( Module::Generic::_in_global_destruction() );
Branch: 1 branch
BranchTrueFalseTotal
if Module::Generic::_in_global_destruction() 0 212 212
351 212 my $self = CORE::shift( @_ );
352 212 ▶ CORE::return if( !CORE::defined( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 212 212
353 212 my $repo = Module::Generic::Global->new( 'local_tz' => 'system' );
354 212 $repo->cleanup;
355 };
356  
357 sub FREEZE
358 2 ▶{
Subroutine FREEZE: called CC=6 SCAR=18.3
Pod undocumented
359 2 my $self = CORE::shift( @_ );
360 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
361 2 my $class = CORE::ref( $self );
362 2 my @props = qw( filepath _data );
363 2 my $hash = {};
364 2 foreach my $prop ( @props )
365 {
366 4 ▶ if( CORE::exists( $self->{ $prop } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$prop}) 4 0 4
367 {
368 4 $hash->{ $prop } = $self->{ $prop };
369 }
370 }
371 # Return an array reference rather than a list so this works with Sereal and CBOR
372 # On or before Sereal version 4.023, Sereal did not support multiple values returned
373 2 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 1 1 2
374 {
375 1 require Sereal::Encoder;
376 1 require version;
377
378 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
379 {
380 0 CORE::return( [$class, $hash] );
381 }
382 }
383 # But Storable want a list with the first element being the serialised element
384 2 CORE::return( $class, $hash );
385 }
386  
387 1 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
388  
389 1 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
390  
391 # 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.
392 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
393 sub THAW
394 2 ▶{
Subroutine THAW: called CC=10 SCAR=27.6
Pod undocumented
395 2 my( $self, undef, @args ) = @_;
396 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
397 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
398 2 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 2 0 2
399 2 my $new;
400 # Storable pattern requires to modify the object it created rather than returning a new one
401 2 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 1 1 2
402 {
403 1 foreach( CORE::keys( %$hash ) )
404 {
405 2 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
406 }
407 1 $new = $self;
408 }
409 else
410 {
411 1 $new = CORE::bless( $hash => $class );
412 }
413 2 CORE::return( $new );
414 }
415  
416 0 ▶sub TO_JSON { CORE::return( CORE::shift->filepath ); }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
417  
418 sub _datetime
419 15 ▶{
Subroutine _datetime: called CC=22 SCAR=39.7
420 15 my $self = shift( @_ );
421 15 my $t = shift( @_ );
422 15 ▶ return( $self->error( "No epoch time was provided." ) ) if( !length( $t ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $t 0 15 15
423 15 ▶ return( $self->error( "Invalid epoch time provided \"$t\"." ) ) if( $t !~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $t =~ /^\d+$/ 0 15 15
424 15 ▶ my $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
425 15 ▶ $self->_load_class( 'DateTime::Lite' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("DateTime::Lite") or return $self->pass_error 15 0 15
426 15 ▶ $self->_load_class( 'DateTime::Format::Lite' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("DateTime::Format::Lite") or return $self->pass_error 15 0 15
427 15 ▶ $self->_load_class( 'Module::Generic::DateTime' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Module::Generic::DateTime") or return $self->pass_error 15 0 15
428 # my $err_key = $class;
429 # Previous approach
430 # my $repo = Module::Generic::Global->new( 'local_tz' => $class, key => $err_key );
431 # New system-wide shared approach
432 15 my $repo = Module::Generic::Global->new( 'local_tz' => 'system' );
433 15 my $has_local_tz = $repo->get;
434 15 my $dt;
435 # NOTE: Only the boolean capability flag is cached in the shared repository. The
436 # DateTime::Lite object itself is always built locally, further below, from the
437 # resolved flag. A previous implementation built $dt only inside the locked
438 # initialisation block, so any thread that acquired the lock after another thread
439 # had already populated the flag would skip the inner block entirely and leave $dt
440 # undefined. That undefined value then died on ->set_formatter, which made atime()
441 # and its siblings return a false value under ithreads in a non-deterministic,
442 # scheduling-dependent way.
443 15 ▶ if( !defined( $has_local_tz ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $has_local_tz) 3 12 15
444 {
445 3 $repo->lock;
446 # Double-check now that we hold the lock.
447 3 $has_local_tz = $repo->get;
448 3 ▶ if( !defined( $has_local_tz ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $has_local_tz) 3 0 3
449 {
450 # try-catch
451 3 local $@;
452 # We only need to know whether the local time zone resolves. The probe
453 # object is discarded; the real object is built once, after the lock.
454 my $dt_test = eval
455 3 {
456 3 DateTime::Lite->from_epoch( epoch => $t, time_zone => 'local' );
457 };
458 # Either an unexpected error has occurred, or DateTime::Lite has returned an error
459 3 ▶ if( $@ || !defined( $dt_test ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $dt_test) { } 0 3 3
Condition33%1/3
$@ or not defined $dt_test
l!l&&r!l&&!r
003
MC/DC0%0/2
$@ or not defined $dt_test
$@ not defined $dt_test
Truth table33%1/3
A or not(B)
A= $@B= not defined $dt_test
ABresult
1X1
011
000
460 {
461 0 $has_local_tz = 0;
462 0 ▶ warn( "Your system is missing key timezone components. ${class}::_datetime is reverting to UTC instead of local time zone." ) if( $self->_is_warnings_enabled( 'Module::Generic' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("Module::Generic") 0 0 0
463 }
464 else
465 {
466 3 $has_local_tz = 1;
467 }
468 3 $repo->set( $has_local_tz );
469 # unlocks automatically at end of scope.
470 }
471 # Not strictly necessary
472 3 $repo->unlock;
473 }
474  
475 # NOTE: Build the object from the resolved flag, unconditionally, so that every
476 # thread produces its own object regardless of which thread won the initialisation
477 # race above.
478 # try-catch
479 15 local $@;
480 eval
481 15 {
482 15 ▶ $dt = DateTime::Lite->from_epoch( epoch => $t, time_zone => ( $has_local_tz ? 'local' : 'UTC' ) );
Branch: 1 branch
BranchTrueFalseTotal
$has_local_tz ? : 15 0 15
483 };
484 15 ▶ if( $@ || !defined( $dt ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $dt) 0 15 15
Condition33%1/3
$@ or not defined $dt
l!l&&r!l&&!r
0015
MC/DC0%0/2
$@ or not defined $dt
$@ not defined $dt
Truth table33%1/3
A or not(B)
A= $@B= not defined $dt
ABresult
1X1
011
000
485 {
486 0 ▶ warn( "Error trying to set a DateTime::Lite object using ", ( $has_local_tz ? 'local' : 'UTC' ), " time zone: ", ( $@ || DateTime::Lite->error || 'unknown error' ) ) if( $self->_is_warnings_enabled( 'Module::Generic' ) );
Branch: 2 branches
BranchTrueFalseTotal
if $self->_is_warnings_enabled("Module::Generic") 0 0 0
$has_local_tz ? : 0 0 0
Condition0%0/5
$@ || 'DateTime::Lite'->error || 'unknown error'
l!l
00
$@ or "DateTime::Lite"->error
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || 'DateTime::Lite'->error || 'unknown error'
$@ "DateTime::Lite"->error
Truth table0%0/3
A or B
A= $@B= "DateTime::Lite"->error
ABresult
1X1
011
000
487 0 $dt = DateTime::Lite->from_epoch( epoch => $t, time_zone => 'UTC' );
488 }
489 15 ▶ return( $self->error( "Could not build a DateTime::Lite object for epoch \"$t\"." ) ) if( !defined( $dt ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dt 0 15 15
490  
491 # try-catch
492 15 my $o;
493 eval
494 15 {
495 15 my $fmt = DateTime::Format::Lite->new(
496 pattern => '%s',
497 );
498 15 $dt->set_formatter( $fmt );
499 15 ▶ $o = Module::Generic::DateTime->new( $dt ) ||
Condition50%1/2
'Module::Generic::DateTime'->new($dt) || (return $self->pass_error('Module::Generic::DateTime'->error))
l!l
150
MC/DC0%0/1
'Module::Generic::DateTime'->new($dt) || (return $self->pass_error('Module::Generic::DateTime'->error))
'Module::Generic::DateTime'->new($dt)
Truth table50%1/2
A
A= 'Module::Generic::DateTime'->new($dt)
Aresult
11
00
500 return( $self->pass_error( Module::Generic::DateTime->error ) );
501 };
502 15 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 15 15
503 {
504 0 return( $self->error( "Unable to get the datetime object for \"$t\": $@" ) );
505 }
506 15 return( $o );
507 }
508  
509 # Credits: IPC::SysV <https://metacpan.org/release/JACKS/IPC_SysV>
510 sub mode_s2n
511 1 ▶{
Subroutine mode_s2n: called CC=3 SCAR=11.1
Pod documented
512 1 my $self = shift( @_ );
513 1 my $mode = shift( @_ );
514  
515 my $s2n = sub
516 3 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
517 3 my $str = shift( @_ );
518 # From string to binary
519 3 $str =~ tr/\-a-z/01/;
520 # Convert from binary into an octal digit.
521 3 $str = oct( unpack( 'N', pack( 'B32', substr( '0' x 32 . $str, -32 ) ) ) );
522 3 return( $str );
523 1 };
524  
525 # Just in case its a number already
526 1 ▶ $mode =~ /^\d+$/ && return( $mode + 0 );
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ /^\d+$/ and return $mode + 0 0 1 1
527  
528 # $mode =~ /^([r\-])([w\-])\-([r\-])([w\-])\-([r\-])([w\-])\-$/ || return;
529 1 ▶ $mode =~ /^(?<is_dir>[d\-]?)(?<user>[r\-][w\-][x\-])(?<group>[r\-][w\-][x\-])(?<other>[r\-][w\-][x\-])$/ || return;
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ /^(?<is_dir>[d\-]?)(?<user>[r\-][w\-][x\-])(?<group>[r\-][w\-][x\-])(?<other>[r\-][w\-][x\-])$/ or return 1 0 1
530  
531 1 my $n_mode = 0 . $s2n->( $+{user} ) . $s2n->( $+{group} ) . $s2n->( $+{other} );
532 # $n_mode |= 00400 if( $1 eq 'r' );
533 # $n_mode |= 00200 if( $2 eq 'w' );
534 # $n_mode |= 00040 if( $3 eq 'r' );
535 # $n_mode |= 00020 if( $4 eq 'w' );
536 # $n_mode |= 00004 if( $5 eq 'r' );
537 # $n_mode |= 00002 if( $6 eq 'w' );
538 1 return( $n_mode );
539 }
540  
541 sub mode_n2s
542 1 ▶{
Subroutine mode_n2s: called CC=13 SCAR=28.1
Pod documented
543 1 my $self = shift( @_ );
544 1 my $n_mode = shift( @_ );
545  
546 # Just in case its a string already
547 # $n_mode =~ /^[r\-][w\-]\-[r\-][w\-]\-[r\-][w\-]\-$/ && return( $n_mode );
548 1 ▶ $n_mode =~ /^([d\-]?)([r\-][w\-][x\-]){3}$/ && return( $n_mode );
Branch: 1 branch
BranchTrueFalseTotal
$n_mode =~ /^([d\-]?)([r\-][w\-][x\-]){3}$/ and return $n_mode 0 1 1
549  
550 # $n_mode =~ /^\d+$/ || return;
551 # ( $n_mode =~ /^0?\d{3}$/ && $n_mode !~ /[89]/ ) || return;
552  
553 # Convert numeric string to octal value
554 1 ▶ if( $n_mode =~ /^[0-7]{3,4}$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($n_mode =~ /^[0-7]{3,4}$/) 1 0 1
555 {
556 1 $n_mode = oct( $n_mode );
557 }
558  
559 # Ensure it's a number now
560 1 ▶ return unless( $n_mode =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $n_mode =~ /^\d+$/ 0 1 1
561  
562 1 my @ftype = ('', 'p', 'c', '?', 'd', '?', 'b', '?', '-', '?', 'l', '?', 's', 'D', '?', '?');
563 1 my $ftype = $ftype[ ( $n_mode & 0170000 ) >> 12 ];
564  
565 1 my $mode = '';
566 1 ▶ $mode .= ( $n_mode & 00400 ) ? 'r' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 256 ? : 1 0 1
567 1 ▶ $mode .= ( $n_mode & 00200 ) ? 'w' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 128 ? : 1 0 1
568 1 ▶ $mode .= ( $n_mode & 00100 ) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 64 ? : 1 0 1
569 1 ▶ $mode .= ( $n_mode & 00040 ) ? 'r' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 32 ? : 1 0 1
570 1 ▶ $mode .= ( $n_mode & 00020 ) ? 'w' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 16 ? : 0 1 1
571 1 ▶ $mode .= ( $n_mode & 00010 ) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 8 ? : 1 0 1
572 1 ▶ $mode .= ( $n_mode & 00004 ) ? 'r' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 4 ? : 1 0 1
573 1 ▶ $mode .= ( $n_mode & 00002 ) ? 'w' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 2 ? : 0 1 1
574 1 ▶ $mode .= ( $n_mode & 00001 ) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$n_mode & 1 ? : 1 0 1
575  
576 1 return( $ftype . $mode );
577 }
578  
579 14 1;
580 # NOTE: POD
581 __END__
« lib/Module/Generic/Hash.pm ↑ Summary lib/Module/Generic/Number.pm »