Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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
Re: Easy cloning leaving out selected attributes
by jeffa (Chancellor) 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.


    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (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.
Re: Easy cloning leaving out selected attributes
by zwon (Monsignor) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965324]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-08-01 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls