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
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
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 ---------
This should be a clone method, of course.
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/ )
Uri Guttman schrieb:
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
>
> 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.
> 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.
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/
Shlomi Fish schrieb:
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/
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
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.
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.
Shlomi Fish schrieb:
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.
<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
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
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? ...