Perl 5 distribution Type-Tiny; see homepage for download, issue tracker, and more info.
Please report any issues via RT.
Perl 5 distribution Type-Tie; see homepage for download, issue tracker, and more info.
Home Page: https://metacpan.org/release/Type-Tie
Migrated from rt.cpan.org #127576 (status was 'open')
Requestors:
From [email protected] on 2018-11-05 11:36:55
:
Clone::clone-ing a ttie-d variable causes an error when using it. I assume
because of the Inside-out object implementation.
Example:
#!/usr/bin/perl -w
use strict;
use Types::Standard qw(-types);
use Type::Tie;
use Clone qw(clone);
my %hash;
ttie %hash, Bool;
$hash{a} = 1;
my $clone = clone(%hash);
$clone->{a} = 1;
The last line dies with:
Can't use an undefined value as a subroutine reference at
/usr/share/perl5/Type/Tie.pm line 118.
Guesswork at analysis:
Type Type::Tie implementation makes use of Hash::Util::FieldHash for
inside-out-classes. And then it makes sense for clone not to work. And
indeed line 118 includes
$check->($val)
where $check is defined a few lines above as:
my $check = $CHECK{$self};
and $CHECK is a fieldhash.
So $self is cloned to a new object/ref, but new entries aren't put in
%TYPE, %COERCE and %CHECK.
(It seems the Tie::* classes work fine with cloning and perldoc Clone shows
that it should also work for tied variables. So I don't think it is clone
per se.)
It works if I just call
ttie %$clone, Bool;
Before operating on $clone. But...
In reality, I'm using it with Moo like so:
{
package MyTied;
use Moo;
use Types::Standard qw(-types);
use Type::Tie;
has field => (
is => 'ro',
default => sub {
return ttie my %hash, Bool;
}
);
}
my $obj = MyTied->new();
$obj->field->{foo} = 1;
my $objclone = clone($obj);
$objclone->field->{foo} = 1;
And so it would sorta break encapsulation if users of MyTied need to know
to do something special for MyTied objects.
Any suggestions on how to proceed, other than not clone-ing? Not being able
to clone otherwise pure Moo objects is nasty for us.
One workaround is to use Storable::dclone instead of Clone::clone and setup
the hooks correctly, but it seems like a lot of work, and still breaks for
Clone::clone (and I don't necessarily control what my users are going to
use).
#!/usr/bin/perl -w
use strict;
use Types::Standard qw(-types);
use Type::Tie;
use Storable qw(dclone thaw freeze);
{
package MyTied2;
use Moo;
use Types::Standard qw(-types);
use Type::Tie;
use Storable;
has field => (
is => 'ro',
default => sub {
return ttie my %hash, Bool;
}
);
sub STORABLE_freeze {
my $self = shift;
return Storable::freeze({ %$self });
}
sub STORABLE_thaw {
my ($self, $cloning, $serialized) = @_;
my $selfRawHash = Storable::thaw($serialized);
%$self = %$selfRawHash;
ttie %{ $self->field }, Bool;
}
}
my $obj = MyTied2->new();
$obj->field->{foo} = 1;
my $objclone = dclone($obj);
$objclone->field->{foo} = 1;
I suggest that unless something can be done (easily) to let clone work,
then at least document this issue, so we know beforehand.
What do you think?
Peter Valdemar Mørch
http://www.morch.com
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.