| 1 | package Tie::Cfg; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use LockFile::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use vars qw($VERSION %cnf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | $VERSION="0.32"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 36 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||
| 10 | 36 | my $class = shift; | |||||||||||||||||||||||||||
| 11 | 36 | my $args = { READ => undef, | |||||||||||||||||||||||||||
| 12 | WRITE => undef, | ||||||||||||||||||||||||||||
| 13 | MODE => 0640, | ||||||||||||||||||||||||||||
| 14 | LOCK => undef, | ||||||||||||||||||||||||||||
| 15 | SEP => "=", | ||||||||||||||||||||||||||||
| 16 | REGSEP => undef, | ||||||||||||||||||||||||||||
| 17 | COMMENT => ";", | ||||||||||||||||||||||||||||
| 18 | REGCOMMENT => undef, | ||||||||||||||||||||||||||||
| 19 | CHANGE => undef, | ||||||||||||||||||||||||||||
| 20 | @_ | ||||||||||||||||||||||||||||
| 21 | }; | ||||||||||||||||||||||||||||
| 22 | 36 | my $file = $args->{READ}; | |||||||||||||||||||||||||||
| 23 | 36 | my $outfile = $args->{WRITE}; | |||||||||||||||||||||||||||
| 24 | 36 | my $lock = $args->{LOCK}; | |||||||||||||||||||||||||||
| 25 | 36 | my $mode = $args->{MODE}; | |||||||||||||||||||||||||||
| 26 | 36 | my $sep = $args->{SEP}; | |||||||||||||||||||||||||||
| 27 | 36 | my $splitsep= $args->{REGSEP}; | |||||||||||||||||||||||||||
| 28 | 36 | my $comment = $args->{COMMENT}; | |||||||||||||||||||||||||||
| 29 | 36 | my $regcmnt = $args->{REGCOMMENT}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 36 | ▶ | if (not $splitsep) { $splitsep=$sep; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 36 | ▶ | if (not $regcmnt) { $regcmnt=$comment; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 36 | my %cnf = (); | |||||||||||||||||||||||||||
| 35 | 36 | my $fh; | |||||||||||||||||||||||||||
| 36 | my $val; | ||||||||||||||||||||||||||||
| 37 | 36 | my $key; | |||||||||||||||||||||||||||
| 38 | 36 | my $prekey=""; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 36 | ▶ | $outfile="" if (not $outfile); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 36 | my $node = { | |||||||||||||||||||||||||||
| 43 | CNF => {}, | ||||||||||||||||||||||||||||
| 44 | FILE => $outfile, | ||||||||||||||||||||||||||||
| 45 | MODE => $mode, | ||||||||||||||||||||||||||||
| 46 | LOCK => undef, | ||||||||||||||||||||||||||||
| 47 | SEP => $sep, | ||||||||||||||||||||||||||||
| 48 | SSEP => $splitsep, | ||||||||||||||||||||||||||||
| 49 | CMNT => $comment, | ||||||||||||||||||||||||||||
| 50 | SCMNT => $regcmnt, | ||||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 36 | ▶ | if ($lock and $outfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$lock and $outfile
MC/DC50%1/2
$lock and $outfile
$lock
$outfile
Truth table66%2/3
A and B
A= $lockB= $outfile
| |||||||||||||||||||||||||||||
| 54 | 10 | $node->{LOCK}=LockFile::Simple->make(-max => 30, -delay => 1, -nfs => 1); | |||||||||||||||||||||||||||
| 55 | 10 | $node->{LOCK}->lock($outfile); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 36 | ▶ | if (-e $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 27 | my $section=""; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 27 | open $fh, $file; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 27 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 683 | ▶ | next if /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 661 | ▶ | next if /^\s*$regcmnt/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 639 | ▶ | if (/^\s*\[.*\]\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 126 | $section=$_; | |||||||||||||||||||||||||||
| 70 | 126 | $section=~s/^\s*\[//; | |||||||||||||||||||||||||||
| 71 | 126 | $section=~s/\]\s*$//; | |||||||||||||||||||||||||||
| 72 | 126 | $section=~s/^\s+//; | |||||||||||||||||||||||||||
| 73 | 126 | $section=~s/\s+$//; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 126 | $prekey=""; | |||||||||||||||||||||||||||
| 76 | 126 | for (split /\./,$section) { | |||||||||||||||||||||||||||
| 77 | 246 | $prekey.="{$_}"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 126 | next; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 513 | ($key,$val) = split /$splitsep/,$_,2; | |||||||||||||||||||||||||||
| 84 | 513 | $key=~s/^\s+//;$key=~s/\s+$//; | |||||||||||||||||||||||||||
| 85 | 513 | $val=~s/^\s+//;$val=~s/\s+$//; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 513 | for my $chg (@{$args->{CHANGE}}) { | |||||||||||||||||||||||||||
| 88 | 2 | $val=__change($chg,$val); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 513 | ▶ | if ($key=~/([\[][0-9]+[\]])$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 132 | my $index; | |||||||||||||||||||||||||||
| 93 | 132 | $index=$key; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 132 | $key=~s/([\[][0-9]+[\]])$//; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 132 | $index=substr($index,length($key)); | |||||||||||||||||||||||||||
| 98 | 132 | $index=~s/[\[]//; | |||||||||||||||||||||||||||
| 99 | 132 | $index=~s/[\]]//; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 132 | eval('$cnf'."$prekey"."{$key}[$index]=".'"'."$val".'"'); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 381 | eval('$cnf'."$prekey"."{$key}=".'"'."$val".'"'); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 27 | close $fh; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 27 | $node->{CNF}=\%cnf; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 36 | my $this=bless $node, $class; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 36 | return $this; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub getHash { | ||||||||||||||||||||||||||
|
Subroutine
getHash: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | 0 | my $s=shift; | |||||||||||||||||||||||||||
| 120 | 0 | my $r; | |||||||||||||||||||||||||||
| 121 | 0 | ($s,$r)=split /\./,$s,2; | |||||||||||||||||||||||||||
| 122 | 0 | my %c; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | return $c{s} | ||||||||||||||||||||||||||||
| 125 | 0 | } | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | sub __change { | ||||||||||||||||||||||||||
|
Subroutine
__change: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 128 | 2 | my $change=shift; | |||||||||||||||||||||||||||
| 129 | 2 | my $value=shift; | |||||||||||||||||||||||||||
| 130 | 2 | my $from; | |||||||||||||||||||||||||||
| 131 | my $to; | ||||||||||||||||||||||||||||
| 132 | 2 | my $g; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | if (not $to) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 2 | ▶ | if ($change=~/g$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | $g="g"; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 2 | $change=~s/^s//; | |||||||||||||||||||||||||||
| 139 | 2 | $change=~s/^\///; | |||||||||||||||||||||||||||
| 140 | 2 | $change=~s/g$//; | |||||||||||||||||||||||||||
| 141 | 2 | $change=~s/\/$//; | |||||||||||||||||||||||||||
| 142 | 2 | ($from,$to)=split /\//,$change,2; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | if (not defined $g) { $g=""; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | ▶ | if ($g eq "g") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | $value=~s/$from/$to/g; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||
| 151 | 2 | $value=~s/$from/$to/; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | $value; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 551 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 551 | my $self = shift; | |||||||||||||||||||||||||||
| 161 | 551 | my $key = shift; | |||||||||||||||||||||||||||
| 162 | 551 | return $self->{CNF}->{$key}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 88 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 166 | 88 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | 88 | my $key = shift; | |||||||||||||||||||||||||||
| 168 | 88 | my $val = shift; | |||||||||||||||||||||||||||
| 169 | 88 | $self->{CNF}->{$key}=$val; | |||||||||||||||||||||||||||
| 170 | 88 | return $val; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 174 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 0 | my $key = shift; | |||||||||||||||||||||||||||
| 176 | 0 | delete $self->{CNF}->{$key}; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 55 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 180 | 55 | my $self = shift; | |||||||||||||||||||||||||||
| 181 | 55 | my $key = shift; | |||||||||||||||||||||||||||
| 182 | 55 | return exists $self->{CNF}->{$key}; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 186 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 0 | my $temp = keys %{$self->{CNF}}; | |||||||||||||||||||||||||||
| 188 | 0 | return scalar each %{$self->{CNF}}; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 192 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 193 | 0 | return scalar each %{$self->{CNF}}; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 36 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 197 | 36 | my $self = shift; | |||||||||||||||||||||||||||
| 198 | 36 | my $fh; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 36 | ▶ | if ($self->{FILE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 31 | open $fh,">",$self->{FILE}; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 31 | print $fh $self->{CMNT}," Tie::Cfg version $VERSION (c) H. Oesterholt-Dijkema, license perl\n"; | |||||||||||||||||||||||||||
| 204 | 31 | print $fh "\n"; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 31 | __write_self($self->{CNF},$fh,0,$self->{SEP},$self->{CMNT},""); | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 31 | close $fh; | |||||||||||||||||||||||||||
| 209 | 31 | chmod $self->{MODE},$self->{FILE}; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 31 | ▶ | if ($self->{LOCK}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 10 | $self->{LOCK}->unlock($self->{FILE}); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 208 | ▶ | sub __write_self { | ||||||||||||||||||||||||||
|
Subroutine
__write_self: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 219 | 208 | my $cfg = shift; | |||||||||||||||||||||||||||
| 220 | 208 | my $fh = shift; | |||||||||||||||||||||||||||
| 221 | 208 | my $depth = shift; | |||||||||||||||||||||||||||
| 222 | 208 | my $sep = shift; | |||||||||||||||||||||||||||
| 223 | 208 | my $cmnt = shift; | |||||||||||||||||||||||||||
| 224 | 208 | my $section = shift; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 208 | my $key; | |||||||||||||||||||||||||||
| 227 | my $value; | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # Pass 1, Keys that are no sections | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 208 | ▶ | if ($section) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 177 | print $fh "[$section]\n"; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 208 | ▶ | while (($key,$value) = each %{$cfg}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 599 | ▶ | if (ref($value) ne "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 422 | ▶ | if (ref($value) eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 15 | my $idx=0; | |||||||||||||||||||||||||||
| 239 | 15 | for my $element (@{$value}) { | |||||||||||||||||||||||||||
| 240 | 165 | print $fh "$key","[$idx]","$sep","$element\n"; | |||||||||||||||||||||||||||
| 241 | 165 | $idx+=1; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | else { | ||||||||||||||||||||||||||||
| 245 | 407 | print $fh "$key","$sep","$value\n"; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # Pass 2, keys that are sections | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 208 | ▶ | while (($key,$value) = each %{$cfg}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 599 | ▶ | if (ref($value) eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | # OK, It's a section | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 177 | ▶ | if ($depth==0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 69 | __write_self($value,$fh,$depth+1,$sep,$cmnt,$key); | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | else { | ||||||||||||||||||||||||||||
| 260 | 108 | __write_self($value,$fh,$depth+1,$sep,$cmnt,$section.".".$key); | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =pod | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =head1 NAME | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | Tie::Cfg - Ties simple configuration (.ini) files to hashes. | ||||||||||||||||||||||||||||
| 273 | Handles arrays and recurrent sections. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =head1 WARNING | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | This version breaks previous versions as the default mode is '.ini' mode. | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | use Tie::Cfg; | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | ### Sample 1 | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | tie my %conf, 'Tie::Cfg', | ||||||||||||||||||||||||||||
| 286 | READ => "/etc/connect.cfg", | ||||||||||||||||||||||||||||
| 287 | WRITE => "/etc/connect.cfg", | ||||||||||||||||||||||||||||
| 288 | MODE => 0600, | ||||||||||||||||||||||||||||
| 289 | LOCK => 1; | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | $conf{test}="this is a test"; | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | untie %conf; | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | ### Sample 2 | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | my $limit="10000k"; | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | tie my %files, 'Tie::Cfg', | ||||||||||||||||||||||||||||
| 300 | READ => "find $dirs -xdev -type f -size +$limit -printf \"%h/%f:%k\\n\" |", SEP => ':'; | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | if (exists $files{"/etc/passwd"}) { | ||||||||||||||||||||||||||||
| 303 | print "You've got a /etc/passwd file!\n"; | ||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | while (($file,$size) = each %newdb) { | ||||||||||||||||||||||||||||
| 307 | print "Wow! Another file bigger than $limit ($size)\n"; | ||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | untie %files; | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | ### Sample 3 | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | tie my %cfg, 'Tie::Cfg', READ => "config.cfg", WRITE => "config.cfg", SEP => ':', COMMENT => '#'; | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | my $counter=$cfg{"counter"}; | ||||||||||||||||||||||||||||
| 317 | $counter+=1; | ||||||||||||||||||||||||||||
| 318 | $cfg{"counter"}=$counter; | ||||||||||||||||||||||||||||
| 319 | $cfg{"counter"}+=1; | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | untie %cfg; | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | ### Reading and writing an INI file | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | tie my %ini, 'Tie::Cfg', READ => "config.ini", WRITE => "config.ini"; | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | my $counter=$ini{"section1"|{"counter1"}; | ||||||||||||||||||||||||||||
| 328 | $counter+=1; | ||||||||||||||||||||||||||||
| 329 | $ini{"section1"}{"counter1"}=$counter; | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | untie %ini; | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | ### INI file with subsections | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | tie my %ini, 'Tie::Cfg', READ => "config.ini"; | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | my $counter=$ini{"section1"}{"counter1"}; | ||||||||||||||||||||||||||||
| 338 | $counter+=1; | ||||||||||||||||||||||||||||
| 339 | $ini{"section1"}{"counter1"}=$counter; | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | $ini{"section1"}{"subsection1"}{"parameter"}="value"; | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | my @array; | ||||||||||||||||||||||||||||
| 344 | for(1..10) { push @array,$_; } | ||||||||||||||||||||||||||||
| 345 | $ini{"section1"}{"array"}{"a"}=@array; | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | untie %ini; | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | ### CHANGE option | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | tie my %paths, 'Tie::Cfg', READ => "paths.ini", | ||||||||||||||||||||||||||||
| 352 | CHANGE => [ "s/PREFIX/$myprefix/", "s/CONF/$myconfidir/" ]; | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # Do something here | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | untie %paths; | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | This module reads in a configuration file at 'tie' and writes it at 'untie'. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | You can use file locking to prevent others from accessing the configuration file, | ||||||||||||||||||||||||||||
| 364 | but this should only be used if the configuration file is used as a small data file | ||||||||||||||||||||||||||||
| 365 | to hold a few entries that can be concurrently accessed. | ||||||||||||||||||||||||||||
| 366 | Note! In this case a persistent ".lock" file will be created. | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | Mode is used to set access permissions; defaults to 0640. It's only set | ||||||||||||||||||||||||||||
| 369 | if a file should be written (i.e. using the WRITE keyword). | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | INIMODE lets you choose between Windows alike .ini configuration files and simple | ||||||||||||||||||||||||||||
| 372 | key[:=]value entried files. | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | Sections are addressed using a hash within a hash: For a tied %cfg the assignment: | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | $cfg{"section"}{"key"}="value" | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | will write in the configuration file: | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | [section] | ||||||||||||||||||||||||||||
| 381 | key=value | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | Keys that end on [\[][0-9]+[\]] will be interpreted as arrays and will show up | ||||||||||||||||||||||||||||
| 384 | in the tied hash as an array element. For example: | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | [array-section] | ||||||||||||||||||||||||||||
| 387 | var[0]=1 | ||||||||||||||||||||||||||||
| 388 | var[1]=2 | ||||||||||||||||||||||||||||
| 389 | var[2]=3 | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | will show up in a tied %cfg hash like: | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | for (0..2) { | ||||||||||||||||||||||||||||
| 394 | print $cfg{"array-section"}{"var"}[$_],"\n"; | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | Hashes of hashes are permitted: | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | $cfg{"key"}{"subkey"}{"subsubkey"}{"subsubsubkey"}{"par"}="value"; | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | will show up in the configuration file as: | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | [key.subkey.subsubkey.subsubsubkey] | ||||||||||||||||||||||||||||
| 404 | par=value | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =head1 PREREQUISITE | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Perl's Version >= 5.6.0! Please don't test this module with | ||||||||||||||||||||||||||||
| 410 | anything earlier. | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | Hans Oesterholt-Dijkema <hans@oesterholt-dijkema.emailt.nl> | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head1 BUGS | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | Probably. | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =head1 LICENCE | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | Perl. | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =end | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||