Build.PL100664001751001751 357614530305065 15566 0ustar00ishigakiishigaki000000000000Data-Clone-0.006# ========================================================================= # THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. # DO NOT EDIT DIRECTLY. # ========================================================================= use 5.008_001; use strict; use warnings; use utf8; use Module::Build::XSUtil; use File::Basename; use File::Spec; my %args = ( license => 'perl_5', dynamic_config => 0, configure_requires => { 'Module::Build' => '0.4005', }, requires => { 'XSLoader' => '0.02', 'parent' => '0', 'perl' => '5.008001', }, recommends => { }, suggests => { }, build_requires => { 'Devel::PPPort' => '3.19', 'ExtUtils::MakeMaker' => '6.59', 'ExtUtils::ParseXS' => '3.18', 'Module::Build' => '0', 'Module::Build::XSUtil' => '0', 'Test::More' => '0.88', 'Test::Requires' => '0.03', }, test_requires => { }, name => 'Data-Clone', module_name => 'Data::Clone', allow_pureperl => 0, script_files => [glob('script/*'), glob('bin/*')], PL_files => {}, test_files => ((-d '.git' || $ENV{RELEASE_TESTING}) && -d 'xt') ? 't/ xt/' : 't/', recursive_test_files => 1, needs_compiler_c99 => 0, needs_compiler_cpp => 0, generate_ppport_h => 'lib/Data/ppport.h', generate_xshelper_h => 'lib/Data/xshelper.h', cc_warnings => 1, ); if (-d 'share') { $args{share_dir} = 'share'; } my $builder = Module::Build::XSUtil->subclass( class => 'MyBuilder', code => q{ sub ACTION_distmeta { die "Do not run distmeta. Install Minilla and `minil install` instead.\n"; } sub ACTION_installdeps { die "Do not run installdeps. Run `cpanm --installdeps .` instead.\n"; } } )->new(%args); $builder->create_build_script(); Changes100664001751001751 102214530305065 15545 0ustar00ishigakiishigaki000000000000Data-Clone-0.006Revision history for Perl extension Data::Clone 0.006 2023-11-25T05:46:27Z - No code changes - Migrate from Module::Install to Minilla 0.005 2023-11-21 - Fix against Perl 5.38 (#3, by demerphq) 0.004 2014-05-07 06:58:25+0900 - Fix against Perl 5.19 (#1, by tomhukins) 0.003 2011-01-15 23:02:18 - Cloning normal object died as of 0.002, but it seems too strict 0.002 2011-01-15 22:41:13 - Fix a problem on pre-5.10 perls 0.001 Mon Jan 11 10:15:23 2010 - original version; created by Module::Setup LICENSE100664001751001751 4375714530305065 15324 0ustar00ishigakiishigaki000000000000Data-Clone-0.006This software is copyright (c) 2023 by Goro Fuji (gfx) . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2023 by Goro Fuji (gfx) . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2023 by Goro Fuji (gfx) . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End META.json100664001751001751 437514530305065 15711 0ustar00ishigakiishigaki000000000000Data-Clone-0.006{ "abstract" : "Polymorphic data cloning", "author" : [ "Goro Fuji (gfx) " ], "dynamic_config" : 0, "generated_by" : "Minilla/v3.1.23", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Data-Clone", "no_index" : { "directory" : [ "t", "xt", "inc", "share", "eg", "examples", "author", "builder" ] }, "prereqs" : { "build" : { "requires" : { "Devel::PPPort" : "3.19", "ExtUtils::MakeMaker" : "6.59", "ExtUtils::ParseXS" : "3.18", "Module::Build" : "0", "Module::Build::XSUtil" : "0", "Test::More" : "0.88", "Test::Requires" : "0.03" } }, "configure" : { "requires" : { "Module::Build" : "0.4005", "Module::Build::XSUtil" : "0.03" } }, "develop" : { "requires" : { "Test::CPAN::Meta" : "0", "Test::MinimumVersion::Fast" : "0.04", "Test::PAUSE::Permissions" : "0.07", "Test::Pod" : "1.41", "Test::Spellunker" : "v0.2.7" } }, "runtime" : { "requires" : { "XSLoader" : "0.02", "parent" : "0", "perl" : "5.008001" } } }, "provides" : { "Data::Clone" : { "file" : "lib/Data/Clone.pm", "version" : "0.006" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/gfx/p5-Data-Clone/issues" }, "homepage" : "https://github.com/gfx/p5-Data-Clone", "repository" : { "type" : "git", "url" : "https://github.com/gfx/p5-Data-Clone.git", "web" : "https://github.com/gfx/p5-Data-Clone" } }, "version" : "0.006", "x_contributors" : [ "Fuji, Goro (gfx) ", "Kenichi Ishigaki ", "Tom Hukins ", "Yves Orton " ], "x_serialization_backend" : "JSON::PP version 4.16", "x_static_install" : 0 } README.md100664001751001751 1157314530305065 15565 0ustar00ishigakiishigaki000000000000Data-Clone-0.006[![Actions Status](https://github.com/gfx/p5-Data-Clone/actions/workflows/test.yml/badge.svg)](https://github.com/gfx/p5-Data-Clone/actions) # NAME Data::Clone - Polymorphic data cloning # VERSION This document describes Data::Clone version 0.006. # SYNOPSIS # as a function use Data::Clone; my $data = YAML::Load("foo.yml"); # complex data structure my $cloned = clone($data); # makes Foo clonable package Foo; use Data::Clone; # ... # Foo is clonable my $o = Foo->new(); my $c = clone($o); # $o is deeply copied # used for custom clone methods package Bar; use Data::Clone qw(data_clone); sub clone { my($proto) = @_; my $object = data_clone($proto); $object->do_something(); return $object; } # ... # Bar is also clonable $o = Bar->new(); $c = clone($o); # Bar::clone() is called # DESCRIPTION `Data::Clone` does data cloning, i.e. copies things recursively. This is smart so that it works with not only non-blessed references, but also with blessed references (i.e. objects). When `clone()` finds an object, it calls a `clone` method of the object if the object has a `clone`, otherwise it makes a surface copy of the object. That is, this module does polymorphic data cloning. Although there are several modules on CPAN which can clone data, this module has a different cloning policy from almost all of them. See ["Cloning policy"](#cloning-policy) and ["Comparison to other cloning modules"](#comparison-to-other-cloning-modules) for details. ## Cloning policy A cloning policy is a rule that how a cloning routine copies data. Here is the cloning policy of `Data::Clone`. ### Non-reference values Non-reference values are copied normally, which will drop their magics. ### Scalar references Scalar references including references to other types of references are **not** copied deeply. They are copied on surface because it is typically used to refer to something unique, namely global variables or magical variables. ### Array references Array references are copied deeply. The cloning policy is applied to each value recursively. ### Hash references Hash references are copied deeply. The cloning policy is applied to each value recursively. ### Glob, IO and Code references These references are **not** copied deeply. They are copied on surface. ### Blessed references (objects) Blessed references are **not** copied deeply by default, because objects might have external resources which `Data::Clone` could not deal with. They will be copied deeply only if `Data::Clone` knows they are clonable, i.e. they have a `clone` method. If you want to make an object clonable, you can use the `clone()` function as a method: package Your::Class; use Data::Clone; # ... my $your_class = Your::Class->new(); my $c = clone($your_object); # $your_object->clone() will be called Or you can import `data_clone()` function to define your custom clone method: package Your::Class; use Data::Clone qw(data_clone); sub clone { my($proto) = @_; my $object = data_clone($proto); # anything what you want return $object; } Of course, you can use `Clone::clone()`, `Storable::dclone()`, and/or anything you want as an implementation of `clone` methods. ## Comparison to other cloning modules There are modules which does data cloning. `Storable` is a standard module which can clone data with `dclone()`. It has a different cloning policy from `Data::Clone`. By default it tries to make a deep copy of all the data including blessed references, but you can change its behaviour with specific hook methods. `Clone` is a well-known cloning module, but it does not polymorphic cloning. This makes a deep copy of data regardless of its types. Moreover, there is no way to change its behaviour, so this is useful only for data which link to no external resources. `Data::Clone` makes a deep copy of data only if it knows that the data are clonable. You can change its behaviour simply by defining `clone` methods. It also exceeds `Storable` and `Clone` in performance. # INTERFACE ## Exported functions ### **clone(Scalar)** Returns a copy of _Scalar_. ## Exportable functions ### **data\_clone(Scalar)** Returns a copy of _Scalar_. The same as `clone()`. Provided for custom clone methods. ### **is\_cloning()** Returns true inside the `clone()` function, false otherwise. # DEPENDENCIES Perl 5.8.1 or later, and a C compiler. # BUGS No bugs have been reported. Please report any bugs or feature requests to the author. # SEE ALSO [Storable](https://metacpan.org/pod/Storable) [Clone](https://metacpan.org/pod/Clone) # AUTHOR Goro Fuji (gfx) <gfuji(at)cpan.org> # LICENSE AND COPYRIGHT Copyright (c) 2010, Goro Fuji (gfx). All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. method.pl100644001751001751 76214530305065 20011 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/benchmark#!perl -w use strict; use Benchmark qw(:all); use Data::Clone; BEGIN{ package Object; sub new { my $class = shift; return bless { @_ }, $class; } package DC; use Data::Clone qw(clone); our @ISA = qw(Object); } my %args = ( foo => 42, inc => { %INC }, ); my $o = DC->new(%args); print "Method vs. Function:\n"; cmpthese -1 => { 'method' => sub{ my $x = $o->clone; }, 'function' => sub{ my $x = clone($o); }, }; object.pl100644001751001751 143214530305065 20012 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/benchmark#!perl -w use strict; use Benchmark qw(:all); use Storable (); use Clone (); use Data::Clone (); BEGIN{ package Object; sub new { my $class = shift; return bless { @_ }, $class; } package ST; use Storable (); our @ISA = qw(Object); *clone = \&Storable::dclone; package C; use Clone qw(clone); our @ISA = qw(Object); package DC; use Data::Clone qw(clone); our @ISA = qw(Object); } my %args = ( foo => 42, inc => { %INC }, ); my $st = ST->new(%args); my $c = C->new(%args); my $dc = DC->new(%args); print "Object:\n"; cmpthese -1 => { 'Clone' => sub{ my $x = $c->clone; }, 'Storable' => sub{ my $x = $st->clone; }, 'Data::Clone' => sub{ my $x = $dc->clone; }, }; vs_clone.pl100644001751001751 127114530305065 20355 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/benchmark#!perl -w use strict; use Benchmark qw(:all); use Storable (); use Clone (); use Data::Clone (); my @array = ( [1 .. 10], [1 .. 10] ); print "ArrayRef:\n"; cmpthese -1 => { 'Clone' => sub{ my $x = Clone::clone(\@array); }, 'Storable' => sub{ my $x = Storable::dclone(\@array); }, 'Data::Clone' => sub{ my $x = Data::Clone::clone(\@array); }, }; my %hash = ( key => \@array, ); print "HashRef:\n"; cmpthese -1 => { 'Clone' => sub{ my $x = Clone::clone(\%hash); }, 'Storable' => sub{ my $x = Storable::dclone(\%hash); }, 'Data::Clone' => sub{ my $x = Data::Clone::clone(\%hash); }, }; vs_fast.pl100644001751001751 130614530305065 20211 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/benchmark#!perl -w use strict; use Benchmark qw(:all); use Clone::Fast (); use Clone (); use Data::Clone (); my @array = ( [1 .. 10], [1 .. 10] ); print "ArrayRef:\n"; cmpthese -1 => { 'Clone' => sub{ my $x = Clone::clone(\@array); }, 'Clone::Fast' => sub{ my $x = Clone::Fast::clone(\@array); }, 'Data::Clone' => sub{ my $x = Data::Clone::clone(\@array); }, }; my %hash = ( key => \@array, ); print "HashRef:\n"; cmpthese -1 => { 'Clone' => sub{ my $x = Clone::clone(\%hash); }, 'Clone::Fast' => sub{ my $x = Clone::Fast::clone(\%hash); }, 'Data::Clone' => sub{ my $x = Data::Clone::clone(\%hash); }, }; cpanfile100664001751001751 55514530305065 15750 0ustar00ishigakiishigaki000000000000Data-Clone-0.006requires 'XSLoader', '0.02'; requires 'parent'; requires 'perl', '5.008001'; on build => sub { requires 'Module::Build'; requires 'Module::Build::XSUtil'; requires 'Devel::PPPort', '3.19'; requires 'ExtUtils::MakeMaker', '6.59'; requires 'ExtUtils::ParseXS', '3.18'; requires 'Test::More', '0.88'; requires 'Test::Requires', '0.03'; }; scalar_refs.pl100644001751001751 100514530305065 20525 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/example#!perl -w use strict; use Clone qw(clone); use Data::Clone qw(data_clone); use Storable qw(dclone); use Errno qw(ENOENT); use Devel::Peek; use Data::Dumper; our $errstr = 'foo'; my $data = { errstr_ref => \$errstr, errno_ref => \$! }; my $c1 = clone($data); my $c2 = dclone($data); my $c3 = data_clone($data); $errstr = 'bar'; $! = ENOENT; print Data::Dumper->Dump([$c1], ['Clone']); print Data::Dumper->Dump([$c2], ['Storable']); print Data::Dumper->Dump([$c3], ['DataClone']); #Dump($c1); #Dump($c2); Clone.pm100644001751001751 1210414530305065 17310 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/lib/Datapackage Data::Clone; use 5.008_001; use strict; our $VERSION = '0.006'; use XSLoader; XSLoader::load(__PACKAGE__, $VERSION); use parent qw(Exporter); our @EXPORT = qw(clone); our @EXPORT_OK = qw(data_clone TIECLONE); sub data_clone; *data_clone = \&clone; # alias sub TIECLONE; *TIECLONE = \&clone; # alias 1; __END__ =head1 NAME Data::Clone - Polymorphic data cloning =head1 VERSION This document describes Data::Clone version 0.006. =head1 SYNOPSIS # as a function use Data::Clone; my $data = YAML::Load("foo.yml"); # complex data structure my $cloned = clone($data); # makes Foo clonable package Foo; use Data::Clone; # ... # Foo is clonable my $o = Foo->new(); my $c = clone($o); # $o is deeply copied # used for custom clone methods package Bar; use Data::Clone qw(data_clone); sub clone { my($proto) = @_; my $object = data_clone($proto); $object->do_something(); return $object; } # ... # Bar is also clonable $o = Bar->new(); $c = clone($o); # Bar::clone() is called =head1 DESCRIPTION C does data cloning, i.e. copies things recursively. This is smart so that it works with not only non-blessed references, but also with blessed references (i.e. objects). When C finds an object, it calls a C method of the object if the object has a C, otherwise it makes a surface copy of the object. That is, this module does polymorphic data cloning. Although there are several modules on CPAN which can clone data, this module has a different cloning policy from almost all of them. See L and L for details. =head2 Cloning policy A cloning policy is a rule that how a cloning routine copies data. Here is the cloning policy of C. =head3 Non-reference values Non-reference values are copied normally, which will drop their magics. =head3 Scalar references Scalar references including references to other types of references are B copied deeply. They are copied on surface because it is typically used to refer to something unique, namely global variables or magical variables. =head3 Array references Array references are copied deeply. The cloning policy is applied to each value recursively. =head3 Hash references Hash references are copied deeply. The cloning policy is applied to each value recursively. =head3 Glob, IO and Code references These references are B copied deeply. They are copied on surface. =head3 Blessed references (objects) Blessed references are B copied deeply by default, because objects might have external resources which C could not deal with. They will be copied deeply only if C knows they are clonable, i.e. they have a C method. If you want to make an object clonable, you can use the C function as a method: package Your::Class; use Data::Clone; # ... my $your_class = Your::Class->new(); my $c = clone($your_object); # $your_object->clone() will be called Or you can import C function to define your custom clone method: package Your::Class; use Data::Clone qw(data_clone); sub clone { my($proto) = @_; my $object = data_clone($proto); # anything what you want return $object; } Of course, you can use C, C, and/or anything you want as an implementation of C methods. =head2 Comparison to other cloning modules There are modules which does data cloning. C is a standard module which can clone data with C. It has a different cloning policy from C. By default it tries to make a deep copy of all the data including blessed references, but you can change its behaviour with specific hook methods. C is a well-known cloning module, but it does not polymorphic cloning. This makes a deep copy of data regardless of its types. Moreover, there is no way to change its behaviour, so this is useful only for data which link to no external resources. C makes a deep copy of data only if it knows that the data are clonable. You can change its behaviour simply by defining C methods. It also exceeds C and C in performance. =head1 INTERFACE =head2 Exported functions =head3 B<< clone(Scalar) >> Returns a copy of I. =head2 Exportable functions =head3 B<< data_clone(Scalar) >> Returns a copy of I. The same as C. Provided for custom clone methods. =head3 B<< is_cloning() >> Returns true inside the C function, false otherwise. =head1 DEPENDENCIES Perl 5.8.1 or later, and a C compiler. =head1 BUGS No bugs have been reported. Please report any bugs or feature requests to the author. =head1 SEE ALSO L L =head1 AUTHOR Goro Fuji (gfx) Egfuji(at)cpan.orgE =head1 LICENSE AND COPYRIGHT Copyright (c) 2010, Goro Fuji (gfx). All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Clone.xs100664001751001751 2233114530305065 17333 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/lib/Data#define PERL_NO_GET_CONTEXT #define NO_XSLOCKS /* for exceptions */ #include "xshelper.h" #include "data_clone.h" #ifndef SvRXOK #define SvRXOK(sv) (SvROK(sv) && SvMAGICAL(SvRV(sv)) && mg_find(SvRV(sv), PERL_MAGIC_qr)) #endif #define REINTERPRET_CAST(T, value) ((T)value) #define PTR2STR(ptr) REINTERPRET_CAST(const char*, (&ptr)) #define MY_CXT_KEY "Data::Clone::_guts" XS_VERSION typedef struct { U32 depth; HV* seen; CV* caller_cv; GV* my_clone; GV* object_callback; SV* clone_method; /* "clone" */ SV* tieclone_method; /* "TIECLONE" */ } my_cxt_t; START_MY_CXT static SV* clone_rv(pTHX_ pMY_CXT_ SV* const cloning); static SV* clone_sv(pTHX_ pMY_CXT_ SV* const cloning) { assert(cloning); SvGETMAGIC(cloning); if(SvROK(cloning)){ return clone_rv(aTHX_ aMY_CXT_ cloning); } else{ SV* const cloned = newSV(0); /* no need to set SV_GMAGIC */ sv_setsv_flags(cloned, cloning, SV_NOSTEAL); return cloned; } } static void clone_hv_to(pTHX_ pMY_CXT_ HV* const cloning, HV* const cloned) { HE* iter; assert(cloning); assert(cloned); hv_iterinit(cloning); while((iter = hv_iternext(cloning))){ SV* const key = hv_iterkeysv(iter); SV* const val = clone_sv(aTHX_ aMY_CXT_ hv_iterval(cloning, iter)); (void)hv_store_ent(cloned, key, val, 0U); } } static void clone_av_to(pTHX_ pMY_CXT_ AV* const cloning, AV* const cloned) { I32 last, i; assert(cloning); assert(cloned); last = av_len(cloning); av_extend(cloned, last); for(i = 0; i <= last; i++){ SV** const svp = av_fetch(cloning, i, FALSE); if(svp){ (void)av_store(cloned, i, clone_sv(aTHX_ aMY_CXT_ *svp)); } } } static GV* find_method_sv(pTHX_ HV* const stash, SV* const name) { HE* const he = hv_fetch_ent(stash, name, FALSE, 0U); if(he && isGV(HeVAL(he)) && GvCV((GV*)HeVAL(he))){ /* shortcut */ return (GV*)HeVAL(he); } assert(SvPOKp(name)); return gv_fetchmeth_autoload(stash, SvPVX(name), SvCUR(name), 0); } static int sv_has_backrefs(pTHX_ SV* const sv) { if(SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_backref)) { return TRUE; } #ifdef HvAUX else if(SvTYPE(sv) == SVt_PVHV){ return SvOOK(sv) && HvAUX((HV*)sv)->xhv_backreferences != NULL; } #endif return FALSE; } /* my_dopoptosub_at() and caller_cv() are stolen from pp_ctl.c */ static I32 my_dopoptosub_at(pTHX_ const PERL_CONTEXT* const cxstk, I32 const startingblock) { I32 i; assert(cxstk); for (i = startingblock; i >= 0; i--) { const PERL_CONTEXT* const cx = &cxstk[i]; if(CxTYPE(cx) == CXt_SUB){ break; } } return i; } static CV* caller_cv(pTHX) { const PERL_CONTEXT* cx; const PERL_CONTEXT* ccstack = cxstack; const PERL_SI *si = PL_curstackinfo; I32 cxix = my_dopoptosub_at(aTHX_ ccstack, cxstack_ix); I32 count = 0; for (;;) { /* we may be in a higher stacklevel, so dig down deeper */ while (cxix < 0 && si->si_type != PERLSI_MAIN) { si = si->si_prev; ccstack = si->si_cxstack; cxix = my_dopoptosub_at(aTHX_ ccstack, si->si_cxix); } if (cxix < 0) { return NULL; } /* skip &DB::sub */ if (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub)) count++; if (!count--) break; cxix = my_dopoptosub_at(aTHX_ ccstack, cxix - 1); } cx = &ccstack[cxix]; return cx->blk_sub.cv; } static void store_to_seen(pTHX_ pMY_CXT_ SV* const sv, SV* const proto) { (void)hv_store(MY_CXT.seen, PTR2STR(sv), sizeof(sv), proto, 0U); SvREFCNT_inc_simple_void_NN(proto); } static SV* dc_call_sv1(pTHX_ SV* const proc, SV* const arg1) { dSP; SV* ret; assert(proc); assert(arg1); ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(arg1); PUTBACK; call_sv(proc, G_SCALAR); SPAGAIN; ret = POPs; PUTBACK; SvREFCNT_inc_simple_void_NN(ret); FREETMPS; LEAVE; return sv_2mortal(ret); } static int dc_need_to_call(pTHX_ pMY_CXT_ const CV* const method) { //warn("dc_need_co_call 0x%p 0x%p 0x%p", method, GvCV(MY_CXT.my_clone), MY_CXT.caller_cv); return method != GvCV(MY_CXT.my_clone) && method != MY_CXT.caller_cv; } static SV* dc_clone_object(pTHX_ pMY_CXT_ SV* const cloning, SV* const method_sv) { SV* const sv = SvRV(cloning); GV* const method = find_method_sv(aTHX_ SvSTASH(sv), method_sv); if(!method){ /* not a clonable object */ SV* const object_callback = GvSVn(MY_CXT.object_callback); /* try to $Data::Clone::ObjectCallback->($cloning) */ SvGETMAGIC(object_callback); if(SvOK(object_callback)){ SV* const x = dc_call_sv1(aTHX_ object_callback, cloning); if(!SvROK(x)){ croak("ObjectCallback function returned %s, but it must return a reference", SvOK(x) ? SvPV_nolen_const(x) : "undef"); } return x; } return sv_mortalcopy(cloning); croak("Non-clonable object %"SVf" found (missing a %"SVf" method)", cloning, method_sv); } /* has its own clone method */ if(dc_need_to_call(aTHX_ aMY_CXT_ GvCV(method))){ SV* const x = dc_call_sv1(aTHX_ (SV*)GvCV(method), cloning); if(!SvROK(x)){ croak("Cloning method '%"SVf"' returned %s, but it must return a reference", method_sv, SvOK(x) ? SvPV_nolen_const(x) : "undef"); } return x; } else { /* default clone() behavior: deep copy */ return NULL; } } static SV* clone_rv(pTHX_ pMY_CXT_ SV* const cloning) { int may_be_circular; SV* sv; SV* proto; SV* cloned; MAGIC* mg; //CV* old_cv; assert(cloning); assert(SvROK(cloning)); sv = SvRV(cloning); may_be_circular = (SvREFCNT(sv) > 1 || sv_has_backrefs(aTHX_ sv) ); if(may_be_circular){ SV** const svp = hv_fetch(MY_CXT.seen, PTR2STR(sv), sizeof(sv), FALSE); if(svp){ proto = *svp; goto finish; } } if(SvOBJECT(sv) && !SvRXOK(cloning)){ proto = dc_clone_object(aTHX_ aMY_CXT_ cloning, MY_CXT.clone_method); if(proto){ proto = SvRV(proto); goto finish; } /* fall through to make a deep copy */ } else if((mg = SvTIED_mg(sv, PERL_MAGIC_tied))){ assert(SvTYPE(sv) == SVt_PVAV || SvTYPE(sv) == SVt_PVHV); proto = dc_clone_object(aTHX_ aMY_CXT_ SvTIED_obj(sv, mg), MY_CXT.tieclone_method); if(proto){ SV* const varsv = (SvTYPE(sv) == SVt_PVHV ? (SV*)newHV() : (SV*)newAV()); // can we use newSV_type()? sv_magic(varsv, proto, PERL_MAGIC_tied, NULL, 0); proto = varsv; goto finish; } /* fall through to make a deep copy */ } /* XXX: need to save caller_cv, or not? */ //old_cv = MY_CXT.caller_cv; MY_CXT.caller_cv = NULL; if(SvTYPE(sv) == SVt_PVAV){ proto = sv_2mortal((SV*)newAV()); if(may_be_circular){ store_to_seen(aTHX_ aMY_CXT_ sv, proto); } clone_av_to(aTHX_ aMY_CXT_ (AV*)sv, (AV*)proto); } else if(SvTYPE(sv) == SVt_PVHV){ proto = sv_2mortal((SV*)newHV()); if(may_be_circular){ store_to_seen(aTHX_ aMY_CXT_ sv, proto); } clone_hv_to(aTHX_ aMY_CXT_ (HV*)sv, (HV*)proto); } else { proto = sv; /* do nothing */ } //MY_CXT.caller_cv = old_cv; finish: cloned = newRV_inc(proto); if(SvOBJECT(sv)){ sv_bless(cloned, SvSTASH(sv)); } return SvWEAKREF(cloning) ? sv_rvweaken(cloned) : cloned; } /* as SV* sv_clone(SV* sv) */ SV* Data_Clone_sv_clone(pTHX_ SV* const sv) { SV* VOL retval = NULL; CV* VOL old_cv; dMY_CXT; dXCPT; if(++MY_CXT.depth == U32_MAX){ croak("Depth overflow on clone()"); } old_cv = MY_CXT.caller_cv; MY_CXT.caller_cv = caller_cv(aTHX); XCPT_TRY_START { retval = sv_2mortal(clone_sv(aTHX_ aMY_CXT_ sv)); } XCPT_TRY_END MY_CXT.caller_cv = old_cv; if(--MY_CXT.depth == 0){ hv_undef(MY_CXT.seen); } XCPT_CATCH { XCPT_RETHROW; } return retval; } static void my_cxt_initialize(pTHX_ pMY_CXT) { MY_CXT.depth = 0; MY_CXT.seen = newHV(); MY_CXT.my_clone = CvGV(get_cvs("Data::Clone::clone", GV_ADD)); MY_CXT.object_callback = gv_fetchpvs("Data::Clone::ObjectCallback", GV_ADDMULTI, SVt_PV); MY_CXT.clone_method = newSVpvs_share("clone"); MY_CXT.tieclone_method = newSVpvs_share("TIECLONE"); } MODULE = Data::Clone PACKAGE = Data::Clone PROTOTYPES: DISABLE BOOT: { MY_CXT_INIT; my_cxt_initialize(aTHX_ aMY_CXT); } #ifdef USE_ITHREADS void CLONE(...) CODE: { MY_CXT_CLONE; my_cxt_initialize(aTHX_ aMY_CXT); PERL_UNUSED_VAR(items); } #endif void clone(SV* sv) CODE: { sv = sv_clone(sv); ST(0) = sv; XSRETURN(1); } bool is_cloning() CODE: { dMY_CXT; RETVAL = (MY_CXT.depth != 0); } OUTPUT: RETVAL data_clone.h100664001751001751 55514530305065 20125 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/lib/Data/* * data_clone.h - Polymorphic data cloning engine * * Tihs header file is a part of Data::Clone * * Copyright (c) 2010, Goro Fuji (gfx). * * See also http://search.cpan.org/dist/Data-Clone/. */ #ifndef PERL_DATA_CLONE_H #define PERL_DATA_CLONE_H SV* Data_Clone_sv_clone(pTHX_ SV* const sv); #define sv_clone(sv) Data_Clone_sv_clone(aTHX_ (sv)) #endif minil.toml100664001751001751 30314530305065 16240 0ustar00ishigakiishigaki000000000000Data-Clone-0.006name = "Data-Clone" badges = ["github-actions/test.yml"] module_maker="ModuleBuild" [XSUtil] generate_xshelper_h = "lib/Data/xshelper.h" generate_ppport_h = "lib/Data/ppport.h" cc_warnings = 1 00_load.t100644001751001751 20614530305065 16101 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use Test::More tests => 1; BEGIN { use_ok 'Data::Clone' } diag "Testing Data::Clone/$Data::Clone::VERSION"; 01_basic.t100644001751001751 331514530305065 16270 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use warnings FATAL => 'all'; use Test::More; use Data::Dumper; use Data::Clone; use Tie::Hash; use Tie::Array; $Data::Dumper::Indent = 0; $Data::Dumper::Sortkeys = 1; ok defined(&clone), 'clone() is exported by default'; ok!defined(&data_clone), 'data_clone() is not exported by default'; for(1 .. 2){ # do it twice to test internal data foreach my $data( "foo", 3.14, 1 != 1, *STDOUT, ["foo", "bar", undef, 42], [qr/foo/, qr/bar/], [\*STDOUT, \*STDOUT], { key => [ 'value', \&ok ] }, { foo => { bar => { baz => 42 } } }, ){ note("for $data"); is Dumper(clone($data)), Dumper($data), 'data'; is Dumper(clone(\$data)), Dumper(\$data), 'data ref'; } my $s; $s = \$s; is Dumper(clone(\$s)), Dumper(\$s), 'ref to self (scalar)'; my @a; @a = \@a; is Dumper(clone(\@a)), Dumper(\@a), 'ref to self (array)'; my %h; $h{foo} = \%h; is Dumper(clone(\%h)), Dumper(\%h), 'ref to self (hash)'; @a = ('foo', 'bar', \%h, \%h); is Dumper(clone(\@a)), Dumper(\@a), 'ref to duplicated refs'; # correctly cloned? $s = 99; %h = (foo => 10, bar => 10, baz => [10], qux => \$s); my $cloned = clone(\%h); $cloned->{foo}++; $cloned->{baz}[0]++; cmp_ok $cloned, '!=', \%h, 'different entity'; is Dumper($cloned), Dumper({foo => 11, bar => 10, baz => [11], qux => \$s}), 'deeply copied'; is Dumper(\%h), Dumper({foo => 10, bar => 10, baz => [10], qux => \$s}), 'the original is not touched'; $s++; is ${$h{qux}}, 100; is ${$cloned->{qux}}, 100, 'scalar ref is not copied deeply'; } done_testing; 02_object.t100644001751001751 501214530305065 16452 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use warnings FATAL => 'all'; use Test::More; use Data::Clone; { package MyBase; sub new { my $class = shift; return bless {@_}, $class; } package MyNoclonable; our @ISA = qw(MyBase); package MyClonable; use Data::Clone; our @ISA = qw(MyBase); package MyCustomClonable; use Data::Clone qw(data_clone); our @ISA = qw(MyBase); sub clone { my $cloned = data_clone(@_); $cloned->{bar} = 42; return $cloned; } package FatalClonable; our @ISA = qw(MyBase); sub clone { die 'FATAL'; } } for(1 .. 2){ # do it twice to test internal data note($_); my($o, $c); $o = MyNoclonable->new(foo => 10); eval { local $Data::Clone::ObjectCallback = sub{ die 'Non-clonable object' }; $c = clone($o); }; like $@, qr/Non-clonable object/, 'die on non-clonables'; is $c, undef; eval { $c = clone($o); }; is $@, ''; is $c, $o; $c->{foo}++; is $o->{foo}, 11, 'noclonable with surface copy'; $o = MyClonable->new(foo => 10); $c = clone($o); isnt $c, $o; $c->{foo}++; is $o->{foo}, 10, 'clonable'; $o = MyCustomClonable->new(foo => 10); $c = clone($o); isnt $c, $o; $c->{foo}++; is $o->{foo}, 10, 'clonable'; is_deeply $c, { foo => 11, bar => 42 }, 'custom clone()'; $o = MyClonable->new( aaa => [[42], MyCustomClonable->new(value => 100)], bbb => [[42], MyCustomClonable->new(value => 200)], ); $c = clone($o); $c->{aaa}[1]{value}++; $c->{bbb}[1]{value}++; is $o->{aaa}[1]{value}, 100, 'clone() is reentrant'; is $c->{aaa}[1]{value}, 101; is $c->{aaa}[1]{bar}, 42; is $o->{bbb}[1]{value}, 200, 'clone() is reentrant'; is $c->{bbb}[1]{value}, 201; is $c->{bbb}[1]{bar}, 42; $o = MyCustomClonable->new(); $o->{ccc} = [MyCustomClonable->new(value => 300)]; $o->{ddd} = $o->{ccc}; $c = clone($o); $c->{ccc}[0]{value}++; $c->{ddd}[0]{value}++; is $o->{ccc}[0]{value}, 300; is $c->{ccc}[0]{value}, 302; is $c->{ccc}[0]{bar}, 42, 'clone methods in clone()'; $o = FatalClonable->new(foo => 10); eval{ clone($o); }; like $@, qr/^FATAL \b/xms, 'FATAL in clone()'; is $o->{foo}, 10; $o = MyCustomClonable->new(value => FatalClonable->new(foo => 10)); eval{ clone($o); }; like $@, qr/^FATAL \b/xms, 'FATAL in clone()'; is $o->{value}{foo}, 10; } done_testing; 03_scalar_ref.t100644001751001751 52214530305065 17267 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use warnings FATAL => 'all'; use Test::More; use Data::Clone; for(1 .. 2){ # do it twice to test internal data my $s = 'foobar'; my @a = (\substr $s, 1, 2); my $c = clone(\@a); is ${$c->[0]}, 'oo'; ${$c->[0]} = 'xx'; is $s, 'fxxbar', 'ScalarRef is copied in surface'; } done_testing; 04_tree.t100644001751001751 210714530305065 16147 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use warnings FATAL => 'all'; use Test::More; use Data::Clone; use Scalar::Util qw(isweak weaken); use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Sortkeys = 1; # hash tree my $parent = {}; my $child = {}; $child->{parent} = $parent; $parent->{child} = $child; weaken $child->{parent}; my $cloned = clone($child); is Dumper($cloned), Dumper({ parent => undef }), 'tree structure (child)'; $cloned = clone($parent); is Dumper($cloned), Dumper($parent), 'tree structure (parent)'; cmp_ok $cloned, '==', $cloned->{child}{parent}, 'as circular refs'; ok isweak($cloned->{child}{parent}), 'correctly weaken'; # array tree $parent = ['is_parent']; $child = ['is_child']; push @{$child}, $parent; push @{$parent}, $child; weaken $child->[1]; $cloned = clone($child); is Dumper($cloned), Dumper(['is_child', undef]), 'array tree (child)'; $cloned = clone($parent); is Dumper($cloned), Dumper($parent), 'array tree (parent)'; cmp_ok $cloned, '==', $cloned->[1][1], 'as sircular refs'; ok isweak($cloned->[1][1]), 'correctly weaken'; done_testing; 05_super.t100644001751001751 411614530305065 16351 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use warnings FATAL => 'all'; use Test::More; use Data::Clone; use Scalar::Util qw(isweak weaken); use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Sortkeys = 1; my $c_clone_called; { package A; use Data::Clone; # make clonable sub new { my($class, @args) = @_; return bless {@args}, $class; } package B; our @ISA = qw(A); package C; our @ISA = qw(B); sub clone { my($self) = @_; my $cloned = $self->SUPER::clone(); $cloned->{'c_clone'} = 1; $c_clone_called++; return $cloned; } package D; our @ISA = qw(C); package E; our @ISA = qw(D); } my $b = B->new(foo => 10); my $c = C->new(bar => 20); for(1 .. 2){ note($_); is Dumper($b->clone), Dumper(bless { foo => 10 }, 'B'), 'inherited clone method'; is Dumper(clone($b)), Dumper(bless { foo => 10 }, 'B'), 'inherited clone method via clone() function'; $c_clone_called = 0; is Dumper($c->clone), Dumper(bless { bar => 20, c_clone => 1 }, 'C'), 'work with SUPER::clone()'; is $c_clone_called, 1; $c_clone_called = 0; is Dumper(clone($c)), Dumper(bless { bar => 20, c_clone => 1 }, 'C'), 'work with SUPER::clone()'; is $c_clone_called, 1; is Dumper($c), Dumper(bless { bar => 20 }, 'C'); for my $class(qw(C D E)){ note("for $class"); my @h = ( $class->new(a => 1), $class->new(a => 2), $class->new(a => 3) ); $c_clone_called = 0; is Dumper(clone(\@h)), Dumper([ ( $class->new(a => 1, c_clone => 1), $class->new(a => 2, c_clone => 1), $class->new(a => 3, c_clone => 1) ) ]); is $c_clone_called, 3; my $o = $class->new( c => [$class->new(foo => 42)], c2 => [$class->new(foo => 52)], ); $c_clone_called = 0; is Dumper(clone($o)), Dumper($class->new( c => [$class->new(foo => 42, c_clone => 1)], c2 => [$class->new(foo => 52, c_clone => 1)], c_clone => 1)); is $c_clone_called, 3; } } done_testing; 06_tie.t100644001751001751 374414530305065 16003 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use warnings FATAL => 'all'; use Test::More; use Data::Clone; use Tie::Hash; use Tie::Array; use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Useqq = 1; { package MyNonclonableHash; our @ISA = qw(Tie::StdHash); package MyNonclonableArray; our @ISA = qw(Tie::StdArray); package MyClonableHash; use Data::Clone qw(TIECLONE); our @ISA = qw(Tie::StdHash); package MyClonableArray; use Data::Clone qw(TIECLONE); our @ISA = qw(Tie::StdArray); } # HASH foreach (1 .. 2){ note "for HASH ($_)"; tie my %h, 'MyNonclonableHash'; $h{foo} = 42; $h{bar} = "xyzzy"; my $c; eval{ local $Data::Clone::ObjectCallback = sub{ die 'Non-clonable object' }; clone(\%h); }; like $@, qr/Non-clonable object/, 'clone() croaks'; eval{ $c = clone(\%h); }; is $@, ''; is tied(%{$c}), tied(%h), 'sutface copy'; $c->{foo}++; is Dumper($c), Dumper({ foo => 43, bar => "xyzzy" }); is Dumper(\%h), Dumper({ foo => 43, bar => "xyzzy" }); tie %h, 'MyClonableHash'; $h{foo} = 42; $h{bar} = "xyzzy"; $c = clone(\%h); isnt $c, \%h; $c->{foo}++; is Dumper($c), Dumper({ foo => 43, bar => "xyzzy" }); is Dumper(\%h), Dumper({ foo => 42, bar => "xyzzy" }); # ARRAY note("for ARRAY ($_)"); tie my @a, 'MyNonclonableArray'; @a = (42, "xyzzy"); eval{ local $Data::Clone::ObjectCallback = sub{ die 'Non-clonable object' }; clone(\@a); }; like $@, qr/Non-clonable object/, 'clone() croaks'; eval{ $c = clone(\@a); }; is tied(@{$c}), tied(@a), 'sutface copy'; $c->[0]++; is Dumper($c), Dumper([43, "xyzzy"]); is Dumper(\@a), Dumper([43, "xyzzy"]); tie @a, 'MyClonableArray'; @a = (42, "xyzzy"); $c = clone(\@a); $c->[0]++; is Dumper($c), Dumper([43, "xyzzy"]); is Dumper(\@a), Dumper([42, "xyzzy"]); } done_testing; 07_stack.t100644001751001751 51014530305065 16274 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use warnings FATAL => 'all'; use Test::More; use Data::Clone; { package Bar; sub clone { () = (1)x100000; # extend the stack return [] } } my $before = bless [], Bar::; my $after = clone($before); isnt $after, $before, 'stack reallocation during callback'; done_testing; 10_threads.t100644001751001751 431014530305065 16635 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use constant HAS_THREADS => eval{ require threads }; use if !HAS_THREADS, 'Test::More', skip_all => 'This test requires threads'; use Test::More; use warnings FATAL => 'all'; use Data::Clone; use Time::HiRes qw(usleep); { package MyBase; sub new { my $class = shift; return bless {@_}, $class; } package MyNoclonable; our @ISA = qw(MyBase); package MyClonable; use Data::Clone; our @ISA = qw(MyBase); package MyCustomClonable; use Data::Clone qw(data_clone); our @ISA = qw(MyBase); sub clone { my $cloned = data_clone(@_); $cloned->{bar} = 42; return $cloned; } package CreateThreadsInClone; use Data::Clone qw(data_clone); our @ISA = qw(MyBase); sub clone { my $cloned = data_clone(@_); $cloned->{bar} = threads->create(sub{ data_clone([42]) })->join(); return $cloned; } } my @threads; for(1 .. 3){ push @threads, threads->create(sub{ usleep 10;; my $o = MyNoclonable->new(foo => 10); my $c = do{ local $Data::Clone::ObjectCallback = sub{ $_[0] }; clone($o); }; is $c, $o, "tid - " . threads->tid; $c->{foo}++; is $o->{foo}, 11, 'noclonable'; usleep 10; $o = MyClonable->new(foo => 10); $c = clone($o); isnt $c, $o; $c->{foo}++; is $o->{foo}, 10, 'clonable'; usleep 10; $o = MyCustomClonable->new(foo => 10); $c = clone($o); isnt $c, $o; $c->{foo}++; is $o->{foo}, 10, 'clonable'; is_deeply $c, { foo => 11, bar => 42 }, 'custom clone()'; usleep 10; $o = MyCustomClonable->new(foo => MyClonable->new(bar => 42)); $c = clone($o); $c->{foo}{bar}++; is $o->{foo}{bar}, 42, 'clone() is reentrant'; is $c->{foo}{bar}, 43; $o = CreateThreadsInClone->new(foo => 50); $c = clone($o); usleep 10; is $c->{foo}, 50; is_deeply $c->{bar}, [42], 'threads->create in clone()'; return threads->tid; }); } foreach my $thr(@threads){ pass "\$thr->join: " . $thr->join; } done_testing; 11_leaktrace.t100644001751001751 255114530305065 17144 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/t#!perl -w use strict; use Test::Requires qw(Test::LeakTrace); use Test::More; use warnings FATAL => 'all'; use Data::Clone; { package MyBase; sub new { my $class = shift; return bless {@_}, $class; } package MyNoclonable; our @ISA = qw(MyBase); package MyClonable; use Data::Clone; our @ISA = qw(MyBase); package MyCustomClonable; use Data::Clone qw(data_clone); our @ISA = qw(MyBase); sub clone { my $cloned = data_clone(@_); $cloned->{bar} = 42; return $cloned; } package FatalClonable; use Data::Clone qw(data_clone); our @ISA = qw(MyBase); sub clone { my $cloned = data_clone(@_); die 'FATAL'; } } no_leaks_ok { my $o = [ 42 ]; my $c = clone($o); } or die "Memory leaked"; no_leaks_ok { local $Data::Clone::ObjectCallback = sub{ $_[0] }; my $o = MyNoclonable->new(foo => 10); my $c = clone($o); }; no_leaks_ok { my $o = MyClonable->new(foo => 20); my $c = clone($o); }; no_leaks_ok { my $o = MyCustomClonable->new(foo => 30); my $c = clone($o); }; no_leaks_ok { my $o = MyCustomClonable->new(foo => MyClonable->new(bar => 42)); my $c = clone($o); }; no_leaks_ok { my $o = FatalClonable->new(value => MyClonable->new(foo => 50)); eval{ clone($o) }; } 'fatal in clone()'; done_testing; 02_pod.t100644001751001751 23414530305065 16137 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/xt#!perl -w use strict; use Test::More; eval q{use Test::Pod 1.14}; plan skip_all => 'Test::Pod 1.14 required for testing PODU' if $@; all_pod_files_ok(); 03_pod-coverage.t100644001751001751 32314530305065 17730 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/xt#!perl -w use Test::More; eval q{use Test::Pod::Coverage 1.04}; plan skip_all => 'Test::Pod::Coverage 1.04 required for testing POD coverage' if $@; all_pod_coverage_ok({ also_private => [qw(TIECLONE)], }); 04_synopsis.t100644001751001751 22314530305065 17244 0ustar00ishigakiishigaki000000000000Data-Clone-0.006/xt#!perl -w use strict; use Test::More; eval q{use Test::Synopsis}; plan skip_all => 'Test::Synopsis required for testing' if $@; all_synopsis_ok(); META.yml100664001751001751 236014530305065 15531 0ustar00ishigakiishigaki000000000000Data-Clone-0.006--- abstract: 'Polymorphic data cloning' author: - 'Goro Fuji (gfx) ' build_requires: Devel::PPPort: '3.19' ExtUtils::MakeMaker: '6.59' ExtUtils::ParseXS: '3.18' Module::Build: '0' Module::Build::XSUtil: '0' Test::More: '0.88' Test::Requires: '0.03' configure_requires: Module::Build: '0.4005' Module::Build::XSUtil: '0.03' dynamic_config: 0 generated_by: 'Minilla/v3.1.23, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Data-Clone no_index: directory: - t - xt - inc - share - eg - examples - author - builder provides: Data::Clone: file: lib/Data/Clone.pm version: '0.006' requires: XSLoader: '0.02' parent: '0' perl: '5.008001' resources: bugtracker: https://github.com/gfx/p5-Data-Clone/issues homepage: https://github.com/gfx/p5-Data-Clone repository: https://github.com/gfx/p5-Data-Clone.git version: '0.006' x_contributors: - 'Fuji, Goro (gfx) ' - 'Kenichi Ishigaki ' - 'Tom Hukins ' - 'Yves Orton ' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' x_static_install: 0 MANIFEST100664001751001751 66214530305065 15374 0ustar00ishigakiishigaki000000000000Data-Clone-0.006Build.PL Changes LICENSE META.json README.md benchmark/method.pl benchmark/object.pl benchmark/vs_clone.pl benchmark/vs_fast.pl cpanfile example/scalar_refs.pl lib/Data/Clone.pm lib/Data/Clone.xs lib/Data/data_clone.h minil.toml t/00_load.t t/01_basic.t t/02_object.t t/03_scalar_ref.t t/04_tree.t t/05_super.t t/06_tie.t t/07_stack.t t/10_threads.t t/11_leaktrace.t xt/02_pod.t xt/03_pod-coverage.t xt/04_synopsis.t META.yml MANIFEST