Storable.pm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.
  48. =cut
  49. sub load {
  50. my ($self, $file, @options) = @_;
  51. unless ($self->can("parse")) {
  52. internerr("%s cannot be loaded, it lacks the parse method", ref($self));
  53. }
  54. my $cf = Dpkg::Compression::FileHandle->new();
  55. open($cf, "<", $file) || syserr(_g("cannot read %s"), $file);
  56. my $res = $self->parse($cf, $file, @options);
  57. close($cf) || syserr(_g("cannot close %s"), $file);
  58. return $res;
  59. }
  60. =item $obj->save($filename)
  61. Store the object in the file. If the filename ends with a known
  62. compression extension, it will be compressed on the fly by using a
  63. Dpkg::Compression::FileHandle object.
  64. =cut
  65. sub save {
  66. my ($self, $file, @options) = @_;
  67. unless ($self->can("output")) {
  68. internerr("%s cannot be saved, it lacks the output method", ref($self));
  69. }
  70. my $cf = Dpkg::Compression::FileHandle->new();
  71. open($cf, ">", $file) || syserr(_g("cannot write %s"), $file);
  72. $self->output($cf, @options);
  73. close($cf) || syserr(_g("cannot close %s"), $file);
  74. }
  75. =item "$obj"
  76. Return a string representation of the object.
  77. =cut
  78. sub _stringify {
  79. my ($self) = @_;
  80. unless ($self->can("output")) {
  81. internerr("%s cannot be stringified, it lacks the output method", ref($self));
  82. }
  83. return $self->output();
  84. }
  85. =back
  86. =head1 AUTHOR
  87. Raphaël Hertzog <hertzog@debian.org>.
  88. =cut
  89. 1;