Config-YAML-Modern
view release on metacpan or search on metacpan
lib/Config/YAML/Modern.pm view on Meta::CPAN
!!! important - in this case loaded or added data are NOT BE STORED in object, use it well
=item * C<ignore_empty_file>
If setted to true method:
- load_file() will return or assign to object empty flat hash without created keys by file name - just {}.
- load_dir() will ignore empty files and not been add keys by names of empty files at all
- add_file() and add_dir() will ignore empty files and not use it in merge process
By default empty files NOT ignored, value by default - 'undef'.
=back
=cut
sub new {
my $class = shift;
my $arg = {
__config => {},
merge_behavior => 'LEFT_PRECEDENT',
file_suffix => '.yaml',
key_conversion => undef,
i_dont_use_suffix => undef,
__force_return_data => undef,
ignore_empty_file => undef,
@_
};
my $self = bless( $arg, ref $class || $class );
return $self;
}
=head2 load_file
load_file($filename) - load data from yaml-contained file
$config->load_file($filename);
=cut
sub load_file {
my $self = shift;
my $filename = shift;
unless ( defined $filename ) {
croak sprintf $err_text->[0];
}
unless ( -e $filename ) {
croak sprintf $err_text->[1], $filename;
}
# this block for filename to hash key resolving
# et my.config.yaml -> { my => { config => { $data_here } } }
my ( $filename_for_hash, undef, $suffix ) =
fileparse( $filename, qr/\.[^.]*/ );
my @file_part = split m/\./, $filename_for_hash;
# I care about all of you, but it bad practice!!!
if ( defined $self->{'i_dont_use_suffix'} ) {
$suffix =~ s/^\.//;
# fix empty key addition
push @file_part, $suffix if ( $suffix ne '' );
}
# if we are need key conversation
my $key_conv = $self->{key_conversion};
@file_part = $key_conversion->( $key_conv, @file_part )
if ( defined $key_conv );
# now we are go to load file
my $config_value = {};
my $temp_val;
eval { $temp_val = LoadFile($filename) };
croak sprintf $err_text->[3], $filename, $@ while ($@);
DiveVal( $config_value, @file_part ) = $temp_val;
# return empty hash if file empty to suppress vanish data by empty file
if ( !defined $temp_val && defined $self->{'ignore_empty_file'} ) {
$config_value = {};
}
# for dir_load, or you are may use it, if you want
return $config_value while ( defined $self->{__force_return_data} );
# or get classical $self for chaining
$self->{'__config'} = $config_value;
return $self;
}
=head2 load_dir
load_dir($directory) - get files from directory (non-recursive), load data and merge it together
$config2->load_dir($directory);
=cut
sub load_dir {
my $self = shift;
my $dir = shift;
unless ( defined $dir ) {
croak sprintf $err_text->[4];
}
unless ( -d $dir ) {
croak sprintf $err_text->[5], $dir;
}
my @file_list = $get_files_list->( $self, $dir );
( run in 0.445 second using v1.01-cache-2.11-cpan-71847e10f99 )