File Coverage

lib/Pcore/Util/Config/INI.pm
Criterion Covered Total %
statement 25 50 50.0
branch 6 20 30.0
condition n/a
subroutine 3 5 60.0
pod 0 2 0.0
total 34 77 44.1


line stmt bran cond sub pod time code
1             package Pcore::Util::Config::INI;
2              
3 3     3   20 use Pcore;
  3         4  
  3         17  
4 3     3   21 use Pcore::Util::Text qw[decode_utf8 encode_utf8 trim];
  3         5  
  3         17  
5              
6 3     3 0 6 sub from_ini ($str) {
  3         6  
  3         6  
7 3         4 my $cfg;
8              
9 3         6 my $section = '_';
10              
11 3         79 my @lines = grep { $_ ne q[] } map { trim $_} split /\n/sm, decode_utf8 $str;
  348         460  
  348         4724  
12              
13 3         29 for my $line (@lines) {
14              
15             # section
16 249 100       575 if ( $line =~ /\A\[(.+)\]\z/sm ) {
17 102         176 $section = $1;
18              
19 102 50       335 $cfg->{$section} = {} if !exists $cfg->{$section};
20             }
21              
22             # not a section
23             else {
24              
25             # comment
26 147 50       216 if ( $line =~ /\A;/sm ) {
27 0         0 next;
28             }
29              
30             # variable
31             else {
32 147         344 my ( $key, $val ) = split /=/sm, $line, 2;
33              
34 147 50       248 if ( defined $val ) {
35 147         2156 trim $val;
36              
37 147 50       242 $val = undef if $val eq q[];
38             }
39              
40 147         2108 $cfg->{$section}->{ trim $key} = $val;
41             }
42             }
43             }
44              
45 3         22 return $cfg;
46             }
47              
48 0     0 0   sub to_ini ($hash) {
  0            
  0            
49 0           my $str = q[];
50              
51 0     0     state $write_section = sub ( $str_ref, $section, $data ) {
  0            
  0            
  0            
  0            
52 0 0         if ($section) {
53 0 0         $str_ref->$* .= "\n" x 2 if $str_ref->$*;
54              
55 0           $str_ref->$* .= "[$section]";
56             }
57              
58 0           for my $key ( sort keys $data->%* ) {
59 0 0         $str_ref->$* .= "\n" if $str_ref->$*;
60              
61 0 0         $str_ref->$* .= "$key = " . ( defined $data->{$key} ? "$data->{$key}" : q[] );
62             }
63              
64 0           return;
65 0           };
66              
67 0 0         if ( exists $hash->{_} ) {
68 0           $write_section->( \$str, q[], $hash->{_} );
69             }
70              
71 0           for my $section ( sort grep { $_ ne '_' } keys $hash->%* ) {
  0            
72 0           $write_section->( \$str, $section, $hash->{$section} );
73             }
74              
75 0           encode_utf8 $str;
76              
77 0           return \$str;
78             }
79              
80             1;
81             __END__
82             =pod
83              
84             =encoding utf8
85              
86             =head1 NAME
87              
88             Pcore::Util::Config::INI
89              
90             =head1 SYNOPSIS
91              
92             =head1 DESCRIPTION
93              
94             =head1 ATTRIBUTES
95              
96             =head1 METHODS
97              
98             =head1 SEE ALSO
99              
100             =cut