Code Monkey home page Code Monkey logo

p5-type-tie's Introduction

Type-Tie

Perl 5 distribution Type-Tiny; see homepage for download, issue tracker, and more info.

Issues

Please report any issues via RT.

p5-type-tie's People

Contributors

pmorch avatar tobyink avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

p5-type-tie's Issues

Doesn't work with Clone::clone

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

Peter Valdemar Mørch
http://www.morch.com

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.