| 1 | package TPath::Concatenation; | ||
| 2 | $TPath::Concatenation::VERSION = '1.007'; | ||
| 3 | # ABSTRACT: handles the string concatenation in C<//@foo[1 ~ @bar ~ "quux"]> | ||
| 4 | |||
| 5 | |||
| 6 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | no if $] >= 5.018, warnings => "experimental"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use TPath::TypeConstraints; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use namespace::autoclean; | ||
| 12 | |||
| 13 | |||
| 14 | with 'TPath::Stringifiable'; | ||
| 15 | |||
| 16 | |||
| 17 | has args => ( is => 'ro', isa => 'ArrayRef[ConcatArg]' ); | ||
| 18 | |||
| 19 | has _arg_subs => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => 'ArrayRef[CodeRef]', | ||
| 22 | lazy => 1, | ||
| 23 | builder => '_build_arg_subs' | ||
| 24 | ); | ||
| 25 | |||
| 26 | sub concatenate { | ||
| 27 | my ( $self, $ctx ) = @_; | ||
| 28 | my $s = ''; | ||
| 29 | $s .= $_->($ctx) for @{ $self->_arg_subs }; | ||
| 30 | return $s; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _build_arg_subs { | ||
| 34 | my $self = shift; | ||
| 35 | my @codes; | ||
| 36 | for my $arg ( @{ $self->args } ) { | ||
| 37 | my $sub; | ||
| 38 | for ($arg) { | ||
| 39 | when ( !blessed $_ ) { | ||
| 40 | $sub = sub { $arg } | ||
| 41 | } | ||
| 42 | when ( $_->isa('TPath::Attribute') ) { | ||
| 43 | $sub = sub { $arg->apply(shift) } | ||
| 44 | } | ||
| 45 | when ( $_->isa('TPath::Math') ) { | ||
| 46 | $sub = sub { $arg->to_num(shift) } | ||
| 47 | } | ||
| 48 | when ( $_->isa('TPath::Expression') ) { | ||
| 49 | $sub = sub { join '', @{ $arg->_select( shift, 1 ) } } | ||
| 50 | } | ||
| 51 | default { die 'unexpected concatenation argument type: ' . ref $_ } | ||
| 52 | } | ||
| 53 | push @codes, $sub; | ||
| 54 | } | ||
| 55 | return \@codes; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub to_string { | ||
| 59 | my $self = shift; | ||
| 60 | my $s = ''; | ||
| 61 | my $non_first = 0; | ||
| 62 | for my $arg ( @{ $self->args } ) { | ||
| 63 | $s .= ' ~ ' if $non_first++; | ||
| 64 | $s .= ref $arg ? $arg->to_string : $self->_stringify($arg); | ||
| 65 | } | ||
| 66 | return $s; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ | ||