Storable.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. package Dpkg::Interface::Storable;
  16. use strict;
  17. use warnings;
  18. our $VERSION = "1.00";
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Compression::FileHandle;
  22. use overload
  23. '""' => \&_stringify,
  24. 'fallback' => 1;
  25. =head1 NAME
  26. Dpkg::Interface::Storable - common methods related to object serialization
  27. =head1 DESCRIPTION
  28. Dpkg::Interface::Storable is only meant to be used as parent
  29. class for other objects. It provides common methods that are
  30. all implemented on top of two basic methods parse() and output().
  31. =head1 BASE METHODS
  32. Those methods must be provided by the object that wish to inherit
  33. from Dpkg::Interface::Storable so that the methods provided can work.
  34. =over 4
  35. =item $obj->parse($fh, $desc)
  36. This methods initialize the object with the data stored in the
  37. filehandle. $desc is optional and is a textual description of
  38. the filehandle used in error messages.
  39. =item $string = $obj->output($fh)
  40. This method returns a string representation of the object in $string
  41. and it writes the same string to $fh (if it's defined).
  42. =back
  43. =head1 PROVIDED METHODS
  44. =over 4
  45. =item $obj->load($filename)
  46. Initialize the object with the data stored in the file. The file can be
  47. compressed, it will be uncompressed on the fly by using a
  48. Dpkg::Compression::FileHandle object. If $filename is "-", then the
  49. standard input is read (no compression is allowed in that case).
  50. =cut
  51. sub load {
  52. my ($self, $file, @options) = @_;
  53. unless ($self->can("parse")) {
  54. internerr("%s cannot be loaded, it lacks the parse method", ref($self));
  55. }
  56. my ($desc, $fh) = ($file, undef);
  57. if ($file eq "-") {
  58. $fh = \*STDIN;
  59. $desc = _g("<standard input>");
  60. } else {
  61. $fh = Dpkg::Compression::FileHandle->new();
  62. open($fh, "<", $file) || syserr(_g("cannot read %s"), $file);
  63. }
  64. my $res = $self->parse($fh, $desc, @options);
  65. if ($file ne "-") {
  66. close($fh) || syserr(_g("cannot close %s"), $file);
  67. }
  68. return $res;
  69. }
  70. =item $obj->save($filename)
  71. Store the object in the file. If the filename ends with a known
  72. compression extension, it will be compressed on the fly by using a
  73. Dpkg::Compression::FileHandle object. If $filename is "-", then the
  74. standard output is used (data are written uncompressed in that case).
  75. =cut
  76. sub save {
  77. my ($self, $file, @options) = @_;
  78. unless ($self->can("output")) {
  79. internerr("%s cannot be saved, it lacks the output method", ref($self));
  80. }
  81. my $fh;
  82. if ($file eq "-") {
  83. $fh = \*STDOUT;
  84. } else {
  85. $fh = Dpkg::Compression::FileHandle->new();
  86. open($fh, ">", $file) || syserr(_g("cannot write %s"), $file);
  87. }
  88. $self->output($fh, @options);
  89. if ($file ne "-") {
  90. close($fh) || syserr(_g("cannot close %s"), $file);
  91. }
  92. }
  93. =item "$obj"
  94. Return a string representation of the object.
  95. =cut
  96. sub _stringify {
  97. my ($self) = @_;
  98. unless ($self->can("output")) {
  99. internerr("%s cannot be stringified, it lacks the output method", ref($self));
  100. }
  101. return $self->output();
  102. }
  103. =back
  104. =head1 AUTHOR
  105. Raphaël Hertzog <hertzog@debian.org>.
  106. =cut
  107. 1;