Storable.pm 3.6 KB

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