File Coverage

blib/lib/Perl/ToPerl6/Transformer/Variables/ReplaceNegativeIndex.pm
Criterion Covered Total %
statement 23 34 67.6
branch 0 8 0.0
condition 0 12 0.0
subroutine 10 14 71.4
pod 3 6 50.0
total 36 74 48.6


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::Variables::ReplaceNegativeIndex;
2              
3 1     1   915 use 5.006001;
  1         4  
4 1     1   6 use strict;
  1         2  
  1         20  
5 1     1   6 use warnings;
  1         2  
  1         23  
6 1     1   6 use Readonly;
  1         1  
  1         44  
7              
8 1     1   5 use Perl::ToPerl6::Utils qw{ :severities };
  1         2  
  1         52  
9 1     1   113 use Perl::ToPerl6::Utils::PPI qw{ is_ppi_token_word };
  1         2  
  1         45  
10              
11 1     1   167 use base 'Perl::ToPerl6::Transformer';
  1         2  
  1         366  
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Negative array indexes now need [*-1] notation};
16             Readonly::Scalar my $EXPL => q{Negative array indexes now need [*-1] notation};
17              
18             #-----------------------------------------------------------------------------
19              
20             #
21             # That way we don't have to deal with the integer conversions.
22             #
23             sub run_before {
24 1     1 0 3 return 'BasicTypes::Integers::RewriteBinaryNumbers',
25             'BasicTypes::Integers::RewriteOctalNumbers',
26             'BasicTypes::Integers::RewriteHexNumbers'
27             }
28              
29 1     1 0 5 sub supported_parameters { return () }
30 1     1 1 5 sub default_necessity { return $NECESSITY_HIGHEST }
31 0     0 1   sub default_themes { return qw( core ) }
32             #
33             # Don't test the subscript type because the [-N] may be after a {}.
34             #
35             sub applies_to {
36             return sub {
37 0 0 0 0     $_[1]->isa('PPI::Token::Symbol') and
      0        
38             $_[1]->snext_sibling and
39             ( $_[1]->snext_sibling->isa('PPI::Structure::Subscript') or
40             $_[1]->snext_sibling->isa('PPI::Token::Operator') )
41             }
42 0     0 1   }
43              
44             #-----------------------------------------------------------------------------
45              
46             sub transform {
47 0     0 0   my ($self, $elem, $doc) = @_;
48 0           my $head = $elem;
49              
50 0           while ( $head = $head->snext_sibling ) {
51 0 0 0       next unless $head->isa('PPI::Structure::Subscript') and
52             $head->start eq '[';
53 0 0         if ( $head->schild(0)->isa('PPI::Statement::Expression') ) {
54 0 0 0       if ( $head->schild(0)->schild(0)->isa('PPI::Token::Number') and
55             $head->schild(0)->schild(0)->content =~ /^ [-] /x ) {
56             #
57             # Don't use the operator '*' lest it get confused. This way
58             # the code is still syntactically correct.
59             #
60 0           $head->schild(0)->schild(0)->insert_before(
61             PPI::Token::Word->new('*')
62             );
63             }
64             }
65             }
66              
67 0           return $self->transformation( $DESC, $EXPL, $elem );
68             }
69              
70             1;
71              
72             #-----------------------------------------------------------------------------
73              
74             __END__
75              
76             =pod
77              
78             =head1 NAME
79              
80             Perl::ToPerl6::Transformer::Variables::ReplaceNegativeIndex - Perl6 now uses [*-1] notation to represent negative indices.
81              
82              
83             =head1 AFFILIATION
84              
85             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
86             distribution.
87              
88              
89             =head1 DESCRIPTION
90              
91             Perl6 uses the new open-ended range notation C<[*-1]> to access the last element of an array:
92              
93             $x[-1] --> $x[*-1]
94             $x->[-1] --> $x->[*-1]
95              
96             Transforms variables outside of comments, heredocs, strings and POD.
97              
98             =head1 CONFIGURATION
99              
100             This Transformer is not configurable except for the standard options.
101              
102             =head1 AUTHOR
103              
104             Jeffrey Goff <drforr@pobox.com>
105              
106             =head1 COPYRIGHT
107              
108             Copyright (c) 2015 Jeffrey Goff
109              
110             This program is free software; you can redistribute it and/or modify
111             it under the same terms as Perl itself.
112              
113             =cut
114              
115             ##############################################################################
116             # Local Variables:
117             # mode: cperl
118             # cperl-indent-level: 4
119             # fill-column: 78
120             # indent-tabs-mode: nil
121             # c-indentation-style: bsd
122             # End:
123             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :