| 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 | =head1 NAME | ||||||||||||
| 19 | |||||||||||||
| 20 | Dpkg::Interface::Storable - common methods related to object serialization | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||
| 23 | |||||||||||||
| 24 | Dpkg::Interface::Storable is only meant to be used as parent | ||||||||||||
| 25 | class for other classes. It provides common methods that are | ||||||||||||
| 26 | all implemented on top of two basic methods parse() and output(). | ||||||||||||
| 27 | |||||||||||||
| 28 | =cut | ||||||||||||
| 29 | |||||||||||||
| 30 | package Dpkg::Interface::Storable 1.01; | ||||||||||||
| 31 | |||||||||||||
| 32 | 20 | ▶ | use v5.36; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | |||||||||||||
| 34 | 20 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | |||||||||||||
| 36 | 20 | ▶ | use Dpkg::Gettext; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 37 | 20 | ▶ | use Dpkg::ErrorHandling; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | |||||||||||||
| 39 | use overload | ||||||||||||
| 40 | 20 | ▶ | '""' => \&_stringify, | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | 20 | 'fallback' => 1; | |||||||||||
| 42 | |||||||||||||
| 43 | =head1 BASE METHODS | ||||||||||||
| 44 | |||||||||||||
| 45 | Those methods must be provided by the class that wish to inherit | ||||||||||||
| 46 | from Dpkg::Interface::Storable so that the methods provided can work. | ||||||||||||
| 47 | |||||||||||||
| 48 | =over 4 | ||||||||||||
| 49 | |||||||||||||
| 50 | =item $obj->parse($fh[, $desc]) | ||||||||||||
| 51 | |||||||||||||
| 52 | This methods initialize the object with the data stored in the | ||||||||||||
| 53 | filehandle. $desc is optional and is a textual description of | ||||||||||||
| 54 | the filehandle used in error messages. | ||||||||||||
| 55 | |||||||||||||
| 56 | =item $string = $obj->output([$fh]) | ||||||||||||
| 57 | |||||||||||||
| 58 | This method returns a string representation of the object in $string | ||||||||||||
| 59 | and it writes the same string to $fh (if it's defined). | ||||||||||||
| 60 | |||||||||||||
| 61 | =back | ||||||||||||
| 62 | |||||||||||||
| 63 | =head1 PROVIDED METHODS | ||||||||||||
| 64 | |||||||||||||
| 65 | =over 4 | ||||||||||||
| 66 | |||||||||||||
| 67 | =item $obj->load($filename, %opts) | ||||||||||||
| 68 | |||||||||||||
| 69 | Initialize the object with the data stored in the file. The file can be | ||||||||||||
| 70 | compressed, it will be decompressed on the fly by using a | ||||||||||||
| 71 | L<Dpkg::Compression::FileHandle> object. If $opts{compression} is false the | ||||||||||||
| 72 | decompression support will be disabled. If $filename is "-", then the | ||||||||||||
| 73 | standard input is read (no compression is allowed in that case). | ||||||||||||
| 74 | |||||||||||||
| 75 | =cut | ||||||||||||
| 76 | |||||||||||||
| 77 | 124 | ▶ | sub load { | ||||||||||
|
Subroutine
load: called CC=8 SCAR=22.2
Pod
documented
| |||||||||||||
| 78 | 124 | my ($self, $file, %opts) = @_; | |||||||||||
| 79 | 124 | ▶ | $opts{compression} //= 1; | ||||||||||
|
Condition100%2/2
$opts{"compression"} //= 1
MC/DC100%1/1
$opts{"compression"} //= 1
$opts{"compression"}
Truth table100%2/2
A
A= $opts{"compression"}
| |||||||||||||
| 80 | 124 | ▶ | unless ($self->can('parse')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 0 | croak ref($self) . ' cannot be loaded, it lacks the parse method'; | |||||||||||
| 82 | } | ||||||||||||
| 83 | 124 | my ($desc, $fh) = ($file, undef); | |||||||||||
| 84 | 124 | ▶ | if ($file eq '-') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | $fh = \*STDIN; | |||||||||||
| 86 | 0 | $desc = g_('<standard input>'); | |||||||||||
| 87 | } else { | ||||||||||||
| 88 | 124 | ▶ | if ($opts{compression}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 108 | require Dpkg::Compression::FileHandle; | |||||||||||
| 90 | 108 | $fh = Dpkg::Compression::FileHandle->new(); | |||||||||||
| 91 | } | ||||||||||||
| 92 | 124 | ▶ | open($fh, '<', $file) or syserr(g_('cannot read %s'), $file); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | } | ||||||||||||
| 94 | 124 | my $res = $self->parse($fh, $desc, %opts); | |||||||||||
| 95 | 117 | ▶ | if ($file ne '-') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 117 | ▶ | close($fh) or syserr(g_('cannot close %s'), $file); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | } | ||||||||||||
| 98 | 117 | return $res; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | =item $obj->save($filename, %opts) | ||||||||||||
| 102 | |||||||||||||
| 103 | Store the object in the file. If the filename ends with a known | ||||||||||||
| 104 | compression extension, it will be compressed on the fly by using a | ||||||||||||
| 105 | L<Dpkg::Compression::FileHandle> object. If $opts{compression} is false the | ||||||||||||
| 106 | compression support will be disabled. If $filename is "-", then the | ||||||||||||
| 107 | standard output is used (data are written uncompressed in that case). | ||||||||||||
| 108 | |||||||||||||
| 109 | =cut | ||||||||||||
| 110 | |||||||||||||
| 111 | 6 | ▶ | sub save { | ||||||||||
|
Subroutine
save: called CC=8 SCAR=23.2
Pod
documented
| |||||||||||||
| 112 | 6 | my ($self, $file, %opts) = @_; | |||||||||||
| 113 | 6 | ▶ | $opts{compression} //= 1; | ||||||||||
|
Condition50%1/2
$opts{"compression"} //= 1
MC/DC0%0/1
$opts{"compression"} //= 1
$opts{"compression"}
Truth table50%1/2
A
A= $opts{"compression"}
| |||||||||||||
| 114 | 6 | ▶ | unless ($self->can('output')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 0 | croak ref($self) . ' cannot be saved, it lacks the output method'; | |||||||||||
| 116 | } | ||||||||||||
| 117 | 6 | my $fh; | |||||||||||
| 118 | 6 | ▶ | if ($file eq '-') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | 0 | $fh = \*STDOUT; | |||||||||||
| 120 | } else { | ||||||||||||
| 121 | 6 | ▶ | if ($opts{compression}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 6 | require Dpkg::Compression::FileHandle; | |||||||||||
| 123 | 6 | $fh = Dpkg::Compression::FileHandle->new(); | |||||||||||
| 124 | } | ||||||||||||
| 125 | 6 | ▶ | open($fh, '>', $file) or syserr(g_('cannot write %s'), $file); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | } | ||||||||||||
| 127 | 6 | $self->output($fh, %opts); | |||||||||||
| 128 | 6 | ▶ | if ($file ne '-') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 6 | ▶ | close($fh) or syserr(g_('cannot close %s'), $file); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | } | ||||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | =item $string = "$obj" | ||||||||||||
| 134 | |||||||||||||
| 135 | Return a string representation of the object. | ||||||||||||
| 136 | |||||||||||||
| 137 | =cut | ||||||||||||
| 138 | |||||||||||||
| 139 | 57 | ▶ | sub _stringify { | ||||||||||
|
Subroutine
_stringify: called CC=2 SCAR=7.6
| |||||||||||||
| 140 | 57 | my $self = shift; | |||||||||||
| 141 | 57 | ▶ | unless ($self->can('output')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 0 | croak ref($self) . ' cannot be stringified, it lacks the output method'; | |||||||||||
| 143 | } | ||||||||||||
| 144 | 57 | return $self->output(); | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | =back | ||||||||||||
| 148 | |||||||||||||
| 149 | =head1 CHANGES | ||||||||||||
| 150 | |||||||||||||
| 151 | =head2 Version 1.01 (dpkg 1.19.0) | ||||||||||||
| 152 | |||||||||||||
| 153 | New options: The $obj->load() and $obj->save() methods support a new | ||||||||||||
| 154 | compression option. | ||||||||||||
| 155 | |||||||||||||
| 156 | =head2 Version 1.00 (dpkg 1.15.6) | ||||||||||||
| 157 | |||||||||||||
| 158 | Mark the module as public. | ||||||||||||
| 159 | |||||||||||||
| 160 | =cut | ||||||||||||
| 161 | |||||||||||||
| 162 | 20 | 1; | |||||||||||