| 1 | package Data::Unixish::splice; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use syntax 'each_on_array'; # to support perl < 5.12 | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | #use Log::Any '$log'; | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | ▶ | use Data::Unixish::Util qw(%common_args); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||
| 12 | 2 | our $DATE = '2025-02-24'; # DATE | |||||||||||||
| 13 | 2 | our $DIST = 'Data-Unixish'; # DIST | |||||||||||||
| 14 | 2 | our $VERSION = '1.574'; # VERSION | |||||||||||||
| 15 | |||||||||||||||
| 16 | 2 | our %SPEC; | |||||||||||||
| 17 | |||||||||||||||
| 18 | $SPEC{splice} = { | ||||||||||||||
| 19 | 2 | v => 1.1, | |||||||||||||
| 20 | summary => 'Perform Perl splice() on array', | ||||||||||||||
| 21 | description => <<'MARKDOWN', | ||||||||||||||
| 22 | |||||||||||||||
| 23 | MARKDOWN | ||||||||||||||
| 24 | args => { | ||||||||||||||
| 25 | %common_args, | ||||||||||||||
| 26 | offset => { | ||||||||||||||
| 27 | schema => 'uint*', | ||||||||||||||
| 28 | req => 1, | ||||||||||||||
| 29 | pos => 0, | ||||||||||||||
| 30 | }, | ||||||||||||||
| 31 | length => { | ||||||||||||||
| 32 | schema => 'uint*', | ||||||||||||||
| 33 | pos => 1, | ||||||||||||||
| 34 | }, | ||||||||||||||
| 35 | list => { | ||||||||||||||
| 36 | schema => ['array*', of=>'str*'], # actually it does not have to be array of str, we just want ease of specifying on the cmdline for now | ||||||||||||||
| 37 | pos => 2, | ||||||||||||||
| 38 | slurpy => 1, | ||||||||||||||
| 39 | }, | ||||||||||||||
| 40 | }, | ||||||||||||||
| 41 | tags => [qw/datatype-in:array itemfunc/], | ||||||||||||||
| 42 | }; | ||||||||||||||
| 43 | 3 | ▶ | sub splice { | ||||||||||||
|
Subroutine
splice: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 44 | 3 | my %args = @_; | |||||||||||||
| 45 | 3 | my ($in, $out) = ($args{in}, $args{out}); | |||||||||||||
| 46 | |||||||||||||||
| 47 | 3 | ▶ | while (my ($index, $item) = each @$in) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 9 | ▶ | my @ary = ref $item eq 'ARRAY' ? @$item : ($item); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 9 | ▶ | if (defined $args{list}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 50 | 3 | CORE::splice(@ary, $args{offset}, $args{length}, @{ $args{list} }); | |||||||||||||
| 51 | } elsif (defined $args{length}) { | ||||||||||||||
| 52 | 3 | CORE::splice(@ary, $args{offset}, $args{length}); | |||||||||||||
| 53 | } else { | ||||||||||||||
| 54 | 3 | CORE::splice(@ary, $args{offset}); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | 9 | push @$out, \@ary; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 3 | [200, "OK"]; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 9 | ▶ | sub _splice_item { | ||||||||||||
|
Subroutine
_splice_item: called CC=4 SCAR=13.9
| |||||||||||||||
| 63 | 9 | my ($item, $args) = @_; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 9 | ▶ | my @ary = ref $item eq 'ARRAY' ? @$item : ($item); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 9 | ▶ | if (defined $args->{list}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 67 | 3 | CORE::splice(@ary, $args->{offset}, $args->{length}, @{ $args->{list} }); | |||||||||||||
| 68 | } elsif (defined $args->{length}) { | ||||||||||||||
| 69 | 3 | CORE::splice(@ary, $args->{offset}, $args->{length}); | |||||||||||||
| 70 | } else { | ||||||||||||||
| 71 | 3 | CORE::splice(@ary, $args->{offset}); | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | 9 | \@ary; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 2 | 1; | |||||||||||||
| 77 | # ABSTRACT: Perform Perl splice() on array | ||||||||||||||
| 78 | |||||||||||||||
| 79 | __END__ | ||||||||||||||