File Coverage

blib/lib/Slackware/SBoKeeper/Config.pm
Criterion Covered Total %
statement 33 39 84.6
branch 7 12 58.3
condition 3 5 60.0
subroutine 6 6 100.0
pod 1 1 100.0
total 50 63 79.3


line stmt bran cond sub pod time code
1             package Slackware::SBoKeeper::Config;
2             our $VERSION = '2.06';
3 3     3   90234 use 5.016;
  3         11  
4 3     3   16 use strict;
  3         4  
  3         104  
5 3     3   14 use warnings;
  3         4  
  3         199  
6              
7 3     3   21 use Exporter 'import';
  3         5  
  3         137  
8             our @EXPORT_OK = qw(read_config);
9              
10 3     3   17 use File::Spec;
  3         6  
  3         1499  
11              
12             sub read_config {
13              
14 4     4 1 138760 my $file = shift;
15 4         18 my $callback = shift;
16              
17 4         18 my $config = {};
18              
19 4 50       257 open my $fh, '<', $file
20             or die "Failed to open config file $file for reading: $!\n";
21              
22 4         390 my $param = {
23             File => File::Spec->rel2abs($file),
24             };
25              
26 4         16 my $ln = 0;
27 4         162 while (my $l = readline $fh) {
28              
29 28         47 $ln++;
30              
31             # Skip comments and blanks
32 28 100 100     163 next if $l =~ /^#/ or $l =~ /^\s*$/;
33              
34 15 50       57 unless ($l =~ /=/) {
35 0         0 die "$file line $ln: Missing '='\n";
36             }
37              
38 15         62 my ($field, $val) = split '=', $l, 2;
39 15         92 $field =~ s/^\s+|\s+$//g;
40 15         183 $val =~ s/^\s+|\s+$//g;
41              
42 15 50       48 if ($val eq '') {
43 0         0 die "$file line $ln: Value cannot be empty\n";
44             }
45              
46 15 50       46 unless (defined $callback->{$field}) {
47 0         0 die "$file line $ln: '$field' is not a valid field\n";
48             }
49              
50             eval {
51 15         79 $config->{$field} = $callback->{$field}($val, $param);
52 15         193 1; # so that $config->{$field} being 0 won't cause problems.
53 15 50       42 } or do {
54 0   0     0 my $e = $@ || 'Something went wrong';
55 0         0 chomp $e;
56 0         0 die "$file line $ln: $e\n";
57             };
58              
59             }
60              
61 4         81 return $config;
62              
63             }
64              
65             1;
66              
67             =head1 NAME
68              
69             Slackware::SBoKeeper::Config - Configuration file reader
70              
71             =head1 SYNOPSIS
72              
73             use Slackware::SBoKeeper::Config qw(read_config);
74              
75             my $config = read_config($file, $callbacks);
76              
77             =head1 DESCRIPTION
78              
79             Slackware::SBoKeeper::Config is a module that provides the C
80             subroutine, which can read L config files.
81             Slackware::SBoKeeper::Config is not meant to be used outside of L.
82             If you are looking for L user documentation, please consult its
83             manual.
84              
85             =head1 SUBROUTINES
86              
87             =over 4
88              
89             =item read_config($file, $callbacks)
90              
91             C is a subroutine that reads the config file C<$file> and returns
92             its contents in the form of a hash ref. C<$callbacks> is a hash ref of config
93             fields and their respective callback subroutine references that process the
94             value of said field. C<$callbacks> must contain every field that is able to be
95             read, otherwise C will fail on unknown fields.
96              
97             Configuration files consists of lines of key-value pairs, seperated by an
98             equal sign (=). Blank lines and comments are ignored.
99              
100             The subroutines in C<$callbacks> can take two arguments, the first is the value
101             of the field and the second is a hash ref of extra data that might be useful to
102             the subroutine. The hash ref can have the following fields:
103              
104             =over 4
105              
106             =item File
107              
108             Absolute path of the config file being read.
109              
110             =back
111              
112             C must be manually imported.
113              
114             =back
115              
116             =head1 AUTHOR
117              
118             Written by Samuel Young, Esamyoung12788@gmail.comE.
119              
120             =head1 BUGS
121              
122             Report bugs on my Codeberg, L.
123              
124             =head1 COPYRIGHT
125              
126             Copyright (C) 2024-2025 Samuel Young
127              
128             This program is free software; you can redistribute it and/or modify it under
129             the terms of either: the GNU General Public License as published by the Free
130             Software Foundation; or the Artistic License.
131              
132             =head1 SEE ALSO
133              
134             L
135              
136             =cut