/usr/share/perl5/ConfigReader/Spec.pm is in libconfigreader-perl 0.5-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | # ConfigReader/Spec.pm: specifies a set of configuration directives
#
# Copyright 1996 by Andrew Wilcox <awilcox@world.std.com>.
# All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
package ConfigReader::Spec;
$VERSION = "0.5";
my $This_file = __FILE__; # used to get our filename out of error msgs
require 5.001;
use Carp;
use strict;
=head1 NAME
ConfigReader::Spec - stores specifications of configuration directives
=head1 DESCRIPTION
The ConfigReader::Spec class stores a specification about
configuration directives: their names, whether they are required or if
they have default values, and what parsing function or method to use.
=cut
## Public methods
sub new {
my ($class) = @_;
my $self = {directives => {}, # directive name => 1
alias_to_directive => {}, # map alias to name
default => {}, # name => default value
whence_default => {}, # name => source location of default
parser => {}, # name => value parser
name => {}, # name => 1, ignore this directive
required => {} # name => 1, required directive
};
return bless $self, $class;
}
sub directives {
my ($self) = @_;
return keys %{$self->{'directives'}};
}
sub value {
my ($self, $directive, $values, $whence) = @_;
$directive = $self->canonical_name($directive);
my $name = $self->{'alias_to_directive'}{$directive};
$self->_error("Undefined directive '$directive'", $whence)
unless defined $name;
$self->_error("The directive '$directive' has not been assigned a value",
$whence)
unless exists($values->{$name});
return $values->{$name};
}
sub alias {
my ($self, $directive, @aliases) = @_;
$directive = $self->canonical_name($directive);
my $alias;
foreach $alias (@aliases) {
$self->{'alias_to_directive'}{$self->canonical_name($alias)} =
$directive;
}
}
sub define_directive {
my ($self, $directive, $parser, $whence) = @_;
my ($name, @aliases);
my $ref = ref($directive);
if (defined $ref and $ref eq 'ARRAY') {
$name = shift @$directive;
@aliases = @$directive;
}
else {
$name = $directive;
@aliases = ($directive);
}
$name = $self->canonical_name($name);
$self->{'directives'}{$name} = 1;
$self->alias($name, @aliases);
if (defined $parser) {
$self->{'parser'}{$name} =
$self->_resolve_code($parser,
'specified as parser',
$whence);
}
else {
delete $self->{'parser'};
}
return $name;
}
sub required {
my ($self, $directive, $parser, $whence) = @_;
my $name = $self->define_directive($directive,
$parser,
$whence);
$self->{'required'}{$name} = 1;
}
sub directive {
my ($self, $directive, $parser, $default, $whence) = @_;
my $name = $self->define_directive($directive,
$parser,
$whence);
$self->{'default'}{$name} = $default;
$self->{'whence_default'}{$name} = $whence;
return $name;
}
sub ignore {
my ($self, $directive, $whence) = @_;
my $name = $self->define_directive($directive, undef, undef, $whence);
$self->{'ignore'}{$name} = 1;
}
sub assign {
my ($self, $directive, $value_string, $values, $whence) = @_;
$directive = $self->canonical_name($directive);
my $name = $self->{'alias_to_directive'}{$directive};
$self->undefined_directive($directive, $value_string, $whence)
unless defined $name;
return undef if $self->{'ignore'}{$name};
$self->duplicate_directive($directive, $value_string, $whence)
if defined $values and exists $values->{$name};
if (not defined $value_string) {
$values->{$name} = undef if defined $values;
return undef;
}
my $parser = $self->{parser}{$name};
my $value;
if (defined $parser) {
my @warnings = ();
local $SIG{'__WARN__'} = sub { push @warnings, $_[0] };
my $saved_eval_error = $@;
eval { $value = &$parser($value_string) };
my $error = $@;
$@ = $saved_eval_error;
my $warning;
foreach $warning (@warnings) {
$warning =~ s/ at $This_file line \d+$//o; # uncarp
if (defined $whence) {
warn
"While parsing '$value_string' as the value for the
'$directive' directive as specified
$whence,
I got this warning:
$warning";
}
else {
$warning =~ s/\n?$/\n/;
carp $warning .
" while parsing '$value_string' as the value for the '$directive' directive";
}
}
if ($error) {
$error =~ s/ at $This_file line \d+$//o; # uncroak
if (defined $whence) {
$whence =~ s,\n$,,;
die
"I tried to parse '$value_string' as the value for the '$directive' directive as specified $whence
but the following error occurred:
$error";
}
else {
$error =~ s/\n?$/\n/;
croak $error."while parsing '$value_string' as the value for the '$directive' directive";
}
}
}
else {
$value = $value_string;
}
$values->{$name} = $value if defined $values;
return $value;
}
sub assign_defaults {
my ($self, $values, $whence) = @_;
my $name;
foreach $name ($self->directives()) {
$self->assign_default($name, $values, $whence);
}
}
sub assign_default {
my ($self, $directive, $values, $whence) = @_;
$directive = $self->canonical_name($directive);
my $name = $self->{'alias_to_directive'}{$directive};
$self->_error("Undefined directive '$directive'", $whence)
unless defined $name;
return $values->{$name} if defined $values and exists $values->{$name};
if ($self->{'required'}{$name}) {
$self->_error("Please specify the '$name' directive", $whence);
}
elsif ($self->{'ignore'}{$name}) {
return undef;
}
my $default = $self->{'default'}{$name};
# "as the default value "
my $whence_default = $self->{'whence_default'}{$name};
my $value;
if (not defined $default) {
return $self->assign($name, undef, $values, $whence_default);
}
elsif (not ref $default) {
return $self->assign($name, $default, $values, $whence_default);
}
elsif (ref($default) eq 'CODE') {
local $SIG{'__DIE__'} = sub {
$self->_error("$_[0]\nwhile assigning the default value for the '$name' directive", $whence_default);
};
$value = &$default();
$values->{$name} = $value if defined $values;
return $value;
}
else {
$value = $default;
$values->{$name} = $value if defined $values;
return $value;
}
}
## subclass hooks
sub canonical_name {
my ($self, $directive) = @_;
return $directive;
}
sub undefined_directive {
my ($self, $directive, $value_string, $whence) = @_;
$self->_error("Unknown directive '$directive' specified", $whence);
}
sub duplicate_directive {
my ($self, $directive, $value_string, $whence) = @_;
$self->_error("Duplicate directive '$directive' specified", $whence);
}
## Internal methods
# Allows the user to specify code to run in several different ways.
# Returns a code ref that will run the desired code.
# 'new URI::URL' calls static method 'new' in class 'URI::URL'
# $coderef calls the code ref
# [new => 'URI::URL'] calls new URI::URL
# [parse => $obj] calls $obj->parse()
sub _resolve_code {
my ($self, $sub, $purpose, $whence) = @_;
my ($r, $class, $static_method, $function);
$r = ref($sub);
if (not $r) {
if (($static_method, $class) = ($sub =~ m/^(\w+) \s+ ([\w:]+)$/x)) {
return sub {
$class->$static_method(@_);
};
}
else {
$self->_error("Syntax error in function name '$sub' $purpose",
$whence);
}
}
elsif ($r eq 'CODE') {
return $sub;
}
elsif ($r eq 'ARRAY') {
my ($method, $class_or_obj) = @$sub;
$self->_error("Empty array used to $purpose", $whence)
unless defined $method;
$self->_error("Class or object not specified in array used to $purpose",
$whence)
unless defined $class_or_obj;
return sub {
$class_or_obj->$method(@_);
};
}
else {
$self->_error("Unknown object $purpose", $whence);
}
}
sub _error {
my ($self, $msg, $whence) = @_;
if (defined $whence) {
$whence =~ s,\n?$,\n,;
die "$msg $whence";
}
else {
croak $msg;
}
}
1;
|