Commit | Line | Data |
a7821be5 |
1 | package Moose::Meta::Method::Accessor::Native::Array::splice; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8b9641b8 |
6 | use Moose::Role; |
7 | |
00bbc132 |
8 | with 'Moose::Meta::Method::Accessor::Native::Array::Writer'; |
a7821be5 |
9 | |
10 | sub _minimum_arguments { 1 } |
11 | |
12 | sub _adds_members { 1 } |
13 | |
14 | sub _inline_process_arguments { |
53a4677c |
15 | return ( |
16 | 'my $idx = shift;', |
17 | 'my $len = @_ ? shift : undef;', |
18 | ); |
a7821be5 |
19 | } |
20 | |
21 | sub _inline_check_arguments { |
22 | my $self = shift; |
23 | |
53a4677c |
24 | return ( |
25 | $self->_inline_check_var_is_valid_index('$idx'), |
26 | 'if (defined($len) && $len !~ /^-?\d+$/) {', |
27 | $self->_inline_throw_error( |
28 | '"The length argument passed to splice must be an integer"', |
29 | ) . ';', |
30 | '}', |
31 | ); |
a7821be5 |
32 | } |
33 | |
34 | sub _potential_value { |
53a4677c |
35 | my $self = shift; |
36 | my ($slot_access) = @_; |
37 | |
38 | return '(do { ' |
39 | . 'my @potential = @{ (' . $slot_access . ') }; ' |
40 | . '@return = defined $len ' |
41 | . '? (splice @potential, $idx, $len, @_) ' |
42 | . ': (splice @potential, $idx); ' |
43 | . '\@potential;' |
44 | . '})'; |
e32b7489 |
45 | } |
46 | |
a486d5ad |
47 | sub _inline_optimized_set_new_value { |
53a4677c |
48 | my $self = shift; |
49 | my ($inv, $new, $slot_access) = @_; |
e32b7489 |
50 | |
a486d5ad |
51 | return ( |
52 | '@return = defined $len', |
53 | '? (splice @{ (' . $slot_access . ') }, $idx, $len, @_)', |
54 | ': (splice @{ (' . $slot_access . ') }, $idx);', |
55 | ); |
7f5ec80d |
56 | } |
57 | |
58 | sub _return_value { |
53a4677c |
59 | my $self = shift; |
60 | my ($slot_access) = @_; |
7f5ec80d |
61 | |
53a4677c |
62 | return 'wantarray ? @return : $return[-1]'; |
a7821be5 |
63 | } |
64 | |
8b9641b8 |
65 | no Moose::Role; |
66 | |
a7821be5 |
67 | 1; |