File Coverage

blib/lib/Perl/ToPerl6/Transformer/Variables/ReplaceNegativeIndex.pm
Criterion Covered Total %
statement 31 34 91.1
branch 3 8 37.5
condition 6 12 50.0
subroutine 14 14 100.0
pod 3 6 50.0
total 57 74 77.0


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