Config-Files-Simple

 view release on metacpan or  search on metacpan

lib/Config/Files/Simple.pm  view on Meta::CPAN


=over 4

=item * config

=back

=head1 SUBROUTINES/METHODS

=head2 config

Read configuration file from current path.
It needs a YAML file.

=cut

sub config {
    $_hr_config = _check_hashref( $_[0] ) if ( $_[0] );
    return $_hr_config if $_hr_config;
    if ( -f 'config.yaml' ) {
        return config_file( 'config.yaml', 'YAML' );
    }
    elsif ( -f 'config.yml' ) {
        return config_file( 'config.yml', 'YAML' );
    }
    else {
        require Carp;
        Carp::cluck('could not find a config.yml or config.yaml file');
        die;
    }
}

=head2 config_file

Read configuration file from given path.

=cut

sub config_file {
    if ( -f $_[0] && defined $_[1] ) {
        require Module::Load;
        my $loading_package = "Config::Files::Simple::$_[1]";
        Module::Load::load $loading_package;
        $_hr_config = _check_hashref( $loading_package->new->config_file( $_[0] ) );
        return $_hr_config;
    }
    else {
        require Carp;
        Carp::cluck("could not find $_[0] file");
    }
    return undef;
}

=head2 _check_hashref

private hashref checking sub

=cut

sub _check_hashref {
    require Ref::Util;
    return $_[0] if ( Ref::Util::is_hashref( $_[0] ) );
    require Carp;
    Carp::cluck('config data must be a hashref');
}

1;

__END__

=pod
 
=head1 SYNOPSIS
 
Sample if no config file is given
    
    ...
    use Config::Files::Simple qw/config config_file/;

    #set your config
    Config::Files::Simple::config({ key => 'value'});
    

    #read config from specific file
    Config::Files::Simple::config_file('/path/to/config.yml', 'YAML');

    ...

=head1 DESCRIPTION
 
Simple and stupid config reader.
 
=head1 CONFIGURATION
 
Configuration can be automatically parsed from a `config.yaml` or `config.yml`
file  in the current working directory, or it can be explicitly set with the
C<config> function:
 
    Config::Files::Simple::config({ key => 'value'});
 
If you want the config to be autoloaded from a yaml config file, just make sure
to put your config data under a top level C<git_sugar> key.
 
=head1 AUTHOR

Mario Zieschang, C<< <mziescha at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-Config-Files-Simple at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-Files-Simple>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.


=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Config::Files::Simple


You can also look for information at:



( run in 2.211 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )