I would most welcome comments regarding my proposed module Config::File.

It's plus points (for me) are: Update
After reading the replies below, my first thought was to modify it to use Safe, and add options to new to allow for carp, croak or undef on non-existant keys.
However,
After re-reading YAML and the specs, I've been won over! I see the light! I feel happy!
(I'm not kidding)

The POD

NAME

Config::File


SYNOPSIS

OO usage

use Config::File; $conf = Config::File->new( file => $file, path => $path ); print $conf->key;

Procedural usage, retrieving a hash

use Config::File 'config'; %hash = config( file => $file, path => $path ); print $hash{key};

Procedural usage, retrieving a hashref

use Config::File 'config'; $hashref = config( file => $file, path => $path ); print $hashref->{key};

DESCRIPTION

Alows complex configuration information such as nested arrays and hashes to be written in plain text files.

The config file must be in the form of an anonymous perl hash structure.

Using the OO method allows access to configuration values with a minimum of typing and also provides a safety mechanism, such that any call to a non-existant configuration key is fatal.

A procedural method 'config' is provided which will return the entire configuration data structure as a hash or hashref.

The procedural method is about 5% faster than the OO method. Using a hashref is about 1 - 2% faster than using a normal hash.


EXPORT

Exports 'config' subroutine on request.


EXAMPLE

For an example, running this code...

use Config::File; $conf = Config::File->new(file => 'my.conf'); print $conf->db_password, $/; print $conf->blocked_words->[0], $/; print $conf->emails->{webmaster}, $/;

...on the file 'my.conf'...

{ db_password => 'secret', blocked_words => ['list', 'of', 'words'], emails => { sales => 'sales@fireartist.com', webmaster => 'webmaster@fireartist.com', } }

...would give the output...

secret list webmaster@fireartist.com

And the code...

package Config::File; $VERSION = '0.30'; use strict; require Exporter; use vars qw($VERSION @ISA @EXPORT_OK $AUTOLOAD); @ISA = qw(Exporter); @EXPORT_OK = qw/config/; use Carp; use File::Spec; sub new { my $class = shift; my %arg = @_; my $self = {}; bless( $self, $class ); my $file = _file_path( $arg{file}, $arg{path} ); $self->{conf} = do($file) or croak('new() could not load config file'); return $self; } sub config { my $self = shift if ref $_[0]; my %arg = @_; my $file = _file_path( $arg{file}, $arg{path} ); my $conf = do($file) or croak('hash() could not load config file'); return (wantarray ? %{ $conf } : $conf); } sub _file_path { my ($file, $path) = @_; my $exists; croak('_file_path requires file arg') unless defined $file; if ($path && -e File::Spec->catfile( $path, $file )) { $exists = File::Spec->catfile( $path, $file ); } elsif ( -e $file ) { $exists = $file; } else { for (@INC) { if (-e File::Spec->catfile( $_, $file )) { $exists = File::Spec->catfile( $_, $file ); last; } } } croak('could not find config file') unless defined $exists; return $exists; } sub AUTOLOAD { my $self = shift; my $method = $AUTOLOAD; $method =~ s/.*://; unless (defined $self->{conf}->{$method}) { croak($method.' not defined in config file'); } return $self->{conf}->{$method} if not wantarray; if (ref $self->{conf}->{$method} eq 'HASH') { return %{ $self->{conf}->{$method} }; } elsif (ref $self->{conf}->{$method} eq 'ARRAY') { return @{ $self->{conf}->{$method} }; } else { return $self->{conf}->{$method}; } } sub DESTROY { } 1;

In reply to RFC: Config::File by fireartist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.