1 # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
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.
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.
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/>.
16 package Dpkg::Interface::Storable;
21 our $VERSION = '1.00';
26 use Dpkg::ErrorHandling;
27 use Dpkg::Compression::FileHandle;
37 Dpkg::Interface::Storable - common methods related to object serialization
41 Dpkg::Interface::Storable is only meant to be used as parent
42 class for other objects. It provides common methods that are
43 all implemented on top of two basic methods parse() and output().
47 Those methods must be provided by the object that wish to inherit
48 from Dpkg::Interface::Storable so that the methods provided can work.
52 =item $obj->parse($fh, $desc)
54 This methods initialize the object with the data stored in the
55 filehandle. $desc is optional and is a textual description of
56 the filehandle used in error messages.
58 =item $string = $obj->output($fh)
60 This method returns a string representation of the object in $string
61 and it writes the same string to $fh (if it's defined).
65 =head1 PROVIDED METHODS
69 =item $obj->load($filename)
71 Initialize the object with the data stored in the file. The file can be
72 compressed, it will be uncompressed on the fly by using a
73 Dpkg::Compression::FileHandle object. If $filename is "-", then the
74 standard input is read (no compression is allowed in that case).
79 my ($self, $file, @options) = @_;
80 unless ($self->can('parse')) {
81 croak ref($self) . ' cannot be loaded, it lacks the parse method';
83 my ($desc, $fh) = ($file, undef);
86 $desc = g_('<standard input>');
88 $fh = Dpkg::Compression::FileHandle->new();
89 open($fh, '<', $file) or syserr(g_('cannot read %s'), $file);
91 my $res = $self->parse($fh, $desc, @options);
93 close($fh) or syserr(g_('cannot close %s'), $file);
98 =item $obj->save($filename)
100 Store the object in the file. If the filename ends with a known
101 compression extension, it will be compressed on the fly by using a
102 Dpkg::Compression::FileHandle object. If $filename is "-", then the
103 standard output is used (data are written uncompressed in that case).
108 my ($self, $file, @options) = @_;
109 unless ($self->can('output')) {
110 croak ref($self) . ' cannot be saved, it lacks the output method';
116 $fh = Dpkg::Compression::FileHandle->new();
117 open($fh, '>', $file) or syserr(g_('cannot write %s'), $file);
119 $self->output($fh, @options);
121 close($fh) or syserr(g_('cannot close %s'), $file);
127 Return a string representation of the object.
133 unless ($self->can('output')) {
134 croak ref($self) . ' cannot be stringified, it lacks the output method';
136 return $self->output();
143 =head2 Version 1.00 (dpkg 1.15.6)
145 Mark the module as public.