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   936 use 5.006;
  7         24  
4              
5 7     7   32 use strict;
  7         10  
  7         127  
6 7     7   30 use warnings;
  7         12  
  7         164  
7              
8 7     7   29 use Carp;
  7         12  
  7         352  
9 7     7   1185 use PPI::Document;
  7         283437  
  7         227  
10 7         681 use PPIx::QuoteLike::Constant qw{
11             LOCATION_COLUMN
12             LOCATION_LOGICAL_LINE
13             LOCATION_LOGICAL_FILE
14             @CARP_NOT
15 7     7   44 };
  7         14  
16 7         304 use PPIx::QuoteLike::Utils qw{
17             __normalize_interpolation_for_ppi
18             __variables
19 7     7   45 };
  7         13  
20              
21 7     7   34 use base qw{ PPIx::QuoteLike::Token };
  7         13  
  7         2825  
22              
23             our $VERSION = '0.022_01';
24              
25             sub ppi {
26 57     57 1 125 my ( $self ) = @_;
27 57 100       247 unless ( $self->{ppi} ) {
28 34         63 my $content;
29 34         73 my $location = $self->{location};
30 34 100       101 if ( $location ) {
31 13         20 my $fn;
32 13 100       35 if( defined( $fn = $location->[LOCATION_LOGICAL_FILE] ) ) {
33 1         3 $fn =~ s/ (?= [\\"] ) /\\/smxg;
34 1         4 $content = qq{#line $location->[LOCATION_LOGICAL_LINE] "$fn"\n};
35             } else {
36 12         34 $content = qq{#line $location->[LOCATION_LOGICAL_LINE]\n};
37             }
38 13         48 $content .= ' ' x ( $location->[LOCATION_COLUMN] - 1 );
39             }
40              
41 34         106 $content .= __normalize_interpolation_for_ppi( $self->content() );
42              
43 34         212 $self->{ppi} = PPI::Document->new( \$content );
44              
45 34 100       34274 if ( $location ) {
46             # Generate locations now.
47 13         62 $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         2470 my $elem;
53             # Remove the '#line' directive if we find it
54 13 50 33     51 $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         741 my $wid = $location->[LOCATION_COLUMN] - 1;
62             $wid
63 13 50 33     61 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         807 return $self->{ppi};
70             }
71              
72             sub variables {
73 56     56 1 141 my ( $self ) = @_;
74 56         195 return __variables( $self->ppi() );
75             }
76              
77             sub __perl_version_introduced {
78 16     16   23 my ( $self ) = @_;
79             $self->{postderef}
80 16 100       50 and return '5.019005';
81 9         46 return;
82             }
83              
84             1;
85              
86             __END__