Config-Simple
view release on metacpan or search on metacpan
for bug fixes and suggestions
=back
=head1 COPYRIGHT
Copyright (C) 2002-2003 Sherzod B. Ruzmetov.
This software is free library. You can modify and/or distribute it
under the same terms as Perl itself
=head1 AUTHOR
Sherzod B. Ruzmetov E<lt>sherzodr@cpan.orgE<gt>
URI: http://author.handalak.com
=head1 SEE ALSO
L<Config::General>, L<Config::Simple>, L<Config::Tiny>
=cut
# Following methods are loaded on demand.
# returns all the keys as a hash or hashref
sub vars {
my $self = shift;
# it might seem we should have used get_param() or param()
# methods to make the task easier, but param() itself uses
# vars(), so it will result in a deep recursion
my %vars = ();
my $syntax = $self->{_SYNTAX} or die "'_SYNTAX' is not defined";
if ( $syntax eq 'ini' ) {
while ( my ($block, $values) = each %{$self->{_DATA}} ) {
while ( my ($k, $v) = each %{$values} ) {
$vars{"$block.$k"} = (@{$v} > 1) ? $v : $v->[0];
}
}
} else {
while ( my ($k, $v) = each %{$self->{_DATA}} ) {
$vars{$k} = (@{$v} > 1) ? $v : $v->[0];
}
}
return wantarray ? %vars : \%vars;
}
# imports names into the caller's namespace as global variables.
# I'm not sure how secure this method is. Hopefully someone will
# take a look at it for me
sub import_names {
my ($self, $namespace) = @_;
unless ( defined $namespace ) {
$namespace = (caller)[0];
}
if ( $namespace eq 'Config::Simple') {
croak "You cannot import into 'Config::Simple' package";
}
my %vars = $self->vars();
no strict 'refs';
while ( my ($k, $v) = each %vars ) {
$k =~ s/\W/_/g;
${$namespace . '::' . uc($k)} = $v;
}
}
# imports names from a file. Compare with import_names.
sub import_from {
my ($class, $file, $arg) = @_;
if ( ref($class) ) {
croak "import_from() is not an object method.";
}
# this is a hash support
if ( defined($arg) && (ref($arg) eq 'HASH') ) {
my $cfg = $class->new($file) or return;
map { $arg->{$_} = $cfg->param($_) } $cfg->param();
return $cfg;
}
# following is the original version of our import_from():
unless ( defined $arg ) {
$arg = (caller)[0];
}
my $cfg = $class->new($file) or return;
$cfg->import_names($arg);
return $cfg;
}
sub error {
my ($self, $msg) = @_;
if ( $msg ) {
$errstr = $msg;
}
return $errstr;
}
sub dump {
my ($self, $file, $indent) = @_;
require Data::Dumper;
my $d = new Data::Dumper([$self], [ref $self]);
$d->Indent($indent||2);
if ( defined $file ) {
sysopen(FH, $file, O_WRONLY|O_CREAT|O_TRUNC, 0666) or die $!;
print FH $d->Dump();
CORE::close(FH) or die $!;
}
return $d->Dump();
}
sub verbose {
DEBUG or return;
carp "****[$0]: " . join ("", @_);
}
#------------------
# tie() interface
#------------------
sub TIEHASH {
my ($class, $file, $args) = @_;
unless ( defined $file ) {
croak "Usage: tie \%config, 'Config::Simple', \$filename";
}
return $class->new($file);
}
sub FETCH {
( run in 1.728 second using v1.01-cache-2.11-cpan-5511b514fd6 )