~mitya57/pkg-kde-tools/0.15.16ubuntu1

« back to all changes in this revision

Viewing changes to datalib/Dpkg/Interface/Storable.pm

  • Committer: Bazaar Package Importer
  • Author(s): Modestas Vainius
  • Date: 2010-04-08 13:41:29 UTC
  • mfrom: (0.1.20 sid)
  • Revision ID: james.westby@ubuntu.com-20100408134129-bsr1dutc0fnb5192
Tags: 0.7.0.1
Remove perl from pkg-kde-tools Depends to workaround (and use to our own
advantage) brokeness in experimental buildds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <http://www.gnu.org/licenses/>.
 
15
 
 
16
package Dpkg::Interface::Storable;
 
17
 
 
18
use strict;
 
19
use warnings;
 
20
 
 
21
our $VERSION = "1.00";
 
22
 
 
23
use Dpkg::Gettext;
 
24
use Dpkg::ErrorHandling;
 
25
use Dpkg::Compression::FileHandle;
 
26
 
 
27
use overload
 
28
    '""' => \&_stringify,
 
29
    'fallback' => 1;
 
30
 
 
31
=head1 NAME
 
32
 
 
33
Dpkg::Interface::Storable - common methods related to object serialization
 
34
 
 
35
=head1 DESCRIPTION
 
36
 
 
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().
 
40
 
 
41
=head1 BASE METHODS
 
42
 
 
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.
 
45
 
 
46
=over 4
 
47
 
 
48
=item $obj->parse($fh, $desc)
 
49
 
 
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.
 
53
 
 
54
=item $string = $obj->output($fh)
 
55
 
 
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).
 
58
 
 
59
=back
 
60
 
 
61
=head1 PROVIDED METHODS
 
62
 
 
63
=over 4
 
64
 
 
65
=item $obj->load($filename)
 
66
 
 
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).
 
71
 
 
72
=cut
 
73
 
 
74
sub load {
 
75
    my ($self, $file, @options) = @_;
 
76
    unless ($self->can("parse")) {
 
77
        internerr("%s cannot be loaded, it lacks the parse method", ref($self));
 
78
    }
 
79
    my ($desc, $fh) = ($file, undef);
 
80
    if ($file eq "-") {
 
81
        $fh = \*STDIN;
 
82
        $desc = _g("<standard input>");
 
83
    } else {
 
84
        $fh = Dpkg::Compression::FileHandle->new();
 
85
        open($fh, "<", $file) || syserr(_g("cannot read %s"), $file);
 
86
    }
 
87
    my $res = $self->parse($fh, $desc, @options);
 
88
    if ($file ne "-") {
 
89
        close($fh) || syserr(_g("cannot close %s"), $file);
 
90
    }
 
91
    return $res;
 
92
}
 
93
 
 
94
=item $obj->save($filename)
 
95
 
 
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).
 
100
 
 
101
=cut
 
102
 
 
103
sub save {
 
104
    my ($self, $file, @options) = @_;
 
105
    unless ($self->can("output")) {
 
106
        internerr("%s cannot be saved, it lacks the output method", ref($self));
 
107
    }
 
108
    my $fh;
 
109
    if ($file eq "-") {
 
110
        $fh = \*STDOUT;
 
111
    } else {
 
112
        $fh = Dpkg::Compression::FileHandle->new();
 
113
        open($fh, ">", $file) || syserr(_g("cannot write %s"), $file);
 
114
    }
 
115
    $self->output($fh, @options);
 
116
    if ($file ne "-") {
 
117
        close($fh) || syserr(_g("cannot close %s"), $file);
 
118
    }
 
119
}
 
120
 
 
121
=item "$obj"
 
122
 
 
123
Return a string representation of the object.
 
124
 
 
125
=cut
 
126
 
 
127
sub _stringify {
 
128
    my ($self) = @_;
 
129
    unless ($self->can("output")) {
 
130
        internerr("%s cannot be stringified, it lacks the output method", ref($self));
 
131
    }
 
132
    return $self->output();
 
133
}
 
134
 
 
135
=back
 
136
 
 
137
=head1 AUTHOR
 
138
 
 
139
Raphaël Hertzog <hertzog@debian.org>.
 
140
 
 
141
=cut
 
142
 
 
143
1;