stop using excludes within moose, since it's no longer necessary
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / String / substr.pm
CommitLineData
e7724627 1package Moose::Meta::Method::Accessor::Native::String::substr;
2
3use strict;
4use warnings;
5
88e88a7b 6use Moose::Util ();
7
8b9641b8 8use Moose::Role;
9
00bbc132 10with 'Moose::Meta::Method::Accessor::Native::Reader',
11 'Moose::Meta::Method::Accessor::Native::Writer';
e7724627 12
13sub _generate_method {
14 my $self = shift;
15
53a4677c 16 my $inv = '$self';
1e2c801e 17 my $slot_access = $self->_get_value($inv);
e7724627 18
53a4677c 19 return (
20 'sub {',
53a4677c 21 'my ' . $inv . ' = shift;',
22 $self->_inline_curried_arguments,
23 'if (@_ == 1 || @_ == 2) {',
1e2c801e 24 $self->_inline_reader_core($inv, $slot_access),
53a4677c 25 '}',
26 'elsif (@_ == 3) {',
1e2c801e 27 $self->_inline_writer_core($inv, $slot_access),
53a4677c 28 '}',
29 'else {',
30 $self->_inline_check_argument_count,
31 '}',
32 '}',
33 );
e7724627 34}
35
1e2c801e 36sub _minimum_arguments { 1 }
37sub _maximum_arguments { 3 }
e7724627 38
39sub _inline_process_arguments {
53a4677c 40 my $self = shift;
41 my ($inv, $slot_access) = @_;
e7724627 42
53a4677c 43 return (
44 'my $offset = shift;',
45 'my $length = @_ ? shift : length ' . $slot_access . ';',
46 'my $replacement = shift;',
47 );
e7724627 48}
49
50sub _inline_check_arguments {
53a4677c 51 my $self = shift;
52 my ($for_writer) = @_;
53
54 my @code = (
55 'if ($offset !~ /^-?\d+$/) {',
56 $self->_inline_throw_error(
57 '"The first argument passed to substr must be an integer"'
58 ) . ';',
59 '}',
60 'if ($length !~ /^-?\d+$/) {',
61 $self->_inline_throw_error(
62 '"The second argument passed to substr must be an integer"'
63 ) . ';',
64 '}',
65 );
e7724627 66
67 if ($for_writer) {
53a4677c 68 push @code, (
69 'if (!Moose::Util::_STRINGLIKE0($replacement)) {',
70 $self->_inline_throw_error(
71 '"The third argument passed to substr must be a string"'
72 ) . ';',
73 '}',
74 );
e7724627 75 }
76
53a4677c 77 return @code;
e7724627 78}
79
80sub _potential_value {
53a4677c 81 my $self = shift;
82 my ($slot_access) = @_;
e7724627 83
53a4677c 84 return '(do { '
85 . 'my $potential = ' . $slot_access . '; '
86 . '@return = substr $potential, $offset, $length, $replacement; '
87 . '$potential; '
88 . '})';
e7724627 89}
90
a486d5ad 91sub _inline_optimized_set_new_value {
53a4677c 92 my $self = shift;
93 my ($inv, $new, $slot_access) = @_;
e7724627 94
53a4677c 95 return '@return = substr ' . $slot_access . ', '
a486d5ad 96 . '$offset, $length, $replacement;';
e7724627 97}
98
99sub _return_value {
53a4677c 100 my $self = shift;
101 my ($slot_access, $for_writer) = @_;
e7724627 102
7f5ec80d 103 return '$return[0]' if $for_writer;
e7724627 104
53a4677c 105 return 'substr ' . $slot_access . ', $offset, $length';
e7724627 106}
107
8b9641b8 108no Moose::Role;
109
e7724627 1101;