1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
package Catmandu::Fix::substring;
use Catmandu::Sane;
our $VERSION = '1.2024';
use Catmandu::Util qw(as_utf8);
use Catmandu::Util::Path qw(as_path);
use Moo;
use namespace::clean;
use Catmandu::Fix::Has;
has path => (fix_arg => 1);
has args => (fix_arg => 'collect');
with 'Catmandu::Fix::Builder';
sub _build_fixer {
my ($self) = @_;
my $args = $self->args;
my $cb;
if (@$args == 1) {
$cb = sub {
my $val = $_[0];
my $new_val;
eval {
no warnings 'substr';
$new_val = substr(as_utf8($val), $args->[0]);
};
return $new_val if defined $new_val;
return undef, 1, 0;
};
}
elsif (@$args == 2) {
$cb = sub {
my $val = $_[0];
my $new_val;
eval {
no warnings 'substr';
$new_val = substr(as_utf8($val), $args->[0], $args->[1]);
};
return $new_val if defined $new_val;
return undef, 1, 0;
};
}
else {
$cb = sub {
my $val = $_[0];
my $new_val = as_utf8($val);
eval {
no warnings 'substr';
substr($new_val, $args->[0], $args->[1]) = $args->[2];
} // return undef, 1, 0;
$new_val;
};
}
as_path($self->path)->updater(if_value => $cb);
}
1;
__END__
=pod
=head1 NAME
Catmandu::Fix::substring - extract a substring out of the value of a field
=head1 SYNOPSIS
# Extract a substring out of the value of a field
# - Extact from 'initials' the characters at offset 0 (first character) with a length 3
substring(initials, 0, 3)
=head1 SEE ALSO
L<Catmandu::Fix>, substr
=cut
|