Subject: | STORABLE_thaw hook breaks encapsulation of OO Modules (e.g. Bit::Vector) |
By proving a generic constructor for blessed objects, the STORABLE_thaw
hook breaks the encapsulation of OO modules, some of which use a
proprietary constructor for a good reason.
For instance Bit::Vector creates objects outside of the Perl-managed
memory space, and therefore needs a special constructor of its own.
The "fake" Bit::Vector object created by STORABLE_thaw is then very hard
to turn into a "real" Bit::Vector object (requires an ugly workaround).
If - as suggested in http://rt.cpan.org/Public/Bug/Display.html?id=6641
- Storable would return whatever STORABLE_thaw returns, and only would
return the generically created blessed object if STORABLE_thaw returned
nothing, this would probably not break existing code (or at least
necessitate only a short one-liner to correct, in each module affected),
and would solve the problem for modules with a non-standard constructor.
Subject: | Vector.pm |
###############################################################################
## ##
## Copyright (c) 1995 - 2009 by Steffen Beyer. ##
## All rights reserved. ##
## ##
## This package is free software; you can redistribute it ##
## and/or modify it under the same terms as Perl itself. ##
## ##
###############################################################################
package Bit::Vector;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION @CONFIG);
require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
@EXPORT = qw();
@EXPORT_OK = qw();
$VERSION = '6.6';
bootstrap Bit::Vector $VERSION;
sub STORABLE_freeze
{
my($self, $cloning) = @_;
return( Storable::freeze( [ $self->Size(), $self->Block_Read() ] ) );
}
sub STORABLE_thaw
{
my($self, $cloning, $string) = @_;
my($size,$buffer) = @{ Storable::thaw($string) };
$self->Unfake($size); # Undocumented new feature (slightly dangerous!) only for @%$&*# Storable! (Grrr)
$self->Block_Store($buffer);
}
# Why can't Storable just use a module's constructor and provides one of its own instead?!?!
# This breaks the encapsulation of other modules which have their own constructor for a good reason!
1;
__END__