blib/lib/CXC/Gnuplot/V1/Role/Clone.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/CXC/Gnuplot/V1/Range.pm ↑ Summary blib/lib/CXC/Gnuplot/V1/Timestamp.pm »
1 package CXC::Gnuplot::V1::Role::Clone;
2  
3 1 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use experimental 'builtin', 'declared_refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Object::Pad 0.821;
Subroutine BEGIN: called CC=1 SCAR=2.6
6  
7 our $VERSION = 'v0.29.3';
8  
9 role CXC::Gnuplot::V1::Role::Clone;
10  
11 method to_hash;
12  
13 use builtin 'is_bool', 'true', 'blessed';
14 use Hash::Merge;
15  
16 use Lexical::Import qw( Ref::Util is_plain_arrayref );
17  
18 ## no critic ( AmbiguousNames )
19  
20 my $merger = do {
21 my $obj = Hash::Merge->new;
22 $obj->add_behavior_spec( {
23 'SCALAR' => {
24 'SCALAR' => sub { $_[1] },
25 'ARRAY' => sub { $_[1] },
26 'HASH' => sub { $_[1] },
27 },
28 'ARRAY' => {
29 'SCALAR' => sub { $_[1] },
30 'ARRAY' => sub { $_[1] },
31 'HASH' => sub { $_[1] },
32 },
33 'HASH' => {
34 'SCALAR' => sub { $_[1] },
35 'ARRAY' => sub { $_[1] },
36 'HASH' => sub ( $left, $right ) {
37 my \%left = $left;
38 my \%right = $right;
39  
40 # delete the specified entries from $left
41 if ( defined( my $delete = delete $right{-delete} ) ) {
42 # delete everything
43 if ( is_bool( $delete ) ) {
44 %left = () if $delete;
45 }
46 else {
47 # delete specific keys
48 delete @left{ is_plain_arrayref( $delete ) ? $delete->@* : $delete };
49 }
50 }
51  
52 if ( defined( my $overwrite = delete $right{-overwrite} ) ) {
53  
54 # overwrite keys in $left with keys in $right
55 # removing them from %right so they can't be merged
56 my @keys
57 = is_bool( $overwrite ) ? ( $overwrite ? keys %right : () )
58 : is_plain_arrayref( $overwrite ) ? $overwrite->@*
59 : ( $overwrite );
60 @left{@keys} = delete @right{@keys};
61 }
62  
63  
64 return \%left unless keys %right;
65  
66 # merge what's left
67 # _merge_hashes is part of the public API
68 ## no critic ( Subroutines::ProtectPrivateSubs )
69 Hash::Merge::_merge_hashes( \%left, \%right );
70 },
71 },
72 },
73 __PACKAGE__,
74 );
75 # this is on by default, but let's make it explicit
76 $obj->set_clone_behavior( true );
77 $obj;
78 };
79  
80  
81  
82  
83  
84 method clone ( %new ) {
85 my $class = blessed( $self );
86 my \%old = $self->to_hash;
87 my \%attr = $merger->merge( \%old, \%new );
88 return $class->new( %attr );
89 }
90  
91  
92 1;
93  
94 #
95 # This file is part of CXC-Gnuplot
96 #
97 # This software is Copyright (c) 2024 by Smithsonian Astrophysical Observatory.
98 #
99 # This is free software, licensed under:
100 #
101 # The GNU General Public License, Version 3, June 2007
102 #
103  
104 __END__
« blib/lib/CXC/Gnuplot/V1/Range.pm ↑ Summary blib/lib/CXC/Gnuplot/V1/Timestamp.pm »