summaryrefslogtreecommitdiff
path: root/scripts/Dpkg/File.pm
diff options
context:
space:
mode:
authorGuillem Jover <guillem@debian.org>2018-12-24 03:05:05 +0100
committerGuillem Jover <guillem@debian.org>2019-01-15 03:42:39 +0100
commit39eb793a685273f520b25179bf118f8845ece0f6 (patch)
tree6c5ad397c42ee8d9ef98b73faaf5afd5ebc69ae4 /scripts/Dpkg/File.pm
parente326eda15c84d0456aa2e1c22c996e89ef6c40f2 (diff)
downloaddpkg-39eb793a685273f520b25179bf118f8845ece0f6.tar.gz
Dpkg::File: Make file_slurp() also accept pathnames in addition to filehandles
This makes several call sites more clear, as we move the logic inside the function.
Diffstat (limited to 'scripts/Dpkg/File.pm')
-rw-r--r--scripts/Dpkg/File.pm16
1 files changed, 15 insertions, 1 deletions
diff --git a/scripts/Dpkg/File.pm b/scripts/Dpkg/File.pm
index 884923852..6ba49a6e6 100644
--- a/scripts/Dpkg/File.pm
+++ b/scripts/Dpkg/File.pm
@@ -25,12 +25,26 @@ our @EXPORT = qw(
);
use Exporter qw(import);
+use Scalar::Util qw(openhandle);
+
+use Dpkg::ErrorHandling;
+use Dpkg::Gettext;
sub file_slurp {
- my $fh = shift;
+ my $file = shift;
+ my $fh;
+ my $doclose = 0;
+ if (openhandle($file)) {
+ $fh = $file;
+ } else {
+ open $fh, '<', $file or syserr(g_('cannot read %s'), $fh);
+ $doclose = 1;
+ }
local $/;
my $data = <$fh>;
+ close $fh if $doclose;
+
return $data;
}