| 1 | package Catmandu::Fix::copy_field; | ||
| 2 | |||
| 3 | 5 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 5 | our $VERSION = '1.0201_02'; | |
| 6 | |||
| 7 | 5 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 5 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 5 | ▶ | use Catmandu::Fix::Has; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 5 | with 'Catmandu::Fix::Base'; | |
| 12 | |||
| 13 | 5 | has old_path => (fix_arg => 1); | |
| 14 | 5 | has new_path => (fix_arg => 1); | |
| 15 | |||
| 16 | sub emit { | ||
| 17 | 29 | ▶ | my ($self, $fixer) = @_; |
|
Subroutine
emit: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 18 | 29 | my $old_path = $fixer->split_path($self->old_path); | |
| 19 | 29 | my $old_key = pop @$old_path; | |
| 20 | 29 | my $new_path = $fixer->split_path($self->new_path); | |
| 21 | |||
| 22 | 29 | my $vals = $fixer->generate_var; | |
| 23 | 29 | my $current_val = $fixer->generate_var; | |
| 24 | 29 | my $perl = ""; | |
| 25 | 29 | $perl .= $fixer->emit_declare_vars($vals, '[]'); | |
| 26 | 29 | $perl .= $fixer->emit_declare_vars($current_val); | |
| 27 | |||
| 28 | $perl .= $fixer->emit_walk_path( | ||
| 29 | $fixer->var, | ||
| 30 | $old_path, | ||
| 31 | sub { | ||
| 32 | 29 | ▶ | my $var = shift; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 33 | $fixer->emit_get_key( | ||
| 34 | $var, $old_key, | ||
| 35 | sub { | ||
| 36 | 31 | ▶ | my $var = shift; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 37 | 31 | "push(\@{${vals}}, ${var});"; | |
| 38 | } | ||
| 39 | 29 | ); | |
| 40 | } | ||
| 41 | 29 | ); | |
| 42 | |||
| 43 | $perl | ||
| 44 | .= "while (\@{${vals}}) {" | ||
| 45 | . "${current_val} = clone(shift(\@{${vals}}));" | ||
| 46 | . $fixer->emit_create_path( | ||
| 47 | $fixer->var, | ||
| 48 | $new_path, | ||
| 49 | sub { | ||
| 50 | 32 | ▶ | my $var = shift; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 51 | 32 | "${var} = ${current_val};"; | |
| 52 | } | ||
| 53 | 29 | ) . "}"; | |
| 54 | |||
| 55 | 29 | $perl; | |
| 56 | } | ||
| 57 | |||
| 58 | 5 | 1; | |
| 59 | |||
| 60 | __END__ | ||
| 61 | |||
| 62 | =pod | ||
| 63 | |||
| 64 | =head1 NAME | ||
| 65 | |||
| 66 | Catmandu::Fix::copy_field - copy the value of one field to a new field | ||
| 67 | |||
| 68 | =head1 SYNOPSIS | ||
| 69 | |||
| 70 | # Copy the values of foo.bar into bar.foo | ||
| 71 | copy_field(foo.bar, bar.foo) | ||
| 72 | |||
| 73 | =head1 SEE ALSO | ||
| 74 | |||
| 75 | L<Catmandu::Fix> | ||
| 76 | |||
| 77 | =cut | ||