| 1 | package Config::INIPlus; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use IO::String; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | Config::INIPlus - Read and write INI-style config files with structure extensions | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | our $VERSION = '1.0.3'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Some regexes we use for matching | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | my $sp = qr/(?:[ ]|\t)+/; # Space characters | |||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | my $osp = qr/(?:[ ]|\t)*/; # Optional space characters | |||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | my $eol = qr/(?:\r?\n)/x; # End of line | |||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | my $eolc = qr/(?:;.*)?$eol/x; # End of line with optional comment | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # Modes for calls to ->new | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | use constant TOP_MODE => 0; # When new is called non-recursively (i.e., | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | # from the topmost context) | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | use constant HASH_MODE => 1; # When processing a sub-hash | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | ▶ | use constant ARRAY_MODE => 2; # When processing a sub-array | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | ▶ | use constant STRING_MODE => 3; # When processing a multi-line string | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | INIPlus is a configurtion file format based on INI which supports multi-line | ||||||||||||||||||||||||||||||||||||||||||
| 35 | strings and nesting of arrays and hashes. This is useful if you start a | ||||||||||||||||||||||||||||||||||||||||||
| 36 | project using INI files, but realize you need nested data in your | ||||||||||||||||||||||||||||||||||||||||||
| 37 | configurations and want to support extended configurations without | ||||||||||||||||||||||||||||||||||||||||||
| 38 | breaking backward compatibility with existing config files. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head2 The INIPlus Config File | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | ; Comment | ||||||||||||||||||||||||||||||||||||||||||
| 43 | Key=Value ; End of line comment | ||||||||||||||||||||||||||||||||||||||||||
| 44 | Key2="Multi | ||||||||||||||||||||||||||||||||||||||||||
| 45 | Line | ||||||||||||||||||||||||||||||||||||||||||
| 46 | Value" ; Post-multi-line comment | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | [Section] | ||||||||||||||||||||||||||||||||||||||||||
| 49 | Foo=This is a foo | ||||||||||||||||||||||||||||||||||||||||||
| 50 | Hash { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | Bar=Hey it's a bar | ||||||||||||||||||||||||||||||||||||||||||
| 52 | Baz="Is Baz at the bar?" | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | Array ( | ||||||||||||||||||||||||||||||||||||||||||
| 55 | Value One | ||||||||||||||||||||||||||||||||||||||||||
| 56 | "Value Two" | ||||||||||||||||||||||||||||||||||||||||||
| 57 | "Value | ||||||||||||||||||||||||||||||||||||||||||
| 58 | Three | ||||||||||||||||||||||||||||||||||||||||||
| 59 | Is multi-line!" | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | The hashes and arrays can be nested any number of levels deep: | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | Hash { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | ArrayOfSubhashes ( | ||||||||||||||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | Key1=Val1 | ||||||||||||||||||||||||||||||||||||||||||
| 68 | Key2=Val2 | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | HeyAnotherArray ( | ||||||||||||||||||||||||||||||||||||||||||
| 72 | Value1 | ||||||||||||||||||||||||||||||||||||||||||
| 73 | Value2 | ||||||||||||||||||||||||||||||||||||||||||
| 74 | Value3 | ||||||||||||||||||||||||||||||||||||||||||
| 75 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =head2 Creating a config object | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | use Config::INIPlus; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # Create the config object from a file | ||||||||||||||||||||||||||||||||||||||||||
| 85 | $cfg = Config::INIPlus->new( file => 'foo.ini' ); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | # Create the config object from a filehandle | ||||||||||||||||||||||||||||||||||||||||||
| 88 | $filehandle = IO::File->new('file.ini'); | ||||||||||||||||||||||||||||||||||||||||||
| 89 | $cfg = Config::INIPlus->new( fh => $filehandle ); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | # Create the config from a string | ||||||||||||||||||||||||||||||||||||||||||
| 92 | $string = <<EOF; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | Key1=Val1 | ||||||||||||||||||||||||||||||||||||||||||
| 94 | Key2=Val2 | ||||||||||||||||||||||||||||||||||||||||||
| 95 | ; ... | ||||||||||||||||||||||||||||||||||||||||||
| 96 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | $cfg = Config::INIPlus->new( string => $string ); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =head2 Extracting the contents of a config object | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # Gets a non-sectioned value (like "Key2" in the example INI above) | ||||||||||||||||||||||||||||||||||||||||||
| 103 | my $val = $cfg->get( 'KeyName' ); | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Gets a value from a section (e.g., "Foo" under "Section" in | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # the example above) | ||||||||||||||||||||||||||||||||||||||||||
| 107 | my $val = $cfg->get( 'KeyName', 'SectionName' ); | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # Gets the entire structure as a hash reference | ||||||||||||||||||||||||||||||||||||||||||
| 110 | my $hash = $cfg->as_hashref(); | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # Get one section as a hash reference (e.g., "Section" in the | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # exampe INI above) | ||||||||||||||||||||||||||||||||||||||||||
| 114 | my $sec = $cfg->section_as_hashref( 'SectionName' ); | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | =head2 Modifying a config object | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # Set a non-sectioned value | ||||||||||||||||||||||||||||||||||||||||||
| 119 | $cfg->set( 'KeyName', 'KeyValue' ); | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # Set a value for a key within a section | ||||||||||||||||||||||||||||||||||||||||||
| 122 | $cfg->set( 'KeyName', 'KeyValue', 'SectionName' ); | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | # Remove a non-sectioned key (and respective value) | ||||||||||||||||||||||||||||||||||||||||||
| 125 | $cfg->del( 'KeyName' ); | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | # Remove a sectioned key | ||||||||||||||||||||||||||||||||||||||||||
| 128 | $cfg->del( 'KeyName', 'SectionName' ); | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | # Add a section | ||||||||||||||||||||||||||||||||||||||||||
| 131 | $cfg->add_section( 'SectionName' ); | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | # Remove a section | ||||||||||||||||||||||||||||||||||||||||||
| 134 | $cfg->del_section( 'SectionName' ); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | =head2 Getting the config object as text / writing to a file | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # Get the configuration as a string | ||||||||||||||||||||||||||||||||||||||||||
| 139 | $string = $cfg->as_string; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # Write the configuration back into the file it was originally | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # read from | ||||||||||||||||||||||||||||||||||||||||||
| 143 | $cfg->write; | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # Write the configuration to a specific file | ||||||||||||||||||||||||||||||||||||||||||
| 146 | $cfg->write( 'filename.ini' ); | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | =head2 Config::INIPlus->new( file => 'filename' ) | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =head2 Config::INIPlus->new( fh => $perl_filehandle ) | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =head2 Config::INIPlus->new( string => $string_config ) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | Creates a new config object. You can use a filename with the 'file' paramter, | ||||||||||||||||||||||||||||||||||||||||||
| 157 | a IO::Handle style filehandle using the 'fh', or pull from the entire INIPlus | ||||||||||||||||||||||||||||||||||||||||||
| 158 | configuration loaded into a string using the 'string' paramter. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 20 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=55 SCAR=45.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 20 | my $pkg = shift; # Package | |||||||||||||||||||||||||||||||||||||||||
| 165 | 20 | my %p = @_; # Parameters | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | # Default some of the parameters | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 20 | ▶ | unless ( defined $p{'debug'} ) { $p{'debug'} = 0; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 20 | ▶ | unless ( defined $p{'file'} ) { $p{'file'} = ''; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 20 | ▶ | unless ( defined $p{'_mode'} ) { $p{'_mode'} = TOP_MODE; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | # Are we being called from the topmost context without a filehandle? | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 20 | ▶ | if ( ( $p{'_mode'} == TOP_MODE ) && ( not defined $p{'fh'} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$p{"_mode"} == 0 and not defined $p{"fh"}
MC/DC50%1/2
$p{"_mode"} == 0 and not defined $p{"fh"}
$p{"_mode"} == 0
not defined $p{"fh"}
Truth table66%2/3
A and not(B)
A= $p{"_mode"} == 0B= not defined $p{"fh"}
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 4 | ▶ | if ( defined $p{'string'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | # Turn the string we've been passed into a filehandle | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 2 | $p{'fh'} = IO::String->new( $p{'string'}, 'r' ); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | if ( not defined $p{'fh'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | croak "Error opening string $p{'string'}: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | elsif ( $p{'file'} ) { | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | # Open the filename we've been passed to a new filehandle | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | $p{'fh'} = IO::File->new( $p{'file'}, 'r' ); | |||||||||||||||||||||||||||||||||||||||||
| 186 | 2 | ▶ | if ( not defined $p{'fh'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | croak "Error opening file $p{'file'}: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | # Check that the filehandle we should have at this point looks like a | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # filehandle (I used ->can instead of ->isa since many handle interfaces | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # don't actually inherit from IO::Handle but all of the ones which work | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # with this module will support the method 'getline' | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 20 | ▶ | unless ( defined( $p{'fh'} ) && eval { $p{'fh'}->can('getline') } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $p{"fh"} and eval {
do {
$p{"fh"}->can("getline")
}
}
MC/DC0%0/2
defined $p{"fh"} and eval {
do {
$p{"fh"}->can("getline")
}
}
defined $p{"fh"}
eval {
do {
$p{"fh"}->can("getline")
}
}
Truth table33%1/3
A and B
A= defined $p{"fh"}B= eval {
do {
$p{"fh"}->can("getline")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | croak "Must be called with a filename, string or filehandle"; | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 20 | my $struct; # This contains whatever we're going to return up the | |||||||||||||||||||||||||||||||||||||||||
| 201 | # chain: a hash, array or string | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 20 | my @sections = (); # This is a list of INI file sections | |||||||||||||||||||||||||||||||||||||||||
| 203 | 20 | my $section = ''; # This is the current section being processed | |||||||||||||||||||||||||||||||||||||||||
| 204 | 20 | my $line = ''; # The current line being processed | |||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 20 | local $/ = "\012"; # Unix newline... will catch DOS newlines too. | |||||||||||||||||||||||||||||||||||||||||
| 207 | # Local is necessary if the parent context has set | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # $/ to something different | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | # Keep processing the FH until we hit the end | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 20 | ▶ | while ( not $p{'fh'}->eof ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 136 | $line = $p{'fh'}->getline; | |||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | # If debugging is enabled then show the line being processed | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 136 | ▶ | $p{'debug'} && _debug( $p{'fh'}, $line, "line - $line" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 136 | ▶ | if ( $p{'_mode'} == TOP_MODE || $p{'_mode'} == HASH_MODE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$p{"_mode"} == 0 or $p{"_mode"} == 1
MC/DC100%2/2
$p{"_mode"} == 0 or $p{"_mode"} == 1
$p{"_mode"} == 0
$p{"_mode"} == 1
Truth table100%3/3
A or B
A= $p{"_mode"} == 0B= $p{"_mode"} == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | # Process in a HASH style context | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 88 | ▶ | unless ( defined $struct ) { $struct = {}; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 88 | my $name; | |||||||||||||||||||||||||||||||||||||||||
| 224 | my $val; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 88 | ▶ | if ( $line =~ m/ ^ $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | $p{'debug'} | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 24 | ▶ | && _debug( $p{'fh'}, "Skipping blank/comment line" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | elsif ( $line =~ m/ ^ $osp \[ $osp (.+) $osp \] $osp $eolc $ /x ) | ||||||||||||||||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | # Process a [section] definition | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 8 | $section = $1; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 8 | ▶ | if ( $section =~ m/^_/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | croak( $p{'fh'}, $p{'file'}, | |||||||||||||||||||||||||||||||||||||||||
| 237 | "Sections cannot begin with underscore" ); | ||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 8 | ▶ | if ( $p{'_mode'} == TOP_MODE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 8 | push @sections, $section; | |||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, | |||||||||||||||||||||||||||||||||||||||||
| 244 | "Unexpected section definition $section during subhash" | ||||||||||||||||||||||||||||||||||||||||||
| 245 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | elsif ( $line =~ m/ ^ $osp (.*?) $osp \{ $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 4 | $name = $1; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 4 | $val = $pkg->new( %p, '_mode' => HASH_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | elsif ( $line =~ m/ ^ $osp (.*?) $osp \( $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 4 | $name = $1; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 4 | $val = $pkg->new( %p, '_mode' => ARRAY_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | elsif ( | ||||||||||||||||||||||||||||||||||||||||||
| 257 | $line =~ m/ ^ $osp (.*?) $osp \= $osp \"( [^"]* $eol ) $ /x ) | ||||||||||||||||||||||||||||||||||||||||||
| 258 | { | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 4 | $name = $1; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 4 | $val = _fix_newlines( | |||||||||||||||||||||||||||||||||||||||||
| 261 | $2 . $pkg->new( %p, '_mode' => STRING_MODE ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | elsif ( | ||||||||||||||||||||||||||||||||||||||||||
| 264 | $line =~ m/ ^ $osp (.*?) $osp \= $osp \"([^"]+)\" $eolc $ /x ) | ||||||||||||||||||||||||||||||||||||||||||
| 265 | { | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 3 | $name = $1; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 3 | $val = $2; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | elsif ( | ||||||||||||||||||||||||||||||||||||||||||
| 270 | $line =~ m/ ^ $osp (.*?) $osp \= $osp (.*?) $osp $eolc $ /x ) | ||||||||||||||||||||||||||||||||||||||||||
| 271 | { | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 33 | $name = $1; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 33 | $val = $2; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | elsif ( $line =~ m/ ^ $osp ( \} | \) )$osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 8 | my $char = $1; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 8 | ▶ | if ( ( $p{'_mode'} != TOP_MODE ) && ( $char eq '}' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$p{"_mode"} != 0 and $char eq "}"
MC/DC0%0/2
$p{"_mode"} != 0 and $char eq "}"
$p{"_mode"} != 0
$char eq "}"
Truth table33%1/3
A and B
A= $p{"_mode"} != 0B= $char eq "}"
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | # We should only get to this line if we're nested | ||||||||||||||||||||||||||||||||||||||||||
| 280 | $p{'debug'} | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 8 | ▶ | && _debug( $p{'fh'}, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | "Returning nested hash back up the chain" ); | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 8 | return $struct; | |||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | # We saw a } or ) that doesn't belong here. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, | |||||||||||||||||||||||||||||||||||||||||
| 288 | "Unexpected $char" ); | ||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, "Malformed line" ); | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 80 | ▶ | if ( defined($name) && defined($val) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and defined $val
MC/DC50%1/2
defined $name and defined $val
defined $name
defined $val
Truth table66%2/3
A and B
A= defined $nameB= defined $val
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 48 | ▶ | if ( ( $p{'_mode'} == TOP_MODE ) && $section ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$p{"_mode"} == 0 and $section
MC/DC100%2/2
$p{"_mode"} == 0 and $section
$p{"_mode"} == 0
$section
Truth table100%3/3
A and B
A= $p{"_mode"} == 0B= $section
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 16 | $struct->{$section}{$name} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 32 | $struct->{$name} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | } ## end if ( $p{'_mode'} == TOP_MODE || $p{'_mode'} == HASH_MODE ) | ||||||||||||||||||||||||||||||||||||||||||
| 304 | elsif ( $p{'_mode'} == ARRAY_MODE ) { | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | # Process in an ARRAY style context | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 36 | ▶ | unless ( defined $struct ) { $struct = []; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 36 | my $val; | |||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 36 | ▶ | if ( $line =~ m/ ^ $osp $eolc $/x ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 9 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | $p{'debug'} | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | && _debug( $p{'fh'}, "Skipping blank/comment line" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | elsif ( $line =~ m/ ^ $osp \[ $osp (.+) $osp \] $osp $eolc $ /x ) | ||||||||||||||||||||||||||||||||||||||||||
| 316 | { | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, | |||||||||||||||||||||||||||||||||||||||||
| 318 | "Unexpected section definition $1 during subarray" ); | ||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | elsif ( $line =~ m/ ^ $osp \{ $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 4 | $val = $pkg->new( %p, '_mode' => HASH_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | elsif ( $line =~ m/ ^ $osp \( $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | $val = $pkg->new( %p, '_mode' => ARRAY_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | elsif ( $line =~ m/ ^ $osp \"( [^"]* $eol ) $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | $val = _fix_newlines( | |||||||||||||||||||||||||||||||||||||||||
| 328 | $1 . $pkg->new( %p, '_mode' => STRING_MODE ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | elsif ( $line =~ m/ ^ $osp \"([^"]+)\" $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | $val = $1; | |||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | elsif ( $line =~ m/ ^ $osp \} $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | # We saw a } that doesn't belong here. | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, "Unexpected }" ); | |||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||
| 338 | elsif ( $line =~ m/ ^ $osp \) $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | # We should only get to this line if we're nested | ||||||||||||||||||||||||||||||||||||||||||
| 341 | $p{'debug'} | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 4 | ▶ | && _debug( $p{'fh'}, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | "Returning nested array back up the chain" ); | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 4 | return $struct; | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | elsif ( $line =~ m/ ^ $osp (.*?) $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 28 | $val = $1; | |||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, "Malformed line" ); | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 32 | push @$struct, $val; | |||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | elsif ( $p{'_mode'} == STRING_MODE ) { | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | # Process in a multi-line string context | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 12 | ▶ | unless ( defined $struct ) { $struct = ''; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 12 | ▶ | if ( $line =~ m/ ^ ([^"]*) " $osp $eolc $ /x ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 4 | return $struct . $1; | |||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||
| 363 | elsif ( $line =~ m/"/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, | |||||||||||||||||||||||||||||||||||||||||
| 365 | "Unexpected mid-string quote" ); | ||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 368 | 8 | $struct .= $line; | |||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, | |||||||||||||||||||||||||||||||||||||||||
| 374 | "Unknown mode: $p{'_mode'}" ); | ||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | } ## end while ( not $p{'fh'}->eof ) | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | # If we got to the end of the file, but we weren't done processing a | ||||||||||||||||||||||||||||||||||||||||||
| 379 | # context other than top, then the file ended before we expected. | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 4 | ▶ | if ( $p{'_mode'} != TOP_MODE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | croak _error( $p{'fh'}, $line, $p{'file'}, "Premature end of file" ); | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | # Weed out any duplicate sections | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 4 | my %sections_index = (); # Keeps an index of unique sections | |||||||||||||||||||||||||||||||||||||||||
| 386 | 4 | my @sections_flattened = (); # Keeps the final list of sections in order | |||||||||||||||||||||||||||||||||||||||||
| 387 | 4 | foreach my $section (@sections) { | |||||||||||||||||||||||||||||||||||||||||
| 388 | 8 | ▶ | next if ( exists $sections_index{$section} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | 8 | $sections_index{$section} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 390 | 8 | push @sections_flattened, $section; | |||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | # Save metadata into the object | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 4 | $struct->{'_file'} = $p{'file'}; # Filename, used for | |||||||||||||||||||||||||||||||||||||||||
| 395 | # writing the file | ||||||||||||||||||||||||||||||||||||||||||
| 396 | # back out | ||||||||||||||||||||||||||||||||||||||||||
| 397 | 4 | $struct->{'_debug'} = $p{'debug'}; # Enable/disable | |||||||||||||||||||||||||||||||||||||||||
| 398 | # debugging | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 4 | $struct->{'_sections'} = \@sections_flattened; # List of sections in | |||||||||||||||||||||||||||||||||||||||||
| 400 | # order | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 4 | $struct->{'_sections_index'} = \%sections_index; # List of unique | |||||||||||||||||||||||||||||||||||||||||
| 402 | # sections | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | # We're done constructing the object, return it back up the chain | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 4 | bless $struct, $pkg; | |||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | } ## end sub new | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | # Print debugging information to STDERR | ||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | sub _debug { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_debug: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | my $fh = shift; # For the line number | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | my $message = shift; # What we're reporting | |||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | print STDERR __PACKAGE__ | |||||||||||||||||||||||||||||||||||||||||
| 416 | . " Line " | ||||||||||||||||||||||||||||||||||||||||||
| 417 | . $fh->input_line_number . ' ' | ||||||||||||||||||||||||||||||||||||||||||
| 418 | . $message . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | # Format an error message with context information about the line | ||||||||||||||||||||||||||||||||||||||||||
| 422 | # number and contents for passing to croak | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | sub _error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | my $fh = shift; # For the line number | |||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | my $line = shift; # For the contents of the line | |||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | my $file = shift; # What file we're processing (if available) | |||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | my $message = shift; # What we're complaining about | |||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | $message | |||||||||||||||||||||||||||||||||||||||||
| 433 | .= " at input line " . $fh->input_line_number . " '" . $line . "'"; | ||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | ▶ | if ($file) { $message .= " in file $file"; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | return $message; | |||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =head2 $cfg->as_hashref | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | Returns the entire INIPlus structure as a reference to a hash. | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | 4 | ▶ | sub as_hashref { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_hashref: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 449 | 4 | my $out = shift; | |||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | 4 | foreach my $key ( keys %$self ) { | |||||||||||||||||||||||||||||||||||||||||
| 452 | 36 | ▶ | next if ( $key =~ m/^_/ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | 20 | $out->{$key} = $self->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 4 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | =head2 $cfg->get( name [ , section ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | Gets the value of a particular entry. For entries within a section, the | ||||||||||||||||||||||||||||||||||||||||||
| 462 | section name must be provided. | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | my $section = shift; | |||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | ▶ | if ( defined($section) && ( $section ne '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $section and $section ne ""
MC/DC0%0/2
defined $section and $section ne ""
defined $section
$section ne ""
Truth table0%0/3
A and B
A= defined $sectionB= $section ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | return $self->{$section}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | return $self->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | =head2 $cfg->set( name, val [ , section ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | Sets the value of a particular entry. If an existing entry exists it will be | ||||||||||||||||||||||||||||||||||||||||||
| 483 | overwritten. For entries within a section, the section name must be provided. | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | my $val = shift; | |||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | my $section = shift; | |||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | 0 | ▶ | if ( ( not defined $name ) || ( $name eq '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $name or $name eq ""
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name
$name eq ""
Truth table0%0/3
not(A) or B
A= not defined $nameB= $name eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | croak "Name must be provided"; | |||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | ▶ | unless ( defined($val) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | croak "Value must be defined"; | |||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 0 | ▶ | if ( $name =~ m/^_/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 0 | croak "Keys can not begin with underscore"; | |||||||||||||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | ▶ | if ( defined($section) && ( $section ne '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $section and $section ne ""
MC/DC0%0/2
defined $section and $section ne ""
defined $section
$section ne ""
Truth table0%0/3
A and B
A= defined $sectionB= $section ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | $self->{$section}{$name} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 507 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 0 | $self->{$name} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | =head2 $cfg->del( name [ , section ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | Removes an entry. For entries within a section, the section name must be | ||||||||||||||||||||||||||||||||||||||||||
| 515 | provided. | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | ▶ | sub del { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
del: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | my $section = shift; | |||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | ▶ | if ( ( not defined $name ) || ( $name eq '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $name or $name eq ""
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name
$name eq ""
Truth table0%0/3
not(A) or B
A= not defined $nameB= $name eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | croak "Name must be provided"; | |||||||||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | ▶ | if ( defined($section) && ( $section ne '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $section and $section ne ""
MC/DC0%0/2
defined $section and $section ne ""
defined $section
$section ne ""
Truth table0%0/3
A and B
A= defined $sectionB= $section ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | delete $self->{$section}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||
| 532 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | delete $self->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | =head2 $cfg->add_section( section ) | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | Adds a new section. | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | ▶ | sub add_section { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_section: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | my $section = shift; | |||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | 0 | ▶ | if ( ( not defined $section ) || ( $section eq '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $section or $section eq ""
MC/DC0%0/2
not defined $section or $section eq ""
not defined $section
$section eq ""
Truth table0%0/3
not(A) or B
A= not defined $sectionB= $section eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 550 | 0 | croak "Section must be provided"; | |||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | ▶ | if ( $section =~ m/^_/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | croak "Sections cannot begin with underscore"; | |||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | 0 | ▶ | if ( $self->section_exists($section) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 0 | croak "Section $section already exists"; | |||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | 0 | ▶ | if ( defined $self->{$section} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 560 | 0 | croak "Cannot create a conflicting top-level section when the same " | |||||||||||||||||||||||||||||||||||||||||
| 561 | . "key name $section already exists"; | ||||||||||||||||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||||||||||||||||
| 563 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 564 | 0 | $self->{$section} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 565 | 0 | push @{ $self->{'_sections'} }, $section; | |||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | $self->{'_sections_index'}{$section} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | =head2 $cfg->section_exists( section ) | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | Returns true if a section exists, false if it does not. | ||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||
| 574 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | 5 | ▶ | sub section_exists { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
section_exists: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 579 | 5 | my $section = shift; | |||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | 5 | ▶ | if ( ( not defined $section ) || ( $section eq '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $section or $section eq ""
MC/DC0%0/2
not defined $section or $section eq ""
not defined $section
$section eq ""
Truth table33%1/3
not(A) or B
A= not defined $sectionB= $section eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | croak "Section must be provided"; | |||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | 5 | return exists $self->{'_sections_index'}{$section}; | |||||||||||||||||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||
| 588 | =head2 $cfg->sections() | ||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | Returns a list of all of the sections in the file | ||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | 0 | ▶ | sub sections { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sections: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | return @$self->{'_sections'}; | |||||||||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | =head2 $cfg->del_section( section ) | ||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||
| 603 | Removes a section. | ||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | ▶ | sub del_section { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
del_section: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | my $section = shift; | |||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | 0 | ▶ | if ( ( not defined $section ) || ( $section eq '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $section or $section eq ""
MC/DC0%0/2
not defined $section or $section eq ""
not defined $section
$section eq ""
Truth table0%0/3
not(A) or B
A= not defined $sectionB= $section eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 613 | 0 | croak "Section must be provided"; | |||||||||||||||||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | delete $self->{$section}; | |||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | delete $self->{'_sections_index'}{$section}; | |||||||||||||||||||||||||||||||||||||||||
| 618 | $self->{'_sections'} | ||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | = [ grep { !/^\Q$section\E$/ } @$self->{'_sections'} ]; | |||||||||||||||||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||
| 622 | =head2 $cfg->section_as_hashref( section ) | ||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | Retrieves a section as a reference to a hash. | ||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||
| 626 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | ▶ | sub section_as_hashref { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
section_as_hashref: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | my $section = shift; | |||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | return $self->{$section}; | |||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | =head2 $cfg->write( [ $filename ] ) | ||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | Writes out the configuration to a file to disk. If a filename is provided, | ||||||||||||||||||||||||||||||||||||||||||
| 640 | the configuration is written to that file. If the object was read from a | ||||||||||||||||||||||||||||||||||||||||||
| 641 | source filename and no filename is provided to the write method, then the | ||||||||||||||||||||||||||||||||||||||||||
| 642 | original file is overwritten. The file written will not include the | ||||||||||||||||||||||||||||||||||||||||||
| 643 | formatting or comments of the original file read by this object. | ||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||
| 645 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||
| 647 | 1 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: called CC=5 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 648 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 649 | 1 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||||||||||||
| 651 | 1 | ▶ | unless ( defined($file) && $file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file and $file
MC/DC0%0/2
defined $file and $file
defined $file
$file
Truth table33%1/3
A and B
A= defined $fileB= $file
| |||||||||||||||||||||||||||||||||||||||||||
| 652 | 0 | $file = $self->{'file'}; | |||||||||||||||||||||||||||||||||||||||||
| 653 | 0 | ▶ | unless ( defined($file) && $file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $file and $file
MC/DC0%0/2
defined $file and $file
defined $file
$file
Truth table0%0/3
A and B
A= defined $fileB= $file
| |||||||||||||||||||||||||||||||||||||||||||
| 654 | 0 | croak "You must provide a filename to write if the read " | |||||||||||||||||||||||||||||||||||||||||
| 655 | . "INIPlus file does not have an associated file name"; | ||||||||||||||||||||||||||||||||||||||||||
| 656 | } | ||||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||
| 659 | 1 | my $fh = IO::File->new( $file, 'w' ); | |||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||
| 661 | 1 | $fh->print( $self->as_string ); | |||||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||||
| 663 | } | ||||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||
| 665 | =head2 $cfg->as_string() | ||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | Retrieves the configuration as a string. This will not include the formatting | ||||||||||||||||||||||||||||||||||||||||||
| 668 | or comments of the original file read by this object. | ||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||
| 670 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||
| 672 | 5 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=23 SCAR=32.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 673 | 5 | my $obj = shift; | |||||||||||||||||||||||||||||||||||||||||
| 674 | 5 | my %p = @_; | |||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||
| 676 | 5 | ▶ | if ( not defined $p{indent_level} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 677 | 2 | $p{indent_level} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||
| 680 | 5 | ▶ | if ( not defined $p{indent_string} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 681 | 5 | $p{indent_string} = ' '; | |||||||||||||||||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||
| 684 | 5 | my $indent = $p{indent_string} x $p{indent_level}; | |||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||
| 686 | 5 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||||||||||||
| 688 | 5 | ▶ | my $at_root = blessed($obj) && $obj->isa('Config::INIPlus'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($obj) && $obj->isa('Config::INIPlus')
MC/DC50%1/2
builtin::blessed($obj) && $obj->isa('Config::INIPlus')
builtin::blessed($obj)
$obj->isa('Config::INIPlus')
Truth table66%2/3
A && B
A= builtin::blessed($obj)B= $obj->isa('Config::INIPlus')
| |||||||||||||||||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||||||||||||||||
| 690 | 5 | ▶ | if ( $at_root || ( ref $obj eq 'HASH' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$at_root or ref $obj eq "HASH"
MC/DC100%2/2
$at_root or ref $obj eq "HASH"
$at_root
ref $obj eq "HASH"
Truth table100%3/3
A or B
A= $at_rootB= ref $obj eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 691 | 4 | foreach my $key ( keys %{$obj} ) { | |||||||||||||||||||||||||||||||||||||||||
| 692 | 17 | ▶ | next if ( $key =~ m/^_/ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 693 | 13 | ▶ | next if ( $at_root && $obj->section_exists($key) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$at_root and $obj->section_exists($key)
MC/DC100%2/2
$at_root and $obj->section_exists($key)
$at_root
$obj->section_exists($key)
Truth table100%3/3
A and B
A= $at_rootB= $obj->section_exists($key)
| |||||||||||||||||||||||||||||||||||||||||||
| 694 | 12 | my $value = $obj->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 695 | 12 | ▶ | if ( ref $value eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 696 | 1 | $out .= $indent . $key . " (\n"; | |||||||||||||||||||||||||||||||||||||||||
| 697 | $out .= as_string( $value, | ||||||||||||||||||||||||||||||||||||||||||
| 698 | 1 | 'indent_level' => $p{indent_level} + 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 699 | 1 | $out .= $indent . ")\n"; | |||||||||||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||||||||||||
| 701 | elsif ( ref $value eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 702 | 1 | $out .= $indent . $key . " {\n"; | |||||||||||||||||||||||||||||||||||||||||
| 703 | $out .= as_string( $value, | ||||||||||||||||||||||||||||||||||||||||||
| 704 | 1 | 'indent_level' => $p{indent_level} + 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 705 | 1 | $out .= $indent . "}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||||||||||||||||
| 707 | elsif ( $value =~ m/"/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 708 | 0 | croak "Strings with quotes cannot be serialized"; | |||||||||||||||||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||||||||||||||||
| 710 | elsif ( $value =~ m/$eol/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 711 | 1 | $out .= $indent . "$key=\"$value\"\n"; | |||||||||||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||||||||||||
| 713 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 714 | 9 | $out .= $indent . "$key=$value\n"; | |||||||||||||||||||||||||||||||||||||||||
| 715 | } | ||||||||||||||||||||||||||||||||||||||||||
| 716 | } | ||||||||||||||||||||||||||||||||||||||||||
| 717 | 4 | $out .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 718 | 4 | ▶ | if ($at_root) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 719 | 1 | foreach my $section ( @{ $obj->{'_sections'} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 720 | 2 | $out .= "[$section]\n"; | |||||||||||||||||||||||||||||||||||||||||
| 721 | 2 | ▶ | if ( defined $obj->{$section} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 722 | 1 | $out .= as_string( $obj->{$section} ); | |||||||||||||||||||||||||||||||||||||||||
| 723 | } | ||||||||||||||||||||||||||||||||||||||||||
| 724 | 2 | $out .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 725 | } | ||||||||||||||||||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||||||||||
| 728 | elsif ( ref $obj eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 729 | 1 | foreach my $value (@$obj) { | |||||||||||||||||||||||||||||||||||||||||
| 730 | 8 | ▶ | if ( ref $value eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 731 | 0 | $out .= $indent . "(\n"; | |||||||||||||||||||||||||||||||||||||||||
| 732 | $out .= as_string( $value, | ||||||||||||||||||||||||||||||||||||||||||
| 733 | 0 | 'indent_level' => $p{indent_level} + 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 734 | 0 | $out .= $indent . ")\n"; | |||||||||||||||||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||||||||||||||||
| 736 | elsif ( ref $value eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 737 | 1 | $out .= $indent . "{\n"; | |||||||||||||||||||||||||||||||||||||||||
| 738 | $out .= as_string( $value, | ||||||||||||||||||||||||||||||||||||||||||
| 739 | 1 | 'indent_level' => $p{indent_level} + 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 740 | 1 | $out .= $indent . "}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||||||||||||||||
| 742 | elsif ( $value =~ m/"/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 743 | 0 | croak "Strings with quotes cannot be serialized"; | |||||||||||||||||||||||||||||||||||||||||
| 744 | } | ||||||||||||||||||||||||||||||||||||||||||
| 745 | elsif ( $value =~ m/$eol/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 746 | 0 | $out .= $indent . "\"$value\"\n"; | |||||||||||||||||||||||||||||||||||||||||
| 747 | } | ||||||||||||||||||||||||||||||||||||||||||
| 748 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 749 | 7 | $out .= $indent . "$value\n"; | |||||||||||||||||||||||||||||||||||||||||
| 750 | } | ||||||||||||||||||||||||||||||||||||||||||
| 751 | } | ||||||||||||||||||||||||||||||||||||||||||
| 752 | } | ||||||||||||||||||||||||||||||||||||||||||
| 753 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 754 | 0 | confess "Only INIPlus objects consisting of perl native hashes and " | |||||||||||||||||||||||||||||||||||||||||
| 755 | . "arrays can be serialized by as_string"; | ||||||||||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||
| 758 | 5 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||||||||||
| 760 | } | ||||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||
| 762 | # Takes a string and translates any newlines into whatever the local system's | ||||||||||||||||||||||||||||||||||||||||||
| 763 | # newline is | ||||||||||||||||||||||||||||||||||||||||||
| 764 | 4 | ▶ | sub _fix_newlines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fix_newlines: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 765 | 4 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 766 | 4 | $str =~ s/$eol/\n/gs; | |||||||||||||||||||||||||||||||||||||||||
| 767 | 4 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 768 | } | ||||||||||||||||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||||||||||||||||
| 770 | =head1 FAQ | ||||||||||||||||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||||||||||||||||
| 772 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||||||||
| 774 | =item Why not use YAML/JSON/XML? | ||||||||||||||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||||||||||||||
| 776 | There are times when you have existing INI files you need to maintain backward | ||||||||||||||||||||||||||||||||||||||||||
| 777 | compatibility with, but you need the ability to add richer syntax. If that's | ||||||||||||||||||||||||||||||||||||||||||
| 778 | the problem you're trying to solve, this module's for you. If you're not, then | ||||||||||||||||||||||||||||||||||||||||||
| 779 | you'll likely be better served by L<YAML>. | ||||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||
| 781 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||
| 783 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||
| 785 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||
| 787 | =item * Right now writing will preserve all data, but comments and formatting | ||||||||||||||||||||||||||||||||||||||||||
| 788 | will be lost | ||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||
| 790 | =item * Since double quotes are used to contain multi-line strings, they are | ||||||||||||||||||||||||||||||||||||||||||
| 791 | not allowed in values. This behaviour is different than most other | ||||||||||||||||||||||||||||||||||||||||||
| 792 | INI parsers. | ||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | =item * Obviously any of the formatting which allows for nested arrays and | ||||||||||||||||||||||||||||||||||||||||||
| 795 | hashes will not be compatible with existing INI parsers | ||||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||
| 797 | =item * Keys and section names cannot start with an underscore | ||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||
| 799 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||||||||||||
| 801 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||
| 803 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||
| 805 | =item * L<Config::INI> - The most popular module for reading and writing INI | ||||||||||||||||||||||||||||||||||||||||||
| 806 | files | ||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||
| 808 | =item * L<YAML> - A non-INI way of reading and writing nested structures into | ||||||||||||||||||||||||||||||||||||||||||
| 809 | config files | ||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||
| 811 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||||||||||
| 813 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||||||||||||
| 815 | Anthony Kilna, C<< <anthony at kilna dot com> >> - L<http://anthony.kilna.com> | ||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||
| 817 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||||||||
| 819 | Please report any bugs or feature requests to C<bug-config-iniplus at | ||||||||||||||||||||||||||||||||||||||||||
| 820 | rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||||||||||||||||
| 821 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-INIPlus>. I will be | ||||||||||||||||||||||||||||||||||||||||||
| 822 | notified, and then you'll automatically be notified of progress on your bug | ||||||||||||||||||||||||||||||||||||||||||
| 823 | as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||||||||||||||||
| 825 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||
| 827 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||||||||||||
| 829 | perldoc Config::INIPlus | ||||||||||||||||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||||||||||||||||
| 831 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||
| 833 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||
| 835 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||||||||
| 837 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Config-INIPlus> | ||||||||||||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||||||||||
| 839 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||
| 841 | L<http://annocpan.org/dist/Config-INIPlus> | ||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||
| 843 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||
| 845 | L<http://cpanratings.perl.org/d/Config-INIPlus> | ||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||
| 847 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||||||||
| 849 | L<http://search.cpan.org/dist/Config-INIPlus> | ||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||
| 851 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||
| 853 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||
| 855 | Copyright 2012 Kilna Companies. | ||||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||
| 857 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 858 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||
| 859 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||
| 861 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||||||||||||||
| 863 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||
| 865 | 3 | 1; # End of Config::INIPlus | |||||||||||||||||||||||||||||||||||||||||