Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clone an object

0 views
Skip to first unread message

Christoph Friedrich

unread,
Dec 28, 2009, 5:04:32 AM12/28/09
to Perl Beginners
Hello,

I have searched the internet but didn't found a good answer.
How do I clone a Perl Object?

For example like this:

$a = My::Object->new();
$b = $a->clone();

Where $b is an exact copy of $a but in its own memory.

Greets
Christoph

Philip Potter

unread,
Dec 28, 2009, 5:35:31 AM12/28/09
to Christoph Friedrich, Perl Beginners
2009/12/28 Christoph Friedrich <chri...@christophfriedrich.de>:

You call the function that the object provides for this purpose, if any.

The ability to clone an object is not special in Perl; it is treated
just like any other method. Perl doesn't provide a clone method for
you automatically; if you want one in your own objects, you have to
write it yourself. If you want to clone an object from a library
provided by someone else, you have to read its documentation and find
a clone object.

Not all functions provide a clone function; it may not be possible or
desirable to clone a given object. Consider for example an object
which owns a network socket: if you were able to clone the object, you
could create two different objects which own the same socket; this is
a Bad Thing.

Philip

Uri Guttman

unread,
Dec 28, 2009, 5:29:32 AM12/28/09
to Christoph Friedrich, Perl Beginners
>>>>> "CF" == Christoph Friedrich <chri...@christophfriedrich.de> writes:

CF> I have searched the internet but didn't found a good answer.
CF> How do I clone a Perl Object?

CF> For example like this:

CF> $a = My::Object->new();
CF> $b = $a->clone();

the answer is you can't in the general case. only the class should be
able to clone an object as it knows the implementation. you can't just
do a deep copy if it is implemented with inside out objects or other
stuff that won't allow external code to inspect it.

the only solutions are use a class that supports its own cloning method
or get all the data out of the object with accessor calls and construct
a new object from that.

and i bet you really don't need this but you just think you do. explain
what your bigger problem is and with what particular classes. cloning
isn't a general need in most designs.

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Philip Potter

unread,
Dec 28, 2009, 5:36:51 AM12/28/09
to Christoph Friedrich, Perl Beginners
2009/12/28 Philip Potter <philip....@gmail.com>:

> If you want to clone an object from a library
> provided by someone else, you have to read its documentation and find
> a clone object.

This should be a clone method, of course.

Shlomi Fish

unread,
Dec 28, 2009, 5:53:50 AM12/28/09
to begi...@perl.org, Christoph Friedrich
Hi Christoph!

OK, the first answer is that Storable's dclone *probably* does what you want
as it creates a deep copy of everything and it's implemented in C so it's
fast.

See:

http://perldoc.perl.org/Storable.html

Storable has been core Perl for a while.

The second and more complex answer is that cloning is not always: 1. Uniquely
defined. 2. Straightforward that you can rely on dclone. For example: consider
these examples:

<<<<<<<<<<
my $jack = Person->new({name => "Jack"});

$jack->set_address(Address->new("4 Abby Road"));

my $sophie = Person->new({name => "Sophie"});

$sophie->set_address(Address->new("4 Abby Road"));
>>>>>>>>>

And:

<<<<<<<<<
my $jack = Person->new({name => "Jack"});

my $address = Address->new("4 Abby Road");

$jack->set_address($address);

my $sophie = Person->new({name => "Sophie"});

$sophie->set_address($address);
>>>>>>>>>>

If we clone the "$jack" and "$sophie" persons should we also clone the address
which it points to? This may do the right thing in the first example, but
probably not in the second one.

Moreover what if the object contains handles to system services such as files,
sockets or GUI elements? How should they be cloned.

So my suggestion is that you define a ->clone() method (or a few) based on the
semantics of what you need and how it should behave, while probably making a
use of dclone() as a starting point.

Regards and Happy New Year,

Shlomi Fish

> Greets
> Christoph
>

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman

Bzr is slower than Subversion in combination with Sourceforge.
( By: http://dazjorz.com/ )

Christoph Friedrich

unread,
Dec 28, 2009, 6:11:17 AM12/28/09
to Uri Guttman, Perl Beginners
My Big Problem is that I must copy an object to do a backtracking method
(I am going to develop a sudoku solver).

Uri Guttman schrieb:

Shlomi Fish

unread,
Dec 28, 2009, 6:34:23 AM12/28/09
to begi...@perl.org, Christoph Friedrich, Uri Guttman
Hi Christoph!

On Monday 28 Dec 2009 13:11:17 Christoph Friedrich wrote:
> My Big Problem is that I must copy an object to do a backtracking method
> (I am going to develop a sudoku solver).
>

Well, you can always deep copy the object state, if you are sure it is self-
contained. (Using Storable's dclone() or whatever.).

Moreover, I should note that there are plenty of Sudoku solvers around,
including on CPAN:

http://search.cpan.org/search?query=sudoku&mode=all

And elsewhere:

http://www.google.com/search?q=sudoku%20solver

http://www.google.com/search?hl=en&q=sudoku+solver+open+source&aq=f&oq=&aqi=g1

Please consider re-using one of them.

Regards,

Shlomi Fish

> Uri Guttman schrieb:
> >>>>>> "CF" == Christoph Friedrich <chri...@christophfriedrich.de>
> >>>>>> writes:
> >
> > CF> I have searched the internet but didn't found a good answer.
> > CF> How do I clone a Perl Object?
> >
> > CF> For example like this:
> >
> > CF> $a = My::Object->new();
> > CF> $b = $a->clone();
> >
> > the answer is you can't in the general case. only the class should be
> > able to clone an object as it knows the implementation. you can't just
> > do a deep copy if it is implemented with inside out objects or other
> > stuff that won't allow external code to inspect it.
> >
> > the only solutions are use a class that supports its own cloning method
> > or get all the data out of the object with accessor calls and construct
> > a new object from that.
> >
> > and i bet you really don't need this but you just think you do. explain
> > what your bigger problem is and with what particular classes. cloning
> > isn't a general need in most designs.
> >
> > uri
>

--

-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/

Funny Anti-Terrorism Story - http://shlom.in/enemy

Jeff Peng

unread,
Dec 28, 2009, 6:54:26 AM12/28/09
to begi...@perl.org
Uri Guttman:

>
> and i bet you really don't need this but you just think you do.

why not?
I did have used object clone, like a ruby one:

> class Myclass
> end
=> nil

> x=Myclass.new
=> #<Myclass:0xb7c4bca8>

> y=x.clone
=> #<Myclass:0xb7c47194>

> x.object_id
=> -605921708

> y.object_id
=> -605931318


The object was cloned, they both got different object IDs.

Erez Schatz

unread,
Dec 28, 2009, 7:00:48 AM12/28/09
to begi...@perl.org
On 28/12/2009, Shlomi Fish <shl...@iglu.org.il> wrote:
> Hi Christoph!
>
> On Monday 28 Dec 2009 13:11:17 Christoph Friedrich wrote:
>> My Big Problem is that I must copy an object to do a backtracking method
>> (I am going to develop a sudoku solver).
>>

> Moreover, I should note that there are plenty of Sudoku solvers around,

Please, because re-implementing a solution makes baby Larry cry.

Also, submitting a CPAN solution as your own is considered OK in Perl
courses, so, Bonus!

--
Erez

"The government forgets that George Orwell's 1984 was a warning, and
not a blueprint"
http://www.nonviolent-conflict.org/ -- http://www.whyweprotest.org/

Christoph Friedrich

unread,
Dec 28, 2009, 7:53:04 AM12/28/09
to Shlomi Fish, begi...@perl.org, Uri Guttman
I know that there are many Sudoku solver over the internet.
But than I has no chance of learning perl and learn some of the
algorhytms ^^

Shlomi Fish schrieb:

Shlomi Fish

unread,
Dec 28, 2009, 8:43:48 AM12/28/09
to begi...@perl.org, Christoph Friedrich
Hi Christoph.

On Monday 28 Dec 2009 14:53:04 Christoph Friedrich wrote:
> I know that there are many Sudoku solver over the internet.
> But than I has no chance of learning perl and learn some of the
> algorhytms ^^
>

So it's an exercise for learning. I see. If you would like to work on
something a bit more ground-breaking, then many Perl open-source projects
(including many CPAN Perl packages) would love some help. I also have some
Perl code that you can work on:

* http://www.shlomifish.org/open-source/

* http://search.cpan.org/~shlomif/

I offered my help in tutoring people who wish to contribute to open source in
Perl (or any other programming language I deal with) and so far I did not get
any final acceptance.

Feel free to contact me about it.

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/

Original Riddles - http://www.shlomifish.org/puzzles/

Randal L. Schwartz

unread,
Dec 28, 2009, 11:11:59 AM12/28/09
to begi...@perl.org
>>>>> "Christoph" == Christoph Friedrich <chri...@christophfriedrich.de> writes:

Christoph> My Big Problem is that I must copy an object to do a backtracking
Christoph> method (I am going to develop a sudoku solver).

Then your object should know how to clone itself.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

Uri Guttman

unread,
Dec 28, 2009, 12:41:03 PM12/28/09
to Jeff Peng, begi...@perl.org
>>>>> "JP" == Jeff Peng <jeff...@netzero.net> writes:

JP> Uri Guttman:


>>
>> and i bet you really don't need this but you just think you do.

JP> why not?
JP> I did have used object clone, like a ruby one:

huh??

>> class Myclass
>> end
JP> => nil

>> x=Myclass.new
JP> => #<Myclass:0xb7c4bca8>

>> y=x.clone
JP> => #<Myclass:0xb7c47194>

>> x.object_id
JP> => -605921708

>> y.object_id
JP> => -605931318


JP> The object was cloned, they both got different object IDs.

that is ruby, not perl. not all perl classes offer clone methods and
there is no possible generic way to clone a given perl object since it
can be implemented in so many different and opaque ways.

Shlomi Fish

unread,
Dec 28, 2009, 1:02:00 PM12/28/09
to begi...@perl.org
Hi Christoph!

This message was sent in private and it contains corrections to your English.

On Monday 28 Dec 2009 14:53:04 Christoph Friedrich wrote:

> I know that there are many Sudoku solver over the internet.

s/solver/solvers/ . s/over the/on the/

> But than I has no chance of learning perl and learn some of the
> algorhytms ^^

s/than/then/ - see http://en.wiktionary.org/wiki/then ;
http://en.wiktionary.org/wiki/than.

s/I has/I have/

s/learn some of the algorhytms/cannot learn some of its algorithms/.

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/

Interview with Ben Collins-Sussman - http://shlom.in/sussman

Bzr is slower than Subversion in combination with Sourceforge.

Christoph Friedrich

unread,
Dec 28, 2009, 5:26:19 PM12/28/09
to Shlomi Fish, Perl Beginners
Thank you ^^

Shlomi Fish schrieb:

Erez Schatz

unread,
Dec 29, 2009, 12:54:32 AM12/29/09
to begi...@perl.org
Shlomi, please stop correcting the English of those who post here.
It's rude, off-topic, and unimportant. This isn't a language mailing
list and you are not its chief linguistic officer. I understand you
take pride in your English, since you are not a native speaker, but
that has nothing to do with this mailing list.

2009/12/28 Shlomi Fish <shl...@iglu.org.il>:


> Hi Christoph!
>
> This message was sent in private and it contains corrections to your English.

No it wasn't. And even if it was, harassing people who come here with
your corrections isn't your role in life, and isn't what this mailing
list was created for.

C.DeRykus

unread,
Dec 29, 2009, 8:15:32 AM12/29/09
to begi...@perl.org

<off-topic> But, Ruby's clone method does a shallow copy so
instance variables can get clobbered -- likely
not
the behavior you want. From RDoc:

obj.clone → an_object

Produces a shallow copy of obj—the instance
variables of obj
are copied, but not the objects they
reference. ...
</off-topic>

--
Charles DeRykus

Jenda Krynicky

unread,
Dec 31, 2009, 11:03:33 PM12/31/09
to begi...@perl.org
From: Erez Schatz <moon...@gmail.com>

> Shlomi, please stop correcting the English of those who post here.
> It's rude,

nope

> off-topic,

maybe

> and unimportant.

Not at all. Being able to express your needs/questions clearly is
quite important.

Jenda
===== Je...@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Jenda Krynicky

unread,
Dec 31, 2009, 11:03:33 PM12/31/09
to Perl Beginners
- You might, but what about others? You do want others to read your
messages and help you with your questions, don't you?
- But I remember what I wrote last time so I don't have to read it.
- 'cause it's all backwards!
- Why is that?
- Because it's hard to read.
- Why?
- Please do not top post.

From: Christoph Friedrich <chri...@christophfriedrich.de>
> My Big Problem is that I must copy an object to do a backtracking method
> (I am going to develop a sudoku solver).

Not everything has to be an object.

In either case the object must know how to clone itself. And how does
it do that depends on its internals, on the way it stored its data.
So do you need to copy a hash of scalars? An array? ...

0 new messages