make github the primary repository
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / String / replace.pm
CommitLineData
e7724627 1package Moose::Meta::Method::Accessor::Native::String::replace;
2
3use strict;
4use warnings;
5
88e88a7b 6use Moose::Util ();
7use Params::Util ();
8
8b9641b8 9use Moose::Role;
10
00bbc132 11with 'Moose::Meta::Method::Accessor::Native::Writer';
e7724627 12
13sub _minimum_arguments { 1 }
8b9641b8 14
e7724627 15sub _maximum_arguments { 2 }
16
17sub _inline_check_arguments {
18 my $self = shift;
19
53a4677c 20 return (
21 'if (!Moose::Util::_STRINGLIKE0($_[0]) && !Params::Util::_REGEX($_[0])) {',
22 $self->_inline_throw_error(
23 '"The first argument passed to replace must be a string or '
24 . 'regexp reference"'
25 ) . ';',
26 '}',
27 'if (!Moose::Util::_STRINGLIKE0($_[1]) && !Params::Util::_CODELIKE($_[1])) {',
28 $self->_inline_throw_error(
29 '"The second argument passed to replace must be a string or '
30 . 'code reference"'
31 ) . ';',
32 '}',
33 );
e7724627 34}
35
36sub _potential_value {
53a4677c 37 my $self = shift;
38 my ($slot_access) = @_;
e7724627 39
53a4677c 40 return '(do { '
41 . 'my $val = ' . $slot_access . '; '
42 . 'ref $_[1] '
43 . '? $val =~ s/$_[0]/$_[1]->()/e '
44 . ': $val =~ s/$_[0]/$_[1]/; '
45 . '$val; '
46 . '})';
e7724627 47}
48
a486d5ad 49sub _inline_optimized_set_new_value {
53a4677c 50 my $self = shift;
51 my ($inv, $new, $slot_access) = @_;
e7724627 52
a486d5ad 53 return (
54 'ref $_[1]',
55 '? ' . $slot_access . ' =~ s/$_[0]/$_[1]->()/e',
56 ': ' . $slot_access . ' =~ s/$_[0]/$_[1]/;',
57 );
e7724627 58}
59
8b9641b8 60no Moose::Role;
61
e7724627 621;