File Coverage

blib/lib/Perl/ToPerl6/Transformer/Variables/RewriteSigils.pm
Criterion Covered Total %
statement 19 43 44.1
branch 0 16 0.0
condition n/a
subroutine 8 11 72.7
pod 3 5 60.0
total 30 75 40.0


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::Variables::RewriteSigils;
2              
3 1     1   700 use 5.006001;
  1         3  
4 1     1   5 use strict;
  1         1  
  1         19  
5 1     1   4 use warnings;
  1         2  
  1         20  
6 1     1   6 use Readonly;
  1         1  
  1         42  
7              
8 1     1   5 use Perl::ToPerl6::Utils qw{ :severities };
  1         2  
  1         44  
9              
10 1     1   107 use base 'Perl::ToPerl6::Transformer';
  1         2  
  1         373  
11              
12             #-----------------------------------------------------------------------------
13              
14             Readonly::Scalar my $DESC => q{Transform $x[0] to @x[0]};
15             Readonly::Scalar my $EXPL =>
16             q{Perl6 uses the data type as the sigil now, not the context desired};
17              
18             #-----------------------------------------------------------------------------
19              
20 1     1 0 5 sub supported_parameters { return () }
21 1     1 1 5 sub default_necessity { return $NECESSITY_HIGHEST }
22 0     0 1   sub default_themes { return qw( core ) }
23             sub applies_to {
24 0     0 1   return 'PPI::Token::Symbol',
25             'PPI::Token::ArrayIndex'
26             }
27              
28             #-----------------------------------------------------------------------------
29              
30             #
31             # %foo --> %foo
32             # $foo{a} --> %foo{a} # Note it does not pointify braces.
33             # @foo --> @foo
34             # $foo[1] --> @foo[1]
35             #
36             sub transform {
37 0     0 0   my ($self, $elem, $doc) = @_;
38 0 0         if ( $elem->isa('PPI::Token::ArrayIndex') ) {
39 0           my $content = $elem->content;
40              
41 0           $content =~ s{\$#}{};
42              
43             #
44             # There's a bug that causes $elem->parent to go away here.
45             # Not sure if it's PPI or not...
46             #
47 0 0         unless ( $elem->parent ) {
48 0           warn "XXX PPI bug triggered\n";
49 0           return;
50             }
51              
52             $elem->insert_before(
53 0           PPI::Token::Symbol->new('@' . $content)
54             );
55 0           $elem->insert_before(
56             PPI::Token::Symbol->new('.')
57             );
58 0           $elem->insert_before(
59             PPI::Token::Word->new('end')
60             );
61 0           $elem->delete;
62             }
63             else {
64 0 0         return if $elem->raw_type eq '@';
65 0 0         return if $elem->raw_type eq '%';
66              
67 0 0         if ( $elem->next_sibling ) {
68 0           my $subscript = $elem->snext_sibling;
69 0 0         return unless $subscript->isa('PPI::Structure::Subscript');
70 0           my $new_content = $elem->content;
71              
72 0 0         if ( $subscript->start eq '[' ) {
    0          
73 0           substr($new_content, 0, 1) = '@';
74             }
75             elsif ( $subscript->start eq '{' ) {
76 0           substr($new_content, 0, 1) = '%';
77             }
78 0           $elem->set_content( $new_content );
79             }
80             }
81              
82 0           return $self->transformation( $DESC, $EXPL, $elem );
83             }
84              
85             1;
86              
87             #-----------------------------------------------------------------------------
88              
89             __END__
90              
91             =pod
92              
93             =head1 NAME
94              
95             Perl::ToPerl6::Transformer::Variables::RewriteSigils - Give variables their proper sigils.
96              
97              
98             =head1 AFFILIATION
99              
100             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
101             distribution.
102              
103              
104             =head1 DESCRIPTION
105              
106             Perl6 uses the sigil type as the data type now, and this is probably the most common operation people will want to do to their file. This transformer doesn't alter hash keys or array indices, those are left to transformers down the line:
107              
108             @foo = () --> @foo = ()
109             $foo[1] --> @foo[1]
110             %foo = () --> %foo = ()
111             $foo{a} --> %foo{a} # Not %foo<a> or %foo{'a'} yet.
112              
113             Transforms variables outside of comments, heredocs, strings and POD.
114              
115             =head1 CONFIGURATION
116              
117             This Transformer is not configurable except for the standard options.
118              
119             =head1 AUTHOR
120              
121             Jeffrey Goff <drforr@pobox.com>
122              
123             =head1 COPYRIGHT
124              
125             Copyright (c) 2015 Jeffrey Goff
126              
127             This program is free software; you can redistribute it and/or modify
128             it under the same terms as Perl itself.
129              
130             =cut
131              
132             ##############################################################################
133             # Local Variables:
134             # mode: cperl
135             # cperl-indent-level: 4
136             # fill-column: 78
137             # indent-tabs-mode: nil
138             # c-indentation-style: bsd
139             # End:
140             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :