Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Easy cloning leaving out selected attributes

by mzedeler (Pilgrim)
on Apr 16, 2012 at 14:19 UTC ( #965324=perlquestion: print w/replies, xml ) Need Help??
mzedeler has asked for the wisdom of the Perl Monks concerning the following question:

Is there any easy way to make MooseX::Clone leave out selected attributes (reverting to their defaults) when calling clone()?
  • Comment on Easy cloning leaving out selected attributes

Replies are listed 'Best First'.
Re: Easy cloning leaving out selected attributes
by zwon (Abbot) on Apr 17, 2012 at 13:55 UTC

    Documentation for MooseX::Clone is pretty clear about that, add 'NoClone' trait to attributes you don't want to clone:

    use 5.010; use strict; use warnings; { package Foo; use Moose; with 'MooseX::Clone'; has bar => ( is => 'rw', default => 42, ); has baz => ( is => 'rw', default => 42, traits => [qw(NoClone)], ) +; } my $foo = Foo->new( bar => 33, baz => 33 ); my $clone = $foo->clone; say "Bar: ", $clone->bar; say "Baz: ", $clone->baz; __END__ Bar: 33 Baz: 42
      Thanks, zwon.
      Part of my brain must have been asleep when I read the manual.
Re: Easy cloning leaving out selected attributes
by jeffa (Bishop) on Apr 17, 2012 at 00:03 UTC

    But isn't that the point of cloning? If you want an object to have default attributes then just make a new one.


    (the triplet paradiddle with high-hat)

      The point is that some (few) attributes should never be cloned, while others should. In this case, I have some objects that are stored through KioukuDB, and it never makes sense that they get the same id as the original object.

      I have a role that provides default ids, so it would be nice to have the clone method default to that.

      Another thing I need to differentiate is between some attributes that should be shallow clones and others that should be deep clones (using the clone() method on them, of course).

Re: Easy cloning leaving out selected attributes
by Anonymous Monk on Apr 17, 2012 at 13:28 UTC
    Maybe you should just define your own role, using MooseX::Clone as a model to work from.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965324]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-02-22 11:34 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (291 votes). Check out past polls.