Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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

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.


    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.

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?
[Your Mother]: Stick with what you know. Or post a SOPW and if it's not complicated I guarantee someone will answer; me if it's straightforward and no one else gets it better first.
[1nickt]: ++YourMother (Grammar Vigilante of Bristol)
[Lady_Aleena]: Um, how does grep do multiline? print "$file:$_" if grep { /get_(array|hash| data)\(.+\)/ } $_; only returns the same 18 lines the command line grep does.
[Lady_Aleena]: my scratchpad has what I have so far.
[choroba]: what do you mean by multiline?
[Discipulus]: thanks 1nickt!
[choroba]: grep is a filter, it selects elements from a list based on a boolean condition
[moritz]: you need to read the file in one go to do a multiline regex match
[erix]: ha ha, I like the Vigilante
[Lady_Aleena]: Forget it. I'm getting angry with it.

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2017-05-23 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?