File Coverage

blib/lib/PPIx/QuoteLike/Token/Interpolation.pm
Criterion Covered Total %
statement 48 48 100.0
branch 12 14 85.7
condition 5 15 33.3
subroutine 11 11 100.0
pod 2 2 100.0
total 78 90 86.6


line stmt bran cond sub pod time code
1             package PPIx::QuoteLike::Token::Interpolation;
2              
3 7     7   1173 use 5.006;
  7         24  
4              
5 7     7   30 use strict;
  7         10  
  7         161  
6 7     7   24 use warnings;
  7         11  
  7         247  
7              
8 7     7   28 use Carp;
  7         10  
  7         336  
9 7     7   1497 use PPI::Document;
  7         500138  
  7         295  
10 7         761 use PPIx::QuoteLike::Constant qw{
11             LOCATION_COLUMN
12             LOCATION_LOGICAL_LINE
13             LOCATION_LOGICAL_FILE
14             @CARP_NOT
15 7     7   37 };
  7         13  
16 7         335 use PPIx::QuoteLike::Utils qw{
17             __normalize_interpolation_for_ppi
18             __variables
19 7     7   34 };
  7         10  
20              
21 7     7   31 use base qw{ PPIx::QuoteLike::Token };
  7         11  
  7         3742  
22              
23             our $VERSION = '0.024';
24              
25             sub ppi {
26 57     57 1 87 my ( $self ) = @_;
27 57 100       211 unless ( $self->{ppi} ) {
28 34         56 my $content;
29 34         58 my $location = $self->{location};
30 34 100       80 if ( $location ) {
31 13         16 my $fn;
32 13 100       31 if( defined( $fn = $location->[LOCATION_LOGICAL_FILE] ) ) {
33 1         3 $fn =~ s/ (?= [\\"] ) /\\/smxg;
34 1         3 $content = qq{#line $location->[LOCATION_LOGICAL_LINE] "$fn"\n};
35             } else {
36 12         37 $content = qq{#line $location->[LOCATION_LOGICAL_LINE]\n};
37             }
38 13         41 $content .= ' ' x ( $location->[LOCATION_COLUMN] - 1 );
39             }
40              
41 34         98 $content .= __normalize_interpolation_for_ppi( $self->content() );
42              
43 34         183 $self->{ppi} = PPI::Document->new( \$content );
44              
45 34 100       32567 if ( $location ) {
46             # Generate locations now.
47 13         44 $self->{ppi}->location();
48             # Remove the stuff we originally injected. NOTE that we can
49             # only get away with doing this if the removal does not
50             # invalidate the locations of the other tokens that we just
51             # generated.
52 13         2288 my $elem;
53             # Remove the '#line' directive if we find it
54 13 50 33     58 $elem = $self->{ppi}->child( 0 )
      33        
55             and $elem->isa( 'PPI::Token::Comment' )
56             and $elem->content() =~ m/ \A \#line\b /smx
57             and $elem->remove();
58             # Remove the white space if we find it, and if it in fact
59             # represents only the white space we injected to get the
60             # column numbers right.
61 13         642 my $wid = $location->[LOCATION_COLUMN] - 1;
62             $wid
63 13 50 33     41 and $elem = $self->{ppi}->child( 0 )
      33        
      33        
64             and $elem->isa( 'PPI::Token::Whitespace' )
65             and $wid == length $elem->content()
66             and $elem->remove();
67             }
68             }
69 57         706 return $self->{ppi};
70             }
71              
72             sub variables {
73 56     56 1 102 my ( $self ) = @_;
74 56         137 return __variables( $self->ppi() );
75             }
76              
77             sub __perl_version_introduced {
78 16     16   21 my ( $self ) = @_;
79             $self->{postderef}
80 16 100       34 and return '5.019005';
81 9         14 return;
82             }
83              
84             1;
85              
86             __END__