Subject: | read_config_file does not return a hash reference upon an empty config file |
Date: | Tue, 2 Sep 2014 17:27:30 +0200 |
To: | bug-Config-File [...] rt.cpan.org |
From: | Axel Beckert <abe [...] deuxchevaux.org> |
Hi Gunnar,
as promised on IRC, here's the bug report:
The POD says:
Function "read_config_file"
[...]
Return value
This function returns a hash reference. Each key of the hash is
a value defined in the configuration file.
This is not true for an empty configuration file or for /dev/null:
$ perl -MConfig::File -MData::Dumper -le 'print Dumper Config::File::read_config_file("/dev/null")'
$VAR1 = undef;
$
I would have expected "$VAR1 = {};" here according to the
documentation.
The following patch would add a test for this case and fixes the
issue, but will require at least Perl 5.8 for the defined-or operator.
Additionally the module still throws warnings about not finding any
"=" being read, so maybe it's not the best patch.
diff --git a/lib/Config/File.pm b/lib/Config/File.pm
index fcdd2ed..6d85fe1 100755
--- a/lib/Config/File.pm
+++ b/lib/Config/File.pm
@@ -48,7 +48,7 @@ sub read_config_file($) {
}
$fh->close;
- return $conf;
+ return $conf // {};
}
1;
diff --git a/t/empty_config b/t/empty_config
new file mode 100644
index 0000000..e69de29
diff --git a/t/test.t b/t/test.t
index 3fabfca..8bc45cc 100755
--- a/t/test.t
+++ b/t/test.t
@@ -6,7 +6,7 @@ use Test;
# First check that the module loads OK.
use vars qw($loaded);
-BEGIN { $| = 1; plan tests => 11; }
+BEGIN { $| = 1; plan tests => 13; }
END {print "not ok 1\n" unless $loaded;}
use Config::File;
@@ -39,3 +39,7 @@ ok($config->{dummy}->{3}, 'data 3');
print "! Testing whether we correctly ignore invalid keys\n";
ok(scalar(keys %$config), 6);
+
+my $empty_config = Config::File::read_config_file("t/empty_config");
+ok(defined($empty_config));
+ok(ref($empty_config),'HASH');
Kind regards, Axel
--
/~\ Plain Text Ribbon Campaign | Axel Beckert
\ / Say No to HTML in E-Mail and News | abe@deuxchevaux.org (Mail)
X See http://www.nonhtmlmail.org/campaign.html | abe@noone.org (Mail+Jabber)
/ \ I love long mails: http://email.is-not-s.ms/ | http://noone.org/abe/ (Web)