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