File Coverage

blib/lib/Clone/Util.pm
Criterion Covered Total %
statement 25 28 89.2
branch 2 6 33.3
condition 2 3 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 38 46 82.6


line stmt bran cond sub pod time code
1             package Clone::Util;
2              
3             our $DATE = '2016-03-10'; # DATE
4             our $VERSION = '0.03'; # VERSION
5              
6 1     1   382 use 5.010001;
  1         2  
7 1     1   3 use strict;
  1         1  
  1         13  
8 1     1   3 use warnings;
  1         0  
  1         20  
9              
10 1     1   373 use Function::Fallback::CoreOrPP qw(clone);
  1         457  
  1         52  
11              
12 1     1   4 use Exporter qw(import);
  1         1  
  1         132  
13             our @EXPORT_OK = qw(clone modclone sclone);
14              
15             sub modclone(&$;@) {
16 4     4 1 2810 my $code = shift;
17 4         4 my $data = shift;
18 4         7 my $clone = clone($data);
19 4         32 local $_ = $clone;
20 4         7 $code->($clone);
21 4 50 66     24 if (@_ || wantarray) {
22 4         14 return ($clone, @_);
23             } else {
24 0         0 return $clone;
25             }
26             }
27              
28             sub sclone($) {
29 1     1 1 1306 my $data = shift;
30 1         2 my $ref = ref($data);
31 1 50       2 if ($ref eq 'ARRAY') {
    0          
32 1         3 return [@$data];
33             } elsif ($ref eq 'HASH') {
34 0           return {%$data};
35             } else {
36 0           die "Cannot shallow clone $data ($ref)";
37             }
38             }
39              
40             1;
41             # ABSTRACT: Utilities related to cloning
42              
43             __END__
44              
45             =pod
46              
47             =encoding UTF-8
48              
49             =head1 NAME
50              
51             Clone::Util - Utilities related to cloning
52              
53             =head1 VERSION
54              
55             This document describes version 0.03 of Clone::Util (from Perl distribution Clone-Util), released on 2016-03-10.
56              
57             =head1 SYNOPSIS
58              
59             use Clone::Util qw(modclone sclone);
60              
61             my $row = {name=>'ujang', gender=>'m', nationality=>'id'};
62             my $table = [
63             $row,
64             modclone { $_->{name} = 'budi' } $row,
65             modclone { $_->{name} = 'asep' } $row,
66             modclone { $_->{name} = 'rini'; $_->{gender} = 'f' } $row,
67             ];
68              
69             my $shallow_clone = sclone($data);
70              
71             =head1 FUNCTIONS
72              
73             None of the functions are exported by default, but they are exportable.
74              
75             =head2 clone($data) => $cloned
76              
77             This is just re-exported L<Function::Fallback::CoreOrPP>'s C<clone()>.
78              
79             =head2 modclone(\&code, $data) => $clone
80              
81             Clone C<$data> and then run code. Code will be given the clone of data. For
82             convenience, C<$_> will also be localized and set to the clone of data. So you
83             can access the clone using C<$_> in addition to C<$_[0]>.
84              
85             =head2 sclone($data) => $clone
86              
87             Shallow-clone C<$data>, which must be an arrayref or a hashref. Shallow cloning
88             means only copying the first level of data.
89              
90             my $array = [0,1,2,[3]];
91             my $clone = sclone $array; # => [0,1,2,[3]], but [3] is still the same reference
92              
93             $clone->[3][0] = "three"; # $clone as well as $array become [0,1,2,["three"]]
94              
95             $clone->[0] = "zero"; # $clone becomes ["zero",1,...] but $array still [0,1,...]
96              
97             You can perform shallow copying trivially yourself using:
98              
99             my $cloned_array = [@$array];
100             my $cloned_hash = {%$hash};
101              
102             =head1 HOMEPAGE
103              
104             Please visit the project's homepage at L<https://metacpan.org/release/Clone-Util>.
105              
106             =head1 SOURCE
107              
108             Source repository is at L<https://github.com/perlancar/perl-Clone-Util>.
109              
110             =head1 BUGS
111              
112             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Clone-Util>
113              
114             When submitting a bug or request, please include a test-file or a
115             patch to an existing test-file that illustrates the bug or desired
116             feature.
117              
118             =head1 SEE ALSO
119              
120             L<Function::Fallback::CoreOrPP>
121              
122             =head1 AUTHOR
123              
124             perlancar <perlancar@cpan.org>
125              
126             =head1 COPYRIGHT AND LICENSE
127              
128             This software is copyright (c) 2016 by perlancar@cpan.org.
129              
130             This is free software; you can redistribute it and/or modify it under
131             the same terms as the Perl 5 programming language system itself.
132              
133             =cut