File Coverage

File:Dpkg/Interface/Storable.pm
Coverage:79.6%

linestmtbrancondsubpodtimecode
1# Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16package Dpkg::Interface::Storable;
17
18
49
49
49
7198
44
573
use strict;
19
49
49
49
71
34
1001
use warnings;
20
21our $VERSION = '1.01';
22
23
49
49
49
81
28
952
use Carp;
24
25
49
49
49
190
26
989
use Dpkg::Gettext;
26
49
49
49
202
47
1622
use Dpkg::ErrorHandling;
27
28use overload
29
49
120
    '""' => \&_stringify,
30
49
49
3770
19771
    'fallback' => 1;
31
32=encoding utf8
33
34 - 76
=head1 NAME

Dpkg::Interface::Storable - common methods related to object serialization

=head1 DESCRIPTION

Dpkg::Interface::Storable is only meant to be used as parent
class for other classes. It provides common methods that are
all implemented on top of two basic methods parse() and output().

=head1 BASE METHODS

Those methods must be provided by the class that wish to inherit
from Dpkg::Interface::Storable so that the methods provided can work.

=over 4

=item $obj->parse($fh[, $desc])

This methods initialize the object with the data stored in the
filehandle. $desc is optional and is a textual description of
the filehandle used in error messages.

=item $string = $obj->output([$fh])

This method returns a string representation of the object in $string
and it writes the same string to $fh (if it's defined).

=back

=head1 PROVIDED METHODS

=over 4

=item $obj->load($filename, %opts)

Initialize the object with the data stored in the file. The file can be
compressed, it will be decompressed on the fly by using a
Dpkg::Compression::FileHandle object. If $opts{compression} is false the
decompression support will be disabled. If $filename is "-", then the
standard input is read (no compression is allowed in that case).

=cut
77
78sub load {
79
131
1
169
    my ($self, $file, %opts) = @_;
80
131
333
    $opts{compression} //= 1;
81
131
246
    unless ($self->can('parse')) {
82
0
0
        croak ref($self) . ' cannot be loaded, it lacks the parse method';
83    }
84
131
135
    my ($desc, $fh) = ($file, undef);
85
131
139
    if ($file eq '-') {
86
0
0
        $fh = \*STDIN;
87
0
0
        $desc = g_('<standard input>');
88    } else {
89
131
147
        if ($opts{compression}) {
90
109
2291
            require Dpkg::Compression::FileHandle;
91
109
364
            $fh = Dpkg::Compression::FileHandle->new();
92        }
93
131
440
        open($fh, '<', $file) or syserr(g_('cannot read %s'), $file);
94    }
95
131
241
    my $res = $self->parse($fh, $desc, %opts);
96
124
143
    if ($file ne '-') {
97
124
229
        close($fh) or syserr(g_('cannot close %s'), $file);
98    }
99
124
268
    return $res;
100}
101
102 - 110
=item $obj->save($filename, %opts)

Store the object in the file. If the filename ends with a known
compression extension, it will be compressed on the fly by using a
Dpkg::Compression::FileHandle object. If $opts{compression} is false the
compression support will be disabled. If $filename is "-", then the
standard output is used (data are written uncompressed in that case).

=cut
111
112sub save {
113
6
1
76
    my ($self, $file, %opts) = @_;
114
6
38
    $opts{compression} //= 1;
115
6
20
    unless ($self->can('output')) {
116
0
0
        croak ref($self) . ' cannot be saved, it lacks the output method';
117    }
118
6
6
    my $fh;
119
6
6
    if ($file eq '-') {
120
0
0
        $fh = \*STDOUT;
121    } else {
122
6
16
        if ($opts{compression}) {
123
6
1536
            require Dpkg::Compression::FileHandle;
124
6
16
            $fh = Dpkg::Compression::FileHandle->new();
125        }
126
6
10
        open($fh, '>', $file) or syserr(g_('cannot write %s'), $file);
127    }
128
6
14
    $self->output($fh, %opts);
129
6
14
    if ($file ne '-') {
130
6
14
        close($fh) or syserr(g_('cannot close %s'), $file);
131    }
132}
133
134 - 138
=item "$obj"

Return a string representation of the object.

=cut
139
140sub _stringify {
141
57
797
    my $self = shift;
142
57
149
    unless ($self->can('output')) {
143
0
0
        croak ref($self) . ' cannot be stringified, it lacks the output method';
144    }
145
57
62
    return $self->output();
146}
147
148=back
149
150 - 161
=head1 CHANGES

=head2 Version 1.01 (dpkg 1.19.0)

New options: The $obj->load() and $obj->save() methods support a new
compression option.

=head2 Version 1.00 (dpkg 1.15.6)

Mark the module as public.

=cut
162
1631;