Acrux/Digest.pm

filter: Statementstmt 64.0% 32 / 50 Branchbran 14.2% 2 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 10 / 15 Podpod 100% 9 / 9 totaltotal 56.9% 53 / 93 CC 9 SCAR 32.6
CC
9
Cov
51%
addfile
72.0
data
2.0
hexdigest
2.0
CRAP 26.0
« Acrux/Config.pm ↑ Summary Acme/Crux/Plugin/Config.pm »
1 package Acrux::Digest;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 Acrux::Digest - Acrux Digest base class
10  
11 =head1 SYNOPSIS
12  
13 use parent qw/Acrux::Digest/;
14  
15 =head1 DESCRIPTION
16  
17 Acrux Digest base class
18  
19 =head2 new
20  
21 my $provider = Acrux::Digest::Provider->new();
22  
23 Returns Digest Provider instance
24  
25 =head1 ATTRIBUTES
26  
27 This class implements the following attributes
28  
29 =head2 data
30  
31 my $data = $provider->data;
32 $provider = $provider->data({foo => 'bar'});
33  
34 Data structure to be processed
35  
36 =head1 METHODS
37  
38 This class implements the following methods
39  
40 =head2 add
41  
42 $provider->add("data", "and another data", ...);
43  
44 Add data to digest calculate.
45 All specified data of array will be concatenated to one pool of data
46  
47 =head2 addfile
48  
49 $provider->addfile("/path/of/file");
50  
51 Add file content to data pool
52  
53 $provider->addfile(*STDIN);
54  
55 Add STDIN content to data pool
56  
57 =head2 digest
58  
59 my $digest = $provider->digest;
60  
61 Returns result digest (as is)
62  
63 =head2 hexdigest
64  
65 my $digest = $provider->hexdigest;
66  
67 Returns sesult digest as hex string
68  
69 =head2 b64digest, base64digest
70  
71 my $digest = $provider->b64digest;
72  
73 Returns sesult digest as b64 string
74  
75 =head2 reset
76  
77 $provider->reset;
78  
79 Reset data (set to "")
80  
81 =head1 HISTORY
82  
83 See C<Changes> file
84  
85 =head1 TO DO
86  
87 See C<TODO> file
88  
89 =head1 SEE ALSO
90  
91 L<Digest>
92  
93 =head1 AUTHOR
94  
95 Serż Minus (Sergey Lepenkov) L<https://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
96  
97 =head1 COPYRIGHT
98  
99 Copyright (C) 1998-2026 D&D Corporation
100  
101 =head1 LICENSE
102  
103 This program is distributed under the terms of the Artistic License Version 2.0
104  
105 See the C<LICENSE> file or L<https://opensource.org/license/artistic-2-0> for details
106  
107 =cut
108  
109 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
110 3 BUFFER_SIZE => 4*1024, # 4kB
111 3 };
112  
113 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 3 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
119 3 my $class = shift;
120 3 return bless {
121 data => '',
122 }, $class;
123 }
124 24 ▶sub data {
Subroutine data: called CC=2 SCAR=6.9
Pod documented
125 24 my $self = shift;
126 24 ▶ if (scalar(@_) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 1) 10 14 24
127 10 $self->{data} = shift;
128 10 return $self;
129 }
130 14 return $self->{data};
131 }
132 3 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
133 3 my $self = shift;
134 3 $self->{data} .= join('', @_);
135 3 return $self;
136 }
137 0 ▶sub addfile {
Subroutine addfile: not called CC=8 SCAR=42.8
Pod documented
138 0 my $self = shift;
139 0 my $fh = shift;
140 0 ▶ return $self unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
141 0 ▶ if (!ref($fh) && ref(\$fh) ne "GLOB") {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $fh and ref \$fh ne "GLOB") 0 0 0
Condition0%0/3
not ref $fh and ref \$fh ne "GLOB"
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $fh and ref \$fh ne "GLOB"
not ref $fh ref \$fh ne "GLOB"
Truth table0%0/3
not(A) and B
A= not ref $fhB= ref \$fh ne "GLOB"
ABresult
0X0
100
111
142 0 $fh = IO::File->new($fh, "r");
143 0 ▶ return $self unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
144 }
145 0 ▶ $fh->binmode() or croak(sprintf("Can't switch to binmode: %s", $!));
Branch: 1 branch
BranchTrueFalseTotal
$fh->binmode or croak(sprintf(q[Can't switch to binmode: %s], $!)) 0 0 0
146 0 my $buf;
147 0 ▶ while ($fh->read($buf, BUFFER_SIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->read($buf, 4096)) 0 0 0
148 0 $self->add($buf);
149 }
150 0 ▶ $fh->close() or croak(sprintf("Can't close file: %s", $!));
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak(sprintf(q[Can't close file: %s], $!)) 0 0 0
151 0 return $self;
152 }
153 2 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
154 2 my $self = shift;
155 2 $self->{data} = "";
156 2 return $self;
157 }
158 0 ▶sub hexdigest {
Subroutine hexdigest: not called CC=1 SCAR=6.9
Pod documented
159 0 my $self = shift;
160 0 return unpack("H*", $self->digest(@_));
161 }
162 0 ▶sub base64digest {
Subroutine base64digest: not called CC=1 SCAR=6.9
Pod documented
163 0 my $self = shift;
164 0 return encode_base64($self->digest(@_), "");
165 }
166 0 ▶sub b64digest { goto &base64digest }
Subroutine b64digest: not called CC=1 SCAR=6.9
Pod documented
167 0 ▶sub digest { croak 'Method "digest" not implemented by subclass' };
Subroutine digest: not called CC=1 SCAR=6.9
Pod documented
168  
169 3 1;
170  
171 __END__
« Acrux/Config.pm ↑ Summary Acme/Crux/Plugin/Config.pm »