Config-YAML

 view release on metacpan or  search on metacpan

lib/Config/YAML.pm  view on Meta::CPAN


    die("Can't create Config::YAML object with no config file.\n") 
        if ($_[0] ne "config");
    shift; $priv{config} = shift;

    if (@_ && ($_[0] eq "output")) { shift; $priv{output} = shift; }
    if (@_ && ($_[0] eq "strict")) { shift; $priv{strict} = shift; }

    my $self = bless { _infile   => $priv{config},
                       _outfile  => $priv{output}   || $priv{config},
                       _strict   => $priv{strict}   || 0,
                     }, $class;

    %args = @_;
    @{$self}{keys %args} = values %args;

    $self->read;
    return $self;
}

=head2 get_*/set_*

If you'd prefer not to directly molest the object to store and
retrieve configuration data, autoloading methods of the forms
C<get_[param]> and C<set_[param]> are provided. Continuing from the
previous example:

    print $c->get_foo;      # prints "abc"
    my $val = $c->get_quux; # $c->{quux} doesn't exist; returns undef

    $c->set_bar(30);     # $c->{bar} now equals 30, not "xyz"
    my @list = qw(alpha beta gamma);
    $c->set_baz(\@list); # $c->{baz} now a reference to @list

=cut

sub Config::YAML::AUTOLOAD {
    no strict 'refs';
    my ($self, $newval) = @_;

    if ($AUTOLOAD =~ /.*::get_(\w+)/) {
        my $attr = $1;
        return undef if (!defined $self->{$attr});
        *{$AUTOLOAD} = sub { return $_[0]->{$attr} };
        return $self->{$attr};
    }

    if ($AUTOLOAD =~ /.*::set_(\w+)/) {
        my $attr = $1;
        *{$AUTOLOAD} = sub { $_[0]->{$attr} = $_[1]; return };
        $self->{$attr} = $newval;
        return;
    }
}

=head2 fold

Convenience method for folding multiple values into the config object
at once. Requires a hashref as its argument.

    $prefs{theme}  = param(theme);
    $prefs{format} = param(format);
    $prefs{sortby} = param(order);

    $c->fold(\%prefs);

    my $format = $c->get_format; # value matches that of param(format)

=cut

sub fold {
    my ($self, $data) = @_;
    # add check for HASHREF when strict mode is implemented
    @{$self}{keys %{$data}} = values %{$data};
}

=head2 read

Imports a YAML-formatted config file.

    $c->read('/usr/share/fooapp/fooconf');

C<read()> is called at object creation and imports the file specified
by C<< new(config=>) >>, so there is no need to call it manually
unless multiple config files exist.

=cut

sub read {
    my ($self, $file) = @_;
    $self->{_infile} = $file if $file;

    my $yaml;
    my $line;

    open(FH,'<',$self->{_infile}) or die "Can't open $self->{_infile}; $!\n";
    while ($line = <FH>) {
        next if ($line =~ /^\-{3,}/);
        next if ($line =~ /^#/);
        next if ($line =~ /^$/);
        $yaml .= $line;
    }
    close(FH);

    my $tmpyaml = Load($yaml);
    @{$self}{keys %{$tmpyaml}} = values %{$tmpyaml}; # woo, hash slice
}

=head2 write

Dump current configuration state to a YAML-formatted flat file.

    $c->write;

The file to be written is specified in the constructor call. See the
C<new> method documentation for details.

=cut

sub write {
    my $self = shift;
    my %tmpyaml;

    # strip out internal state parameters
    while(my($k,$v) = each%{$self}) {



( run in 1.163 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )