Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Moose 'clone' Constructor

by choroba (Cardinal)
on Jul 04, 2018 at 12:49 UTC ( [id://1217883]=note: print w/replies, xml ) Need Help??


in reply to Moose 'clone' Constructor

Note that there can't be a general cloning routine free to use in all the cases. Imagine each object has a temp file created somewhere - when cloning an object, do you want it to share the same file(handle)? What if each object registers itself to a static hash or a parent object, generates a unique ID, seeds a PRNG, etc?
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; { package MyObj; use Moose; has fh => (is => 'ro', builder => '_build_fh'); sub _build_fh { my ($self) = @_; open my $fh, '>', "/tmp/$self" or die $!; return $fh } sub DEMOLISH { my ($self) = @_; close $self->fh; unlink "/tmp/$self"; } } my $o = 'MyObj'->new; { my $c = $o->meta->clone_object($o); } say { $o->fh } "Hello world!"; # say() on closed filehandle $fh

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Moose 'clone' Constructor
by kcott (Archbishop) on Jul 05, 2018 at 03:27 UTC

    G'day choroba,

    ++ That's a very valid point; thanks for raising it. It even applies to the code I'm currently working with, which I paraphrased for this post as:

    my $cloned_object = ref($existing_object)->new();

    As I indicated in the OP, I want an object that's of the same class as $existing_object, but I want default values (not whatever values $existing_object has). In fact, if I had taken $existing_object's values, that would've broken my code in all sorts of interesting ways.

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1217883]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found