| 1 | package Catmandu::Fix::substring; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | has path => (fix_arg => 1); | |||||||||
| 12 | 1 | has args => (fix_arg => 'collect'); | |||||||||
| 13 | |||||||||||
| 14 | 1 | with 'Catmandu::Fix::SimpleGetValue'; | |||||||||
| 15 | |||||||||||
| 16 | sub emit_value { | ||||||||||
| 17 | 5 | ▶ | my ($self, $var, $fixer) = @_; | ||||||||
|
Subroutine
emit_value: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 18 | 5 | my $args = $self->args; | |||||||||
| 19 | 5 | ▶ | my $str_args = @$args > 1 ? join(", ", @$args[0, 1]) : $args->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | |||||||||||
| 21 | 5 | ▶ | if (@$args < 3) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | return | ||||||||||
| 23 | 4 | "eval { ${var} = substr(as_utf8(${var}), ${str_args}) } if is_value(${var});"; | |||||||||
| 24 | } | ||||||||||
| 25 | 1 | my $replace = $fixer->emit_string($args->[2]); | |||||||||
| 26 | 1 | "if (is_value(${var})) {" | |||||||||
| 27 | . "utf8::upgrade(${var});" | ||||||||||
| 28 | . "eval { substr(${var}, ${str_args}) = ${replace} };" . "}"; | ||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 1 | 1; | |||||||||
| 32 | |||||||||||
| 33 | __END__ | ||||||||||
| 34 | |||||||||||
| 35 | =pod | ||||||||||
| 36 | |||||||||||
| 37 | =head1 NAME | ||||||||||
| 38 | |||||||||||
| 39 | Catmandu::Fix::substring - extract a substring out of the value of a field | ||||||||||
| 40 | |||||||||||
| 41 | =head1 SYNOPSIS | ||||||||||
| 42 | |||||||||||
| 43 | # Extract a substring out of the value of a field | ||||||||||
| 44 | # - Extact from 'initials' the characters at offset 0 (first character) with a length 3 | ||||||||||
| 45 | substring(initials, 0, 3) | ||||||||||
| 46 | |||||||||||
| 47 | =head1 SEE ALSO | ||||||||||
| 48 | |||||||||||
| 49 | L<Catmandu::Fix>, substr | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||