File Coverage

blib/lib/Data/Unixish/splice.pm
Criterion Covered Total %
statement 33 33 100.0
branch 12 12 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 53 53 100.0


line stmt bran cond sub pod time code
1             package Data::Unixish::splice;
2              
3             our $DATE = '2019-10-26'; # DATE
4             our $DIST = 'Data-Unixish'; # DIST
5             our $VERSION = '1.572'; # VERSION
6              
7 1     1   491 use 5.010001;
  1         6  
8 1     1   4 use strict;
  1         1  
  1         18  
9 1     1   339 use syntax 'each_on_array'; # to support perl < 5.12
  1         19357  
  1         3  
10 1     1   2755 use warnings;
  1         2  
  1         21  
11             #use Log::Any '$log';
12              
13 1     1   338 use Data::Unixish::Util qw(%common_args);
  1         2  
  1         313  
14              
15             our %SPEC;
16              
17             $SPEC{splice} = {
18             v => 1.1,
19             summary => 'Perform Perl splice() on array',
20             description => <<'_',
21              
22             _
23             args => {
24             %common_args,
25             offset => {
26             schema => 'uint*',
27             req => 1,
28             pos => 0,
29             },
30             length => {
31             schema => 'uint*',
32             pos => 1,
33             },
34             list => {
35             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
36             pos => 2,
37             slurpy => 1,
38             },
39             },
40             tags => [qw/datatype-in:array itemfunc/],
41             };
42             sub splice {
43 3     3 1 14 my %args = @_;
44 3         6 my ($in, $out) = ($args{in}, $args{out});
45              
46 3         10 while (my ($index, $item) = each @$in) {
47 9 100       24 my @ary = ref $item eq 'ARRAY' ? @$item : ($item);
48 9 100       34 if (defined $args{list}) {
    100          
49 3         38 CORE::splice(@ary, $args{offset}, $args{length}, @{ $args{list} });
  3         10  
50             } elsif (defined $args{length}) {
51 3         4 CORE::splice(@ary, $args{offset}, $args{length});
52             } else {
53 3         4 CORE::splice(@ary, $args{offset});
54             }
55 9         26 push @$out, \@ary;
56             }
57              
58 3         11 [200, "OK"];
59             }
60              
61             sub _splice_item {
62 9     9   12 my ($item, $args) = @_;
63              
64 9 100       26 my @ary = ref $item eq 'ARRAY' ? @$item : ($item);
65 9 100       17 if (defined $args->{list}) {
    100          
66 3         4 CORE::splice(@ary, $args->{offset}, $args->{length}, @{ $args->{list} });
  3         6  
67             } elsif (defined $args->{length}) {
68 3         5 CORE::splice(@ary, $args->{offset}, $args->{length});
69             } else {
70 3         4 CORE::splice(@ary, $args->{offset});
71             }
72 9         20 \@ary;
73             }
74              
75             1;
76             # ABSTRACT: Perform Perl splice() on array
77              
78             __END__