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
16=encoding utf8
17
18 - 28
=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().

=cut
29
30package Dpkg::Interface::Storable 1.01;
31
32
153
153
153
29475
145
2268
use strict;
33
153
153
153
271
103
3148
use warnings;
34
35
153
153
153
273
86
3794
use Carp;
36
37
153
153
153
691
101
4002
use Dpkg::Gettext;
38
153
153
153
802
118
6975
use Dpkg::ErrorHandling;
39
40use overload
41
153
635
    '""' => \&_stringify,
42
153
153
12975
77766
    'fallback' => 1;
43
44 - 76
=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
L<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
420
1
783
    my ($self, $file, %opts) = @_;
80
420
1370
    $opts{compression} //= 1;
81
420
1284
    unless ($self->can('parse')) {
82
0
0
        croak ref($self) . ' cannot be loaded, it lacks the parse method';
83    }
84
420
572
    my ($desc, $fh) = ($file, undef);
85
420
602
    if ($file eq '-') {
86
0
0
        $fh = \*STDIN;
87
0
0
        $desc = g_('<standard input>');
88    } else {
89
420
778
        if ($opts{compression}) {
90
351
11054
            require Dpkg::Compression::FileHandle;
91
351
1665
            $fh = Dpkg::Compression::FileHandle->new();
92        }
93
420
2113
        open($fh, '<', $file) or syserr(g_('cannot read %s'), $file);
94    }
95
420
1261
    my $res = $self->parse($fh, $desc, %opts);
96
399
780
    if ($file ne '-') {
97
399
1059
        close($fh) or syserr(g_('cannot close %s'), $file);
98    }
99
399
1608
    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
L<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
18
1
296
    my ($self, $file, %opts) = @_;
114
18
184
    $opts{compression} //= 1;
115
18
100
    unless ($self->can('output')) {
116
0
0
        croak ref($self) . ' cannot be saved, it lacks the output method';
117    }
118
18
24
    my $fh;
119
18
56
    if ($file eq '-') {
120
0
0
        $fh = \*STDOUT;
121    } else {
122
18
52
        if ($opts{compression}) {
123
18
8302
            require Dpkg::Compression::FileHandle;
124
18
156
            $fh = Dpkg::Compression::FileHandle->new();
125        }
126
18
58
        open($fh, '>', $file) or syserr(g_('cannot write %s'), $file);
127    }
128
18
214
    $self->output($fh, %opts);
129
18
62
    if ($file ne '-') {
130
18
72
        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
171
3398
    my $self = shift;
142
171
649
    unless ($self->can('output')) {
143
0
0
        croak ref($self) . ' cannot be stringified, it lacks the output method';
144    }
145
171
269
    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;