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   886 use 5.006;
  7         24  
4              
5 7     7   34 use strict;
  7         14  
  7         124  
6 7     7   27 use warnings;
  7         12  
  7         151  
7              
8 7     7   29 use Carp;
  7         13  
  7         360  
9 7     7   1225 use PPI::Document;
  7         288110  
  7         222  
10 7         661 use PPIx::QuoteLike::Constant qw{
11             LOCATION_COLUMN
12             LOCATION_LOGICAL_LINE
13             LOCATION_LOGICAL_FILE
14             @CARP_NOT
15 7     7   42 };
  7         15  
16 7         296 use PPIx::QuoteLike::Utils qw{
17             __normalize_interpolation_for_ppi
18             __variables
19 7     7   40 };
  7         14  
20              
21 7     7   34 use base qw{ PPIx::QuoteLike::Token };
  7         13  
  7         2810  
22              
23             our $VERSION = '0.022';
24              
25             sub ppi {
26 55     55 1 87 my ( $self ) = @_;
27 55 100       138 unless ( $self->{ppi} ) {
28 32         48 my $content;
29 32         47 my $location = $self->{location};
30 32 100       61 if ( $location ) {
31 11         14 my $fn;
32 11 100       27 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 10         25 $content = qq{#line $location->[LOCATION_LOGICAL_LINE]\n};
37             }
38 11         35 $content .= ' ' x ( $location->[LOCATION_COLUMN] - 1 );
39             }
40              
41 32         77 $content .= __normalize_interpolation_for_ppi( $self->content() );
42              
43 32         136 $self->{ppi} = PPI::Document->new( \$content );
44              
45 32 100       26544 if ( $location ) {
46             # Generate locations now.
47 11         51 $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 11         1844 my $elem;
53             # Remove the '#line' directive if we find it
54 11 50 33     43 $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 11         637 my $wid = $location->[LOCATION_COLUMN] - 1;
62             $wid
63 11 50 33     44 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 55         648 return $self->{ppi};
70             }
71              
72             sub variables {
73 54     54 1 96 my ( $self ) = @_;
74 54         94 return __variables( $self->ppi() );
75             }
76              
77             sub __perl_version_introduced {
78 16     16   31 my ( $self ) = @_;
79             $self->{postderef}
80 16 100       60 and return '5.019005';
81 9         17 return;
82             }
83              
84             1;
85              
86             __END__