Commit | Line | Data |
e7724627 |
1 | package Moose::Meta::Method::Accessor::Native::String::replace; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
88e88a7b |
6 | use Moose::Util (); |
7 | use Params::Util (); |
8 | |
245478d5 |
9 | our $VERSION = '1.19'; |
e7724627 |
10 | $VERSION = eval $VERSION; |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
8b9641b8 |
13 | use Moose::Role; |
14 | |
15 | with 'Moose::Meta::Method::Accessor::Native::Writer' => { |
16 | -excludes => [ |
17 | qw( |
18 | _minimum_arguments |
19 | _maximum_arguments |
20 | _inline_check_arguments |
53a4677c |
21 | _optimized_set_new_value |
8b9641b8 |
22 | ) |
23 | ] |
7f5ec80d |
24 | }; |
e7724627 |
25 | |
26 | sub _minimum_arguments { 1 } |
8b9641b8 |
27 | |
e7724627 |
28 | sub _maximum_arguments { 2 } |
29 | |
30 | sub _inline_check_arguments { |
31 | my $self = shift; |
32 | |
53a4677c |
33 | return ( |
34 | 'if (!Moose::Util::_STRINGLIKE0($_[0]) && !Params::Util::_REGEX($_[0])) {', |
35 | $self->_inline_throw_error( |
36 | '"The first argument passed to replace must be a string or ' |
37 | . 'regexp reference"' |
38 | ) . ';', |
39 | '}', |
40 | 'if (!Moose::Util::_STRINGLIKE0($_[1]) && !Params::Util::_CODELIKE($_[1])) {', |
41 | $self->_inline_throw_error( |
42 | '"The second argument passed to replace must be a string or ' |
43 | . 'code reference"' |
44 | ) . ';', |
45 | '}', |
46 | ); |
e7724627 |
47 | } |
48 | |
49 | sub _potential_value { |
53a4677c |
50 | my $self = shift; |
51 | my ($slot_access) = @_; |
e7724627 |
52 | |
53a4677c |
53 | return '(do { ' |
54 | . 'my $val = ' . $slot_access . '; ' |
55 | . 'ref $_[1] ' |
56 | . '? $val =~ s/$_[0]/$_[1]->()/e ' |
57 | . ': $val =~ s/$_[0]/$_[1]/; ' |
58 | . '$val; ' |
59 | . '})'; |
e7724627 |
60 | } |
61 | |
53a4677c |
62 | sub _optimized_set_new_value { |
63 | my $self = shift; |
64 | my ($inv, $new, $slot_access) = @_; |
e7724627 |
65 | |
53a4677c |
66 | return '(do { ' |
67 | . 'ref $_[1] ' |
68 | . '? ' . $slot_access . ' =~ s/$_[0]/$_[1]->()/e ' |
69 | . ': ' . $slot_access . ' =~ s/$_[0]/$_[1]/; ' |
70 | . '})'; |
e7724627 |
71 | } |
72 | |
8b9641b8 |
73 | no Moose::Role; |
74 | |
e7724627 |
75 | 1; |