File Coverage

blib/lib/App/newver/INI.pm
Criterion Covered Total %
statement 27 31 87.1
branch 7 10 70.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 1 1 100.0
total 43 50 86.0


line stmt bran cond sub pod time code
1             package App::newver::INI;
2 2     2   144728 use 5.016;
  2         9  
3 2     2   13 use strict;
  2         55  
  2         72  
4 2     2   12 use warnings;
  2         18  
  2         165  
5             our $VERSION = '0.02';
6              
7 2     2   13 use Exporter qw(import);
  2         4  
  2         1250  
8             our @EXPORT_OK = qw(read_ini);
9              
10             sub read_ini {
11              
12 1     1 1 257881 my ($file) = @_;
13              
14 1         3 my $hash = {};
15              
16 1 50       86 open my $fh, '<', $file or die "Failed to open $file for reading: $!\n";
17              
18 1         3 my $sect = undef;
19 1         3 my $ln = 0;
20 1         53 while (my $l = readline $fh) {
21 15         30 $ln++;
22 15         110 $l =~ s/^\s+|\s+$//g;
23 15 100 100     66 if ($l =~ /^#/ or $l eq '') {
24 3         13 next;
25             }
26 12 100       56 if ($l =~ /^\[\s*(.+)\s*\]$/) {
    50          
27 3         13 $sect = $1;
28             } elsif ($l =~ /^(\w+)\s*=\s*(.+)$/) {
29 9 50       19 if (not defined $sect) {
30 0         0 close $fh;
31 0         0 die "$file line $ln: key-value pair not under a section\n";
32             }
33 9         95 $hash->{ $sect }{ $1 } = $2;
34             } else {
35 0         0 close $fh;
36 0         0 die "$file line $ln: invalid line\n";
37             }
38             }
39              
40 1         15 close $fh;
41              
42 1         9 return $hash;
43              
44             }
45              
46             1;
47              
48             =head1 NAME
49              
50             App::newver::INI - newver INI file parser
51              
52             =head1 SYNOPSIS
53              
54             use App::newver::INI qw(read_ini);
55              
56             my $hash = read_ini($path_to_ini);
57              
58             =head1 DESCRIPTION
59              
60             B is an INI parser module for L. This is a private
61             module, for user documentation please consult the L manual.
62              
63             L uses the following dialect of INI:
64              
65             =over 2
66              
67             =item Key-value pairs are separated by equals (=) signs.
68              
69             =item Sections are lines enclosed in brackets.
70              
71             =item Key-value pairs must be under a section (no default section)
72              
73             =item Comments start with a hash (#) sign.
74              
75             =item Whitespace is trimmed.
76              
77             =item Leading and trailing whitespace is trimmed.
78              
79             =back
80              
81             =head1 SUBROUTINES
82              
83             Subroutines are not exported by default.
84              
85             =head2 $hash = read_ini($ini_file)
86              
87             Reads the INI file C<$ini_file> and returns a hash of hashes representing the
88             file.
89              
90             For example, the following INI file:
91              
92             [Cat]
93             Meows = Yes
94             Tail = Yes
95              
96             [Dog]
97             Meows = No
98             Tail = Yes
99              
100             would yield the following hash structure:
101              
102             {
103             'Cat' => {
104             Meows => 'Yes',
105             Tail => 'Yes',
106             },
107             'Dog' => {
108             Meows => 'No',
109             Tail => 'Yes',
110             },
111             }
112              
113             =head1 AUTHOR
114              
115             Written by L
116              
117             This project's source can be found on its
118             L. Comments and pull
119             requests are welcome.
120              
121             =head1 COPYRIGHT
122              
123             Copyright (C) 2025 Samuel Young.
124              
125             This program is free software; you can redistribute it and/or modify it under
126             the terms of the Artistic License 2.0.
127              
128             =head1 SEE ALSO
129              
130             L
131              
132             =cut