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 <http://www.gnu.org/licenses/>.
16
package Dpkg::Interface::Storable;
21
our $VERSION = "1.00";
24
use Dpkg::ErrorHandling;
25
use Dpkg::Compression::FileHandle;
33
Dpkg::Interface::Storable - common methods related to object serialization
37
Dpkg::Interface::Storable is only meant to be used as parent
38
class for other objects. It provides common methods that are
39
all implemented on top of two basic methods parse() and output().
43
Those methods must be provided by the object that wish to inherit
44
from Dpkg::Interface::Storable so that the methods provided can work.
48
=item $obj->parse($fh, $desc)
50
This methods initialize the object with the data stored in the
51
filehandle. $desc is optional and is a textual description of
52
the filehandle used in error messages.
54
=item $string = $obj->output($fh)
56
This method returns a string representation of the object in $string
57
and it writes the same string to $fh (if it's defined).
61
=head1 PROVIDED METHODS
65
=item $obj->load($filename)
67
Initialize the object with the data stored in the file. The file can be
68
compressed, it will be uncompressed on the fly by using a
69
Dpkg::Compression::FileHandle object. If $filename is "-", then the
70
standard input is read (no compression is allowed in that case).
75
my ($self, $file, @options) = @_;
76
unless ($self->can("parse")) {
77
internerr("%s cannot be loaded, it lacks the parse method", ref($self));
79
my ($desc, $fh) = ($file, undef);
82
$desc = _g("<standard input>");
84
$fh = Dpkg::Compression::FileHandle->new();
85
open($fh, "<", $file) || syserr(_g("cannot read %s"), $file);
87
my $res = $self->parse($fh, $desc, @options);
89
close($fh) || syserr(_g("cannot close %s"), $file);
94
=item $obj->save($filename)
96
Store the object in the file. If the filename ends with a known
97
compression extension, it will be compressed on the fly by using a
98
Dpkg::Compression::FileHandle object. If $filename is "-", then the
99
standard output is used (data are written uncompressed in that case).
104
my ($self, $file, @options) = @_;
105
unless ($self->can("output")) {
106
internerr("%s cannot be saved, it lacks the output method", ref($self));
112
$fh = Dpkg::Compression::FileHandle->new();
113
open($fh, ">", $file) || syserr(_g("cannot write %s"), $file);
115
$self->output($fh, @options);
117
close($fh) || syserr(_g("cannot close %s"), $file);
123
Return a string representation of the object.
129
unless ($self->can("output")) {
130
internerr("%s cannot be stringified, it lacks the output method", ref($self));
132
return $self->output();
139
Raphaël Hertzog <hertzog@debian.org>.