| 1 | ############################################################################## | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (c) 2001-2002 Jan 'Kozo' Vajda <Jan.Vajda@pobox.sk> | ||||||||||||||||||||||||||||
| 4 | # All rights reserved. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | ############################################################################## | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Tie::Config; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Tie::Hash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 6 | @ISA = qw(Tie::StdHash); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Items to export into callers namespace by default | ||||||||||||||||||||||||||||
| 25 | 6 | @EXPORT = qw(); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Other items we are prepared to export if requested | ||||||||||||||||||||||||||||
| 28 | 6 | @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 NAME | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Tie::Config - class definitions for tied hashes config file reading | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | use Tie::Config; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | tie %hash, 'Tie::Config', ".foo_rc", O_RDWR; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Tied config file reader | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 USE | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | use Tie::Config; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | tie %hash, 'Tie::Config', ".foo_rc", O_RDWR; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | print $hash{'key'}; | ||||||||||||||||||||||||||||
| 52 | $hash{'key'} = "newvalue"; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | untie %hash; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Comments is handled internal and is wroted at the top of the file. | ||||||||||||||||||||||||||||
| 57 | If ommited access mode default is O_RDONLY ( read only access ). | ||||||||||||||||||||||||||||
| 58 | Currently supported mode is only O_RDONLY and O_RDWR. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | If config file is changed between tie and untie by other proces, any changes | ||||||||||||||||||||||||||||
| 61 | will be lost. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=12 SCAR=25.3
| |||||||||||||||||||||||||||||
| 66 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 67 | 6 | my $file = shift; | |||||||||||||||||||||||||||
| 68 | 6 | ▶ | my $access = shift || O_RDONLY ; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | my $hash = {}; | |||||||||||||||||||||||||||
| 71 | 6 | bless $hash, $class; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 6 | $hash->{_internal_filename} = $file; | |||||||||||||||||||||||||||
| 74 | 6 | $hash->{_internal_accessmode} = $access; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 6 | ▶ | carp("File ".$hash->{_internal_filename}." does not exist.") unless ( -f $hash->{_internal_filename}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 6 | ▶ | my $rc = IO::File->new($hash->{_internal_filename}, O_RDONLY) if ( -f $hash->{_internal_filename} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 6 | ▶ | if ( defined $rc ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 6 | my $separator = '\s*=\s*'; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | ### pre istotu vymazem komentare | ||||||||||||||||||||||||||||
| 84 | 6 | $hash->{_internal_comments} = ''; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 6 | ▶ | while (<$rc>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 48 | chomp; | |||||||||||||||||||||||||||
| 88 | #### Skip blank text entry fields | ||||||||||||||||||||||||||||
| 89 | 48 | ▶ | next if ( /^\s*$/o ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | ### get comments | ||||||||||||||||||||||||||||
| 91 | 44 | ▶ | if ( /^\s*#/o || /^\s*\;/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
/^\s*#/o or /^\s*\;/o
MC/DC100%2/2
/^\s*#/o or /^\s*\;/o
/^\s*#/o
/^\s*\;/o
Truth table100%3/3
A or B
A= /^\s*#/oB= /^\s*\;/o
| |||||||||||||||||||||||||||||
| 92 | ### pridam do pola komentarov | ||||||||||||||||||||||||||||
| 93 | 10 | $hash->{_internal_comments} .= $_ . "\n"; | |||||||||||||||||||||||||||
| 94 | 10 | next; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | ### Skip unless contain separator | ||||||||||||||||||||||||||||
| 97 | 34 | ▶ | next unless ( /${separator}/o ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 34 | my ($key,$value) = /\s*(.*?)${separator}(.*?)\s*$/o; | |||||||||||||||||||||||||||
| 100 | 34 | my $length = length($value); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | ### skip empty keys | ||||||||||||||||||||||||||||
| 103 | 34 | ▶ | next if ( !$length || !$key ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$length and $key
MC/DC0%0/2
$length and $key
$length
$key
Truth table33%1/3
A and B
A= $lengthB= $key
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 34 | $hash->{$key} = $value; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 6 | $rc->close; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 0 | carp("Canot open file ".$hash->{_internal_filename}); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 6 | $hash; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 3 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=5 SCAR=17.6
| |||||||||||||||||||||||||||||
| 118 | 3 | my ($self, $key, $val) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # print STDERR "$self, $key, $val\n"; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | ▶ | if ( $key =~ /^_internal/o ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | carp "invalid key [$key] in hash"; | |||||||||||||||||||||||||||
| 124 | 0 | return; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3 | ▶ | if ( !$self->{_internal_accessmode} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1 | carp "hash is read only"; | |||||||||||||||||||||||||||
| 129 | 1 | return; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | ▶ | return($val) if ( defined $self->{$key} && $self->{$key} eq $val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{$key} and $self->{$key} eq $val
MC/DC0%0/2
defined $self->{$key} and $self->{$key} eq $val
defined $self->{$key}
$self->{$key} eq $val
Truth table33%1/3
A and B
A= defined $self->{$key}B= $self->{$key} eq $val
| |||||||||||||||||||||||||||||
| 133 | 2 | $self->{_internal_changed} = '1'; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # print STDERR "hash content changed\n"; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 2 | $self->{$key} = $val; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=8 SCAR=21.6
| |||||||||||||||||||||||||||||
| 141 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 1 | my ($key,$value); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # print STDERR "Destroyed\n"; | ||||||||||||||||||||||||||||
| 145 | # print STDERR Data::Dumper->Dump([$self],[qw(*destroyed)]); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | ### is read only | ||||||||||||||||||||||||||||
| 148 | 1 | ▶ | return() unless ($self->{_internal_accessmode}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | ### is changed | ||||||||||||||||||||||||||||
| 151 | 1 | ▶ | return() unless ($self->{_internal_changed}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # print STDERR "untied\n"; | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | my $rc = new IO::File $self->{_internal_filename}, O_CREAT|O_WRONLY|O_TRUNC; | |||||||||||||||||||||||||||
| 156 | 1 | ▶ | if ( defined $rc ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | ### zapiseme komentare ak existuju | ||||||||||||||||||||||||||||
| 159 | 1 | ▶ | print $rc $self->{_internal_comments} if $self->{_internal_comments}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | my $separator = ' = '; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 1 | ▶ | while (($key,$value) = each %{$self}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 4 | ▶ | print $rc "$key${separator}$value\n" unless ($key =~ /^_internal/o); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 1 | $rc->close; | |||||||||||||||||||||||||||
| 167 | 1 | ▶ | carp "Can't close file ".$self->{_internal_filename} .": $1" if $?; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } else { | ||||||||||||||||||||||||||||
| 169 | 0 | carp "Can't open ".$self->{_internal_filename}; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 1 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 174 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 1 | my ($key,$value); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # print STDERR "CLEAR !!\n"; | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 1 | ▶ | while (($key,$value) = each %{$self}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 10 | ▶ | delete $self->{$key} unless ($key =~ /^_internal/o); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | 1 | ▶ | delete $self->{_internal_comments} if $self->{_internal_comments}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | #sub AUTOLOAD { | ||||||||||||||||||||||||||||
| 186 | # my $self = shift; | ||||||||||||||||||||||||||||
| 187 | # my $value = shift; | ||||||||||||||||||||||||||||
| 188 | # my ($name) = $AUTOLOAD; | ||||||||||||||||||||||||||||
| 189 | # | ||||||||||||||||||||||||||||
| 190 | # ($name) = ( $name =~ /^.*::(.*)/); | ||||||||||||||||||||||||||||
| 191 | # | ||||||||||||||||||||||||||||
| 192 | # $self->{$name} = $value if ( defined $value ); | ||||||||||||||||||||||||||||
| 193 | # | ||||||||||||||||||||||||||||
| 194 | # return($self->{$name}); | ||||||||||||||||||||||||||||
| 195 | # | ||||||||||||||||||||||||||||
| 196 | #} | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | ### set True | ||||||||||||||||||||||||||||
| 199 | 6 | 3.14; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | __END__ | ||||||||||||||||||||||||||||