| 1 | =head1 NAME | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | DBIx::SQLEngine::Utility::CloneWithParams - Nifty Cloner | ||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | use DBIx::SQLEngine::Utility::CloneWithParams; | ||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | $clone = clone_with_parameters( $string, @replacements ); | ||||||||||||||||||||||
| 10 | $clone = clone_with_parameters( \@array, @replacements ); | ||||||||||||||||||||||
| 11 | $clone = clone_with_parameters( \%hash, @replacements ); | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | This package provides a function named clone_with_parameters() that makes deep copies of nested data structures, while making replacements in key places. | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | =head2 clone_with_parameters | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | $clone = clone_with_parameters( $reference, @replacements ); | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | This function makes deep copies of nested data structures, with object | ||||||||||||||||||||||
| 22 | reblessing and loop detection to avoid endless cycles. (The internals are | ||||||||||||||||||||||
| 23 | based on clone() from L<Clone::PP>.) | ||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | It's one distinctive behavior is that if a data structure contains references | ||||||||||||||||||||||
| 26 | to the special numeric Perl variables $1, $2, $3, and so forth, when it is | ||||||||||||||||||||||
| 27 | cloned they are replaced with a set of provided parameter values. It also | ||||||||||||||||||||||
| 28 | replaces stringified versions of those references embedded in scalar values. | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | An exception is thrown if the number of parameters provided does not match | ||||||||||||||||||||||
| 31 | the number of special variables referred to. | ||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | B<Limitations:> | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | =over 2 | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | =item * | ||||||||||||||||||||||
| 38 | |||||||||||||||||||||||
| 39 | This will not properly copy tied data. | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | =item * | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | Using this to clone objects will only work with simple objects that don't | ||||||||||||||||||||||
| 44 | do much preprocessing of the values they contain. | ||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | =back | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | B<Examples:> | ||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | =over 2 | ||||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | =item * | ||||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | Here's a simple copy of a string with embedded values to be provided by the caller: | ||||||||||||||||||||||
| 55 | |||||||||||||||||||||||
| 56 | my $template = \$1 . '-' . \$2; | ||||||||||||||||||||||
| 57 | my $clone = clone_with_parameters( $template, 'Foozle', 'Basil' ); | ||||||||||||||||||||||
| 58 | ok( $clone, 'Foozle-Basil' ); | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | =item * | ||||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | Here's a simple cloning of an array with values to be provided by the caller: | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | my $template = [ \$1, '-', \$2 ]; | ||||||||||||||||||||||
| 65 | my $clone = clone_with_parameters( $template, 'Foozle', 'Basil' ); | ||||||||||||||||||||||
| 66 | is_deeply( $clone, [ 'Foozle', '-', 'Basil' ] ); | ||||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | =item * | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | Here's a simple cloning of a hash with key values to be provided by the caller: | ||||||||||||||||||||||
| 71 | |||||||||||||||||||||||
| 72 | my $template = { foo => \$1, bar => \$2 }; | ||||||||||||||||||||||
| 73 | my $clone = clone_with_parameters( $template, 'Foozle', 'Basil' ); | ||||||||||||||||||||||
| 74 | is_deeply( $clone, { foo => 'Foozle', bar => 'Basil' } ); | ||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | =item * | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | Templates to be copied can contain nested data structures, and can use paramters multiple times: | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | my $template = { foo => \$1, bar => [ \$2, 'baz', \$2 ] }; | ||||||||||||||||||||||
| 81 | my $clone = clone_with_parameters( $template, 'Foozle', 'Basil' ); | ||||||||||||||||||||||
| 82 | is_deeply( $clone, { foo=>'Foozle', bar=>['Basil','baz','Basil'] } ); | ||||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | =item * | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | Although hash keys are automatically stringified, they still are substituted: | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | my $template = { foo => 'bar', \$1 => \$2 }; | ||||||||||||||||||||||
| 89 | my $clone = clone_with_parameters( $template, 'Foozle', 'Basil' ); | ||||||||||||||||||||||
| 90 | is_deeply( $clone, { foo => 'bar', Foozle => 'Basil' } ); | ||||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | =item * | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | Objects can be copied to produce properly-blessed clones: | ||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | package My::SimpleObject; | ||||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | sub new { my $class = shift; bless { @_ } $class } | ||||||||||||||||||||||
| 99 | sub foo { ( @_ == 1 ) ? $_[0]->{foo} : ( $_[0]->{foo} = $_[1] ) } | ||||||||||||||||||||||
| 100 | sub bar { ( @_ == 1 ) ? $_[0]->{bar} : ( $_[0]->{bar} = $_[1] ) } | ||||||||||||||||||||||
| 101 | |||||||||||||||||||||||
| 102 | package main; | ||||||||||||||||||||||
| 103 | use DBIx::SQLEngine::Utility::CloneWithParams; | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | my $template = My::SimpleObject->new( foo => \$1, bar => \$2 ); | ||||||||||||||||||||||
| 106 | my $clone = clone_with_parameters( $template, 'Foozle', 'Basil' ); | ||||||||||||||||||||||
| 107 | isa_ok( $clone, 'My::SimpleObject' ); | ||||||||||||||||||||||
| 108 | ok( $clone->foo, 'Foozle' ); | ||||||||||||||||||||||
| 109 | ok( $clone->bar, 'Basil' ); | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | If the class itself imports clone_with_parameters(), it can be called as a method instead of a function: | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | package My::SimpleObject; | ||||||||||||||||||||||
| 114 | use DBIx::SQLEngine::Utility::CloneWithParams; | ||||||||||||||||||||||
| 115 | ... | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | package main; | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | my $template = My::SimpleObject->new( foo => \$1, bar => \$2 ); | ||||||||||||||||||||||
| 120 | my $clone = $template->clone_with_parameters( 'Foozle', 'Basil' ); | ||||||||||||||||||||||
| 121 | ... | ||||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | =back | ||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | ######################################################################## | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | package DBIx::SQLEngine::Utility::CloneWithParams; | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 13 | ▶ | use Exporter; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 132 | 25 | ▶ | sub import { goto &Exporter::import } | ||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 133 | 13 | @EXPORT = @EXPORT_OK = qw( clone_with_parameters safe_eval_with_parameters ); | |||||||||||||||||||||
| 134 | 13 | %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | 13 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 137 | 13 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | ######################################################################## | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | 13 | my @num_refs = map { \$_ } ( $1, $2, $3, $4, $5, $6, $7, $8, $9 ); | |||||||||||||||||||||
| 142 | 13 | my $num_refs = join "|", map { "\Q$_" } @num_refs; | |||||||||||||||||||||
| 143 | 13 | my %num_refs = map { $num_refs[ $_ -1 ] => $_ } ( 1 .. 9 ); | |||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | ######################################################################## | ||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | 13 | ▶ | use vars qw( %CopiedItems @Parameters @ParametersUsed ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | # $deep_copy = clone_with_parameters( $value_or_ref ); | ||||||||||||||||||||||
| 150 | 28 | ▶ | sub clone_with_parameters { | ||||||||||||||||||||
|
Subroutine
clone_with_parameters: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 151 | 28 | my $item = shift; | |||||||||||||||||||||
| 152 | 28 | local @Parameters = @_; | |||||||||||||||||||||
| 153 | 28 | local %CopiedItems = (); | |||||||||||||||||||||
| 154 | 28 | local @ParametersUsed = (); | |||||||||||||||||||||
| 155 | 28 | my $clone = __clone_with_parameters( $item ); | |||||||||||||||||||||
| 156 | 27 | ▶ | if ( scalar @ParametersUsed < scalar @Parameters ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 157 | 1 | confess( "Too many arguments: " . scalar(@Parameters) . | |||||||||||||||||||||
| 158 | " instead of " . scalar(@ParametersUsed)); | ||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||
| 160 | 26 | return $clone; | |||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | 30 | ▶ | sub __get_parameter { | ||||||||||||||||||||
|
Subroutine
__get_parameter: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 164 | 30 | my $placeholder = shift; | |||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | 30 | ▶ | if ( $placeholder > scalar @Parameters ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 167 | 1 | confess( "Too few arguments: " . scalar(@Parameters) . | |||||||||||||||||||||
| 168 | " instead of $placeholder"); | ||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||
| 170 | 29 | $ParametersUsed[ $placeholder -1 ] ++; | |||||||||||||||||||||
| 171 | 29 | return $Parameters[ $placeholder -1 ]; | |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||
| 173 | |||||||||||||||||||||||
| 174 | # $copy = __clone_with_parameters( $value_or_ref ); | ||||||||||||||||||||||
| 175 | 97 | ▶ | sub __clone_with_parameters { | ||||||||||||||||||||
|
Subroutine
__clone_with_parameters: called CC=10 SCAR=23.0
| |||||||||||||||||||||||
| 176 | 97 | my $source = shift; | |||||||||||||||||||||
| 177 | |||||||||||||||||||||||
| 178 | 97 | ▶ | return $CopiedItems{ $source } if ( exists $CopiedItems{ $source } ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 179 | |||||||||||||||||||||||
| 180 | 96 | ▶ | if ( my $placeholder = $num_refs{ $source } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 181 | 26 | return __get_parameter( $placeholder ); | |||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||
| 183 | |||||||||||||||||||||||
| 184 | 70 | my $ref_type = ref $source; | |||||||||||||||||||||
| 185 | 70 | ▶ | if (! $ref_type) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 186 | 44 | $source =~ s/($num_refs)/ __get_parameter( $num_refs{ $1 } ) /geo; | |||||||||||||||||||||
| 187 | 44 | return $source; | |||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||
| 189 | |||||||||||||||||||||||
| 190 | 26 | my $class_name; | |||||||||||||||||||||
| 191 | 26 | ▶ | if ( "$source" =~ /^\Q$ref_type\E\=([A-Z]+)\(0x[0-9a-f]+\)$/ ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 192 | 1 | $class_name = $ref_type; | |||||||||||||||||||||
| 193 | 1 | $ref_type = $1; | |||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||
| 195 | |||||||||||||||||||||||
| 196 | 26 | my $copy; | |||||||||||||||||||||
| 197 | 26 | ▶ | if ($ref_type eq 'SCALAR') { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 198 | 2 | $CopiedItems{ $source } = $copy = \( my $var = "" );; | |||||||||||||||||||||
| 199 | 2 | $$copy = __clone_with_parameters($$source); | |||||||||||||||||||||
| 200 | } elsif ($ref_type eq 'REF') { | ||||||||||||||||||||||
| 201 | 1 | $CopiedItems{ $source } = $copy = \( my $var = "" );; | |||||||||||||||||||||
| 202 | 1 | $$copy = __clone_with_parameters($$source); | |||||||||||||||||||||
| 203 | } elsif ($ref_type eq 'HASH') { | ||||||||||||||||||||||
| 204 | 12 | $CopiedItems{ $source } = $copy = {}; | |||||||||||||||||||||
| 205 | 12 | %$copy = map { __clone_with_parameters($_) } %$source; | |||||||||||||||||||||
| 206 | } elsif ($ref_type eq 'ARRAY') { | ||||||||||||||||||||||
| 207 | 10 | $CopiedItems{ $source } = $copy = []; | |||||||||||||||||||||
| 208 | 10 | @$copy = map { __clone_with_parameters($_) } @$source; | |||||||||||||||||||||
| 209 | } else { | ||||||||||||||||||||||
| 210 | 1 | $copy = $source; | |||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||
| 212 | |||||||||||||||||||||||
| 213 | 26 | ▶ | bless $copy, $class_name if $class_name; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 214 | |||||||||||||||||||||||
| 215 | 26 | return $copy; | |||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||
| 217 | |||||||||||||||||||||||
| 218 | ######################################################################## | ||||||||||||||||||||||
| 219 | |||||||||||||||||||||||
| 220 | =head2 safe_eval_with_parameters | ||||||||||||||||||||||
| 221 | |||||||||||||||||||||||
| 222 | @results = safe_eval_with_parameters( $perl_code_string ); | ||||||||||||||||||||||
| 223 | |||||||||||||||||||||||
| 224 | Uses the Safe package to eval the provided code string. Uses a compartment which shares the same numeric variables, so that values evaluated this way can then be cloned with clone_with_parameters. | ||||||||||||||||||||||
| 225 | |||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||
| 227 | |||||||||||||||||||||||
| 228 | 13 | my $safe_compartment; | |||||||||||||||||||||
| 229 | 3 | ▶ | sub safe_eval_with_parameters { | ||||||||||||||||||||
|
Subroutine
safe_eval_with_parameters: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 230 | 3 | ▶ | $safe_compartment or $safe_compartment = do { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 231 | 1 | require Safe; | |||||||||||||||||||||
| 232 | 1 | my $compartment = Safe->new(); | |||||||||||||||||||||
| 233 | 1 | $compartment->share_from( 'main', [ map { '$' . $_ } ( 1 .. 9 ) ] ); | |||||||||||||||||||||
| 234 | 1 | $compartment; | |||||||||||||||||||||
| 235 | }; | ||||||||||||||||||||||
| 236 | |||||||||||||||||||||||
| 237 | 3 | $safe_compartment->reval( shift ); | |||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||
| 239 | |||||||||||||||||||||||
| 240 | ######################################################################## | ||||||||||||||||||||||
| 241 | |||||||||||||||||||||||
| 242 | =head1 SEE ALSO | ||||||||||||||||||||||
| 243 | |||||||||||||||||||||||
| 244 | See L<DBIx::SQLEngine> for the overall interface and developer documentation. | ||||||||||||||||||||||
| 245 | |||||||||||||||||||||||
| 246 | See L<DBIx::SQLEngine::Docs::ReadMe> for general information about | ||||||||||||||||||||||
| 247 | this distribution, including installation and license information. | ||||||||||||||||||||||
| 248 | |||||||||||||||||||||||
| 249 | =cut | ||||||||||||||||||||||
| 250 | |||||||||||||||||||||||
| 251 | ######################################################################## | ||||||||||||||||||||||
| 252 | |||||||||||||||||||||||
| 253 | 13 | 1; | |||||||||||||||||||||