Subject: | Enhancements: Edge case for undef file and fetch with no path returns hashref |
Hello friends,
I made a couple of small adjustments for which I've attached a patch for
your review:
1. I found that if I had a YAML file that only had the document
separator (---) in it, the merge would fail and only show configuration
merged AFTER this blank file. This is an edge case, but I have some
strange data files, so I thought I would patch.
2. I wanted to be able to get back the whole configuration (_config)
hashref from the fetch() method, however, it would throw warnings about
undefined variables if I just passed an undefined path. So I wrapped the
path parsing in a conditional and now all works fine.
Thanks for your fine work.
Harlan
Subject: | hbarnes.patch |
diff -rupN Config-Path-0.11/lib/Config/Path.pm Config-Path/lib/Config/Path.pm
--- Config-Path-0.11/lib/Config/Path.pm 2011-01-31 10:25:51.000000000 -0500
+++ Config-Path/lib/Config/Path.pm 2012-08-10 15:18:50.000000000 -0400
@@ -196,6 +196,7 @@ sub _build__config {
foreach my $file (@{ $files }) {
# Double check that it exists, as Config::Any might not have loaded it
next unless exists $anyconf->{$file};
+ next unless defined $anyconf->{$file};
$config = $merge->merge($config, $anyconf->{$file});
}
if(defined($config)) {
@@ -243,21 +244,27 @@ sub fetch {
return $self->_mask->{$path} if exists($self->_mask->{$path});
}
- $path =~ s/^\///g; # Remove leading slashes, as they don't do anything
- # and there's no reason to break over it.
-
my $conf = $self->_config;
- foreach my $piece (split(/\//, $path)) {
- if(ref($conf) eq 'HASH') {
- $conf = $conf->{$piece};
- } elsif(ref($conf) eq 'ARRAY' && $piece =~ /\d+/) {
- $conf = $conf->[$piece];
- } else {
- # Not sure what they asked for, but it's not gonna work. Maybe a
- # string member of an array?
- $conf = undef;
+
+ # you should be able to pass nothing and get a hashref back
+ if ( defined $path ) {
+
+ $path =~ s/^\///g; # Remove leading slashes, as they don't do anything
+ # and there's no reason to break over it.
+
+ foreach my $piece (split(/\//, $path)) {
+ if(ref($conf) eq 'HASH') {
+ $conf = $conf->{$piece};
+ } elsif(ref($conf) eq 'ARRAY' && $piece =~ /\d+/) {
+ $conf = $conf->[$piece];
+ } else {
+ # Not sure what they asked for, but it's not gonna work. Maybe a
+ # string member of an array?
+ $conf = undef;
+ }
+ return undef unless defined($conf);
}
- return undef unless defined($conf);
+
}
if ( $self->convert_empty_to_undef ) {