blib/lib/Sys/OsRelease.pm

filter: Statementstmt 83.5% 168 / 201 Branchbran 71.2% 57 / 80 Conditioncond 60.0% 9 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 84.6% 33 / 39 Podpod 77.7% 14 / 18 totaltotal 78.4% 284 / 362 CC 53 SCAR 19.5
CC
53
Cov
79%
_load_file
20.9
platform
12.0
_new_instance
6.6
CRAP 7.0
↑ Summary
1 # Sys::OsRelease
2 # ABSTRACT: read operating system details from standard /etc/os-release file
3 # Copyright (c) 2022-2026 by Ian Kluft
4 # Open Source license Perl's Artistic License 2.0: <http://www.perlfoundation.org/artistic_license_2_0>
5 # SPDX-License-Identifier: Artistic-2.0
6  
7 # This module must be maintained for minimal dependencies so it can be used to build systems and containers.
8  
9 ## no critic (Modules::RequireExplicitPackage)
10 # This resolves conflicting Perl::Critic rules which want package and strictures each before the other
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 ## use critic (Modules::RequireExplicitPackage)
15  
16 package Sys::OsRelease;
17 3 $Sys::OsRelease::VERSION = '0.5.1';
18 3 ▶use if $] >= 5.016, "feature", "fc"; # retain even though 5.22 is minimum Perl, same code in ...::Lite
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # the instance - use Sys::OsRelease->instance() to get it
24 3 my %_instances = ();
25  
26 # default search path and file name for os-release file
27 3 my @std_search_path = qw(/etc /usr/lib /run/host);
28 3 my $std_file_name = "os-release";
29  
30 # defined attributes from FreeDesktop's os-release standard - this needs to be kept up-to-date with the standard
31 3 my @std_attrs = qw(
32 NAME ID ID_LIKE PRETTY_NAME FANCY_NAME CPE_NAME VARIANT VARIANT_ID VERSION VERSION_ID VERSION_CODENAME
33 BUILD_ID IMAGE_ID IMAGE_VERSION RELEASE_TYPE HOME_URL DOCUMENTATION_URL SUPPORT_URL BUG_REPORT_URL
34 PRIVACY_POLICY_URL SUPPORT_END LOGO ANSI_COLOR ANSI_COLOR_REVERSE VENDOR_NAME VENDOR_URL EXPERIMENT
35 EXPERIMENT_URL DEFAULT_HOSTNAME ARCHITECTURE SYSEXT_LEVEL CONFEXT_LEVEL SYSEXT_SCOPE CONFEXT_SCOPE
36 PORTABLE_PREFIXES PORTABLE_SCOPE
37 );
38  
39 # OS ID strings which are preferred as common if found in ID_LIKE
40 3 my %common_id = (
41 alpine => 1,
42 arch => 1,
43 fedora => 1,
44 debian => 1,
45 opensuse => 1,
46 );
47  
48 # call destructor when program ends
49 3 ▶END {
Subroutine END: called CC=2 SCAR=6.9
50 3 foreach my $class (keys %_instances) {
51 1 $class->clear_instance();
52 }
53 3 undef %_instances;
54 }
55  
56 #
57 # singleton management methods
58 # These can be imported by another class by using the import_singleton() method. That was done for Sys::OsPackage,
59 # to avoid copying those methods. But other classes with a similar need to minimize module dependencies which already
60 # use Sys::OsRelease can do this too.
61 #
62  
63 # alternative method to initiate initialization without returning a value
64 sub init
65 0 ▶{
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
66 0 my ($class, @params) = @_;
67 0 $class->instance(@params);
68 0 return;
69 }
70  
71 # exported to caller to provide the instance object whether it loaded Sys::OsRelease or Sys::OsRelease::Lite
72 sub osrelease
73 0 ▶{
Subroutine osrelease: not called CC=1 SCAR=6.9
Pod documented
74 0 return __PACKAGE__->instance();
75 }
76  
77 # import function called by "use" directive
78 # all parameters directed to init() so configs may be set on module load
79 sub import
80 0 ▶{
Subroutine import: not called CC=1 SCAR=2.9
81 0 my ($class, @params) = @_;
82 0 $class->init(@params);
83  
84 {
85 # export osrelease() to caller to get the instance whether they loaded Sys::OsRelease or Sys::OsRelease::Lite
86 0 my $callpkg = caller;
87 ## no critic (TestingAndDebugging::ProhibitNoStrict)
88 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 0 *{ $callpkg . "::osrelease" } = \&osrelease;
90 }
91  
92 0 return;
93 }
94  
95 # new method calls instance
96 sub new
97 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
98 0 my ($class, @params) = @_;
99 0 return $class->instance(@params);
100 }
101  
102 # singleton class instance
103 sub instance
104 31 ▶{
Subroutine instance: called CC=2 SCAR=6.9
Pod documented
105 31 my ($class, @params) = @_;
106  
107 # initialize if not already done
108 31 ▶ if (not $class->defined_instance()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->defined_instance) 12 19 31
109 12 $_instances{$class} = $class->_new_instance(@params);
110 }
111  
112 # return singleton instance
113 31 return $_instances{$class};
114 }
115  
116 # test if instance is defined for testing
117 sub defined_instance
118 45 ▶{
Subroutine defined_instance: called CC=3 SCAR=11.1
Pod documented
119 45 my $class = shift;
120 45 ▶ return ((exists $_instances{$class}) and $_instances{$class}->isa($class)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $_instances{$class} && $_instances{$class}->isa($class) ? : 31 14 45
Condition66%2/3
exists $_instances{$class} and $_instances{$class}->isa($class)
!ll&&!rl&&r
14031
MC/DC50%1/2
exists $_instances{$class} and $_instances{$class}->isa($class)
exists $_instances{$class} $_instances{$class}->isa($class)
Truth table66%2/3
A and B
A= exists $_instances{$class}B= $_instances{$class}->isa($class)
ABresult
0X0
100
111
121 }
122  
123 # clear instance for exit-cleanup or for re-use in testing
124 sub clear_instance
125 12 ▶{
Subroutine clear_instance: called CC=3 SCAR=11.2
Pod documented
126 12 my $class = shift;
127 12 ▶ if ($class->defined_instance()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->defined_instance) 12 0 12
128 # clean up anything that the destructor will miss, such as auto-generated methods
129 12 ▶ if ($class->can("_cleanup_instance")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("_cleanup_instance")) 12 0 12
130 12 $class->_cleanup_instance();
131 }
132  
133 # dereferencing will destroy singleton instance
134 12 delete $_instances{$class};
135 }
136 12 return;
137 }
138  
139 # allow other classes which cooperate with Sys::OsRelease to import our singleton-management methods
140 # This helps maintain minimal prerequisites among modules working to set up Perl on containers or new systems.
141 sub import_singleton
142 0 ▶{
Subroutine import_singleton: not called CC=2 SCAR=12.2
Pod documented
143 0 my $class = shift;
144 0 my $caller_class = caller;
145  
146 # export singleton-management methods to caller class
147 0 foreach my $method_name (qw(init new instance defined_instance clear_instance)) {
148 ## no critic (TestingAndDebugging::ProhibitNoStrict)
149 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
150 0 *{$caller_class."::".$method_name} = \&{$class."::".$method_name};
151 }
152 0 return;
153 }
154  
155 #
156 # os-release data access methods
157 #
158  
159 # access module constants
160 1 ▶sub std_search_path { return @std_search_path; }
Subroutine std_search_path: called CC=1 SCAR=0.0
Pod undocumented
161 1 ▶sub std_attrs { return @std_attrs; }
Subroutine std_attrs: called CC=1 SCAR=0.0
Pod undocumented
162  
163 # fold case for case-insensitive matching
164 3 my $can_fc = CORE->can("fc"); # test fc() once and save result
165 sub fold_case
166 780 ▶{
Subroutine fold_case: called CC=2 SCAR=7.2
Pod undocumented
167 780 my $str = shift;
168  
169 # use fc if available, otherwise lc to support older Perls
170 780 ▶ return $can_fc ? $can_fc->($str) : lc($str);
Branch: 1 branch
BranchTrueFalseTotal
$can_fc ? : 780 0 780
171 }
172  
173 # load os-release contents into instance
174 sub _load_file
175 12 ▶{
Subroutine _load_file: called CC=17 SCAR=30.4
176 12 my $self = shift;
177  
178 # locate os-release file in standard places
179 12 my $osrelease_path;
180 12 ▶ my $file_name = ((exists $self->{_config}{file_name}) ? $self->{_config}{file_name} : $std_file_name);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_config'}{'file_name'} ? : 9 3 12
181 12 ▶ if (( substr( $file_name, 0, 1 ) eq "/" ) and ( -f $file_name )) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($file_name, 0, 1) eq '/' and -f $file_name) { } 0 12 12
Condition33%1/3
substr($file_name, 0, 1) eq "/" and -f $file_name
!ll&&!rl&&r
1200
MC/DC0%0/2
substr($file_name, 0, 1) eq "/" and -f $file_name
substr($file_name, 0, 1) eq "/" -f $file_name
Truth table33%1/3
A and B
A= substr($file_name, 0, 1) eq "/"B= -f $file_name
ABresult
0X0
100
111
182 # if file_name parameter is an absolute path to an existing file, use it and ignore search_path
183 0 $osrelease_path = $file_name;
184 } else {
185 # check search_path directories for first occurrence of file_name
186 my @search_path = ((exists $self->{_config}{search_path})
187 12 ▶ ? @{$self->{_config}{search_path}}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_config'}{'search_path'} ? : 10 2 12
188 : @std_search_path);
189 12 foreach my $search_dir (@search_path) {
190 11 ▶ if (-r "$search_dir/$file_name") {
Branch: 1 branch
BranchTrueFalseTotal
if (-r "$search_dir/$file_name") 11 0 11
191 11 $osrelease_path = $search_dir."/".$file_name;
192 11 last;
193 }
194 }
195 }
196  
197 # If we found os-release on this system, read it
198 # otherwise leave everything empty and platform() method will use Perl's $Config{osname} as a summary value
199 12 ▶ if (defined $osrelease_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $osrelease_path) 11 1 12
200 # save os-release file path
201 11 $self->{_config}{osrelease_path} = $osrelease_path;
202  
203 # read os-release file
204 ## no critic (InputOutput::RequireBriefOpen)
205 11 ▶ if (open my $fh, "<", $osrelease_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $osrelease_path) 11 0 11
206 11 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 134 11 145
207 134 chomp $line; # remove trailing nl
208 134 ▶ if (substr($line, -1, 1) eq "\r") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($line, -1, 1) eq "\r") 0 134 134
209 0 $line = substr($line, 0, -1); # remove trailing cr
210 }
211  
212 # skip comments and blank lines
213 134 ▶ if ($line =~ /^ \s+ #/x or $line =~ /^ \s+ $/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^ \s+ # /x or $line =~ /^ \s+ $/x) 0 134 134
Condition33%1/3
$line =~ /^ \s+ # /x or $line =~ /^ \s+ $/x
l!l&&r!l&&!r
00134
MC/DC0%0/2
$line =~ /^ \s+ # /x or $line =~ /^ \s+ $/x
$line =~ /^ \s+ # /x $line =~ /^ \s+ $/x
Truth table33%1/3
A or B
A= $line =~ /^ \s+ # /xB= $line =~ /^ \s+ $/x
ABresult
1X1
011
000
214 0 next;
215 }
216  
217 # read attribute assignment lines
218 134 ▶ if ($line =~ /^ ([A-Z0-9_]+) = "(.*)" $/x
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^ ([A-Z0-9_]+) = "(.*)" $/x or $line =~ /^ ([A-Z0-9_]+) = '(.*)' $/x or $line =~ /^ ([A-Z0-9_]+) = (.*) $/x) 132 2 134
Condition83%5/6
$line =~ /^ ([A-Z0-9_]+) = "(.*)" $/x or $line =~ /^ ([A-Z0-9_]+) = '(.*)' $/x or $line =~ /^ ([A-Z0-9_]+) = (.*) $/x
l!l&&r!l&&!r
97352
$line =~ /^ ([A-Z0-9_]+) = "(.*)" $/x or $line =~ /^ ([A-Z0-9_]+) = '(.*)' $/x
l!l&&r!l&&!r
97037
MC/DC66%2/3
$line =~ /^ ([A-Z0-9_]+) = "(.*)" $/x or $line =~ /^ ([A-Z0-9_]+) = '(.*)' $/x or $line =~ /^ ([A-Z0-9_]+) = (.*) $/x
$line =~ /^ ([A-Z0-9_]+) = "(.*)" $/x $line =~ /^ ([A-Z0-9_]+) = '(.*)' $/x $line =~ /^ ([A-Z0-9_]+) = (.*) $/x
Truth table75%3/4
A or B or C
A= $line =~ /^ ([A-Z0-9_]+) = "(.*)" $/xB= $line =~ /^ ([A-Z0-9_]+) = '(.*)' $/xC= $line =~ /^ ([A-Z0-9_]+) = (.*) $/x
ABCresult
1XX1
01X1
0011
0000
219 or $line =~ /^ ([A-Z0-9_]+) = '(.*)' $/x
220 or $line =~ /^ ([A-Z0-9_]+) = (.*) $/x)
221 {
222 132 ▶ next if $1 eq "_config"; # don't overwrite _config
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "_config" 0 132 132
223 132 $self->{fold_case($1)} = $2;
224 }
225 }
226 11 close $fh;
227 }
228 }
229  
230 12 return;
231 }
232  
233 # initialize a new instance
234 sub _new_instance
235 12 ▶{
Subroutine _new_instance: called CC=6 SCAR=18.8
236 12 my ($class, @params) = @_;
237  
238 # enforce class lineage - _new_instance() should be overloaded by other classes that import singleton methods
239 12 ▶ if (not $class->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->isa(__PACKAGE__)) 0 12 12
240 0 ▶ croak "_new_instance() should be overloaded by calling class: "
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
241 .(ref $class ? ref $class : $class)." is not a ".__PACKAGE__;
242 }
243  
244 # obtain parameters from array or hashref
245 12 my %obj;
246 12 ▶ if (scalar @params > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params > 0) 10 2 12
247 10 ▶ if (ref $params[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 0 10 10
248 0 $obj{_config} = $params[0];
249 } else {
250 10 $obj{_config} = {@params};
251 }
252 }
253  
254 # add empty _config entry if needed
255 12 ▶ if ( not exists $obj{_config}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $obj{"_config"}) 2 10 12
256 2 $obj{_config} = {};
257 }
258  
259 # bless instance and load file
260 12 my $obj_ref = bless \%obj, $class;
261 12 $obj_ref->_load_file();
262  
263 # generate accessor methods
264 12 $obj_ref->_gen_accessors();
265  
266 # instantiate object
267 12 return $obj_ref;
268 }
269  
270 # helper function to allow methods to get the instance ref when called via the class name
271 sub class_or_obj
272 1140 ▶{
Subroutine class_or_obj: called CC=2 SCAR=6.9
Pod undocumented
273 1140 my $coo = shift;
274  
275 # return the instance
276 1140 ▶ return ((ref $coo) ? $coo : $coo->instance());
Branch: 1 branch
BranchTrueFalseTotal
ref $coo ? : 1122 18 1140
277 }
278  
279 # clean up data in an instance before feeding it to the destructor
280 sub _cleanup_instance
281 12 ▶{
Subroutine _cleanup_instance: called CC=3 SCAR=11.2
282 12 my ($class_or_obj) = @_;
283 12 my $self = class_or_obj($class_or_obj);
284  
285 # enforce class lineage - _cleanup_instance() should be overloaded by other classes that import singleton methods
286 12 ▶ if (not $self->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->isa(__PACKAGE__)) 0 12 12
287 0 croak "_new_instance() should be overloaded by calling class: "
288 .(ef $self)." is not a ".__PACKAGE__;
289 }
290  
291 # clear accessor functions
292 12 foreach my $acc (keys %{$self->{_config}{accessor}}) {
293 432 $self->_clear_accessor($acc);
294 }
295 12 return;
296 }
297  
298 # determine platform type
299 sub platform
300 4 ▶{
Subroutine platform: called CC=10 SCAR=24.9
Pod documented
301 4 my ($class_or_obj) = @_;
302 4 my $self = class_or_obj($class_or_obj);
303
304 # if we haven't already saved this result, compute and save it
305 4 ▶ if (not $self->has_config("platform")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_config("platform")) 2 2 4
306 2 ▶ if ($self->has_attr("id")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_attr("id")) 1 1 2
307 1 $self->config("platform", $self->id);
308 }
309 2 ▶ if ($self->has_attr("id_like")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_attr("id_like")) 1 1 2
310 # check if the configuration has additional common IDs which should be recognized if seen in ID_LIKE
311 1 ▶ if ($self->has_config("common_id")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_config("common_id")) 0 1 1
312 0 my $cids = $self->config("common_id");
313 0 ▶ my @cids = (ref $cids eq "ARRAY") ? (@{$cids}) : (split /\s+/x, $cids);
Branch: 1 branch
BranchTrueFalseTotal
ref $cids eq 'ARRAY' ? : 0 0 0
314 0 foreach my $cid (@cids) {
315 0 $common_id{$cid} = 1;
316 }
317 }
318  
319 # check ID_LIKE for more common names which should be used instead of ID
320 1 foreach my $like (split /\s+/x, $self->id_like) {
321 1 ▶ if (exists $common_id{$like}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $common_id{$like}) 1 0 1
322 1 $self->config("platform", $like);
323 1 last;
324 }
325 }
326 }
327  
328 # if platform is still not set, use Perl's osname config as a summary value
329 2 ▶ if (not $self->has_config("platform")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_config("platform")) 1 1 2
330 1 $self->config("platform", $Config{osname});
331 }
332 }
333 4 return $self->config("platform");
334 }
335  
336 # get location of the os-release file found on this system
337 # return undef if the file was not found
338 sub osrelease_path
339 1 ▶{
Subroutine osrelease_path: called CC=2 SCAR=7.4
Pod documented
340 1 my ($class_or_obj) = @_;
341 1 my $self = class_or_obj($class_or_obj);
342 1 ▶ if (exists $self->{_config}{osrelease_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_config"}{"osrelease_path"}) 1 0 1
343 1 return $self->{_config}{osrelease_path};
344 }
345 0 return;
346 }
347  
348 # return list of attributes found in os-release file
349 sub found_attrs
350 13 ▶{
Subroutine found_attrs: called CC=1 SCAR=0.0
Pod documented
351 13 my ($class_or_obj) = @_;
352 13 my $self = class_or_obj($class_or_obj);
353 13 return grep { $_ ne "_config" } keys %$self;
354 }
355  
356 # attribute existence checker
357 sub has_attr
358 110 ▶{
Subroutine has_attr: called CC=2 SCAR=6.9
Pod documented
359 110 my ($class_or_obj, $key) = @_;
360 110 my $self = class_or_obj($class_or_obj);
361 110 ▶ return ((exists $self->{fold_case($key)}) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
exists $$self{fold_case($key)} ? : 108 2 110
362 }
363  
364 # attribute read-only accessor
365 sub get
366 106 ▶{
Subroutine get: called CC=1 SCAR=0.0
Pod documented
367 106 my ($class_or_obj, $key) = @_;
368 106 my $self = class_or_obj($class_or_obj);
369 106 return $self->{fold_case($key)};
370 }
371  
372 # attribute existence checker
373 sub has_config
374 11 ▶{
Subroutine has_config: called CC=2 SCAR=6.9
Pod documented
375 11 my ($class_or_obj, $key) = @_;
376 11 my $self = class_or_obj($class_or_obj);
377 11 ▶ return ((exists $self->{_config}{$key}) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_config'}{$key} ? : 5 6 11
378 }
379  
380 # config read/write accessor
381 sub config
382 7 ▶{
Subroutine config: called CC=2 SCAR=6.9
Pod documented
383 7 my ($class_or_obj, $key, $value) = @_;
384 7 my $self = class_or_obj($class_or_obj);
385 7 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 3 4 7
386 3 $self->{_config}{$key} = $value;
387 }
388 7 return $self->{_config}{$key};
389 }
390  
391 # generate accessor methods for all defined and standardized attributes
392 # private internal method
393 sub _gen_accessors
394 12 ▶{
Subroutine _gen_accessors: called CC=3 SCAR=11.0
395 12 my ($class_or_obj) = @_;
396 12 my $self = class_or_obj($class_or_obj);
397  
398 # generate accessors for standardized attributes whether or not they were not found in os-release
399 # for attributes which exist, it makes a read-only accessor
400 # for attributes which don't exist, it makes an undef accessor
401 12 foreach my $std_attr (@std_attrs) {
402 432 ▶ next if $std_attr eq "_config"; # protect special/reserved attribute
Branch: 1 branch
BranchTrueFalseTotal
if $std_attr eq "_config" 0 432 432
403 432 my $fc_attr = fold_case($std_attr);
404 432 $self->_gen_accessor($fc_attr);
405 }
406 12 return;
407 }
408  
409 # generate accessor
410 # private internal method
411 sub _gen_accessor
412 432 ▶{
Subroutine _gen_accessor: called CC=4 SCAR=13.9
413 432 my ($class_or_obj, $name) = @_;
414 432 my $self = class_or_obj($class_or_obj);
415 432 ▶ my $class = (ref $self) ? (ref $self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 432 0 432
416 432 my $method_name = $class."::".$name;
417  
418 # mark accessor flag in configuration so it can be deleted for cleanup (mainly for testing)
419 432 ▶ if (not exists $self->{_config}{accessor}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_config"}{"accessor"}) 12 420 432
420 12 $self->{_config}{accessor} = {};
421 }
422  
423 # generate accessor as read-only or undef depending whether it exists in the running system
424 432 ▶ if (exists $self->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$name}) { } 119 313 432
425 # generate read-only accessor for attribute which was found in os-release
426 119 ▶ $self->{_config}{accessor}{$name} = sub { return $self->{$name} };
Subroutine __ANON__: called CC=1 SCAR=0.0
427 } else {
428 # generate undef accessor for standard attribute which was not found in os-release
429 0 ▶ $self->{_config}{accessor}{$name} = sub { return; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
430 }
431  
432 ## no critic (TestingAndDebugging::ProhibitNoStrict)
433 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
434 432 *{$method_name} = $self->{_config}{accessor}{$name};
435 432 return;
436 }
437  
438 # clean up accessor
439 # private internal method
440 sub _clear_accessor
441 432 ▶{
Subroutine _clear_accessor: called CC=3 SCAR=11.0
442 432 my ($class_or_obj, $name) = @_;
443 432 my $self = class_or_obj($class_or_obj);
444 432 ▶ my $class = (ref $self) ? (ref $self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 432 0 432
445 432 ▶ if (exists $self->{_config}{accessor}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_config"}{"accessor"}{$name}) 432 0 432
446 432 my $method_name = $class."::".$name;
447 ## no critic (TestingAndDebugging::ProhibitNoStrict)
448 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
449 432 undef *{$method_name};
450 432 delete $self->{_config}{accessor}{$name};
451 }
452 432 return;
453 }
454  
455 3 1;
456  
457 __END__
↑ Summary