1# Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
2# Copyright © 2012 Guillem Jover <guillem@debian.org>
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17package Dpkg::File;
18
19use strict;
20use warnings;
21
22our $VERSION = '0.01';
23our @EXPORT = qw(
24    file_slurp
25);
26
27use Exporter qw(import);
28use Scalar::Util qw(openhandle);
29
30use Dpkg::ErrorHandling;
31use Dpkg::Gettext;
32
33sub file_slurp {
34    my $file = shift;
35    my $fh;
36    my $doclose = 0;
37
38    if (openhandle($file)) {
39        $fh = $file;
40    } else {
41        open $fh, '<', $file or syserr(g_('cannot read %s'), $fh);
42        $doclose = 1;
43    }
44    local $/;
45    my $data = <$fh>;
46    close $fh if $doclose;
47
48    return $data;
49}
50
511;
52