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 | |
d874ffad |
9 | our $VERSION = '1.20'; |
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 |
21 | _inline_optimized_set_new_value |
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 | |
33 | return $self->_inline_throw_error( |
34 | q{'The first argument passed to replace must be a string or regexp reference'} |
35 | ) |
5394a1c7 |
36 | . q{ unless Moose::Util::_STRINGLIKE0( $_[0] ) || Params::Util::_REGEX( $_[0] );} |
e7724627 |
37 | . $self->_inline_throw_error( |
38 | q{'The second argument passed to replace must be a string or code reference'} |
5394a1c7 |
39 | ) . q{ unless Moose::Util::_STRINGLIKE0( $_[1] ) || Params::Util::_CODELIKE( $_[1] );}; |
e7724627 |
40 | } |
41 | |
42 | sub _potential_value { |
43 | my ( $self, $slot_access ) = @_; |
44 | |
45 | return "( do { my \$val = $slot_access; ref \$_[1] ? \$val =~ s/\$_[0]/\$_[1]->()/e : \$val =~ s/\$_[0]/\$_[1]/; \$val } )"; |
46 | } |
47 | |
e32b7489 |
48 | sub _inline_optimized_set_new_value { |
49 | my ( $self, $inv, $new, $slot_access ) = @_; |
e7724627 |
50 | |
51 | return "if ( ref \$_[1] ) { $slot_access =~ s/\$_[0]/\$_[1]->()/e; } else { $slot_access =~ s/\$_[0]/\$_[1]/; }"; |
52 | } |
53 | |
8b9641b8 |
54 | no Moose::Role; |
55 | |
e7724627 |
56 | 1; |