File Coverage

blib/lib/Perl/ToPerl6/Transformer/Variables/FormatHashKeys.pm
Criterion Covered Total %
statement 22 34 64.7
branch 0 4 0.0
condition 2 18 11.1
subroutine 11 12 91.6
pod 3 5 60.0
total 38 73 52.0


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::Variables::FormatHashKeys;
2              
3 17     17   11380 use 5.006001;
  17         48  
4 17     17   85 use strict;
  17         23  
  17         373  
5 17     17   70 use warnings;
  17         27  
  17         451  
6 17     17   73 use Readonly;
  17         23  
  17         996  
7              
8 17     17   92 use Perl::ToPerl6::Utils qw{ :characters :severities };
  17         26  
  17         904  
9              
10 17     17   4179 use base 'Perl::ToPerl6::Transformer';
  17         26  
  17         6659  
11              
12             our $VERSION = '0.03';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Scalar my $DESC => q{Transform %x{a} to %x{'a'}};
17             Readonly::Scalar my $EXPL =>
18             q{Perl6 assumes that braces are code blocks, so any content must be compilable};
19              
20             #-----------------------------------------------------------------------------
21              
22 40     40 0 1229 sub supported_parameters { return () }
23 29     29 1 110 sub default_severity { return $SEVERITY_HIGHEST }
24 25     25 1 90 sub default_themes { return qw(core bugs) }
25             sub applies_to {
26             return sub {
27 61 0 33 61   834 $_[1]->isa('PPI::Structure::Subscript') and
      33        
      0        
      0        
28             $_[1]->start->content eq '{' and
29             $_[1]->finish->content eq '}' and
30             ( $_[1]->sprevious_sibling->isa('PPI::Token::Symbol') or
31             $_[1]->sprevious_sibling->isa('PPI::Token::Operator') ) and
32             not $_[1]->schild(0)->schild(0)->isa('PPI::Token::Quote')
33             }
34 4     4 1 25 }
35              
36             #-----------------------------------------------------------------------------
37              
38             sub transform {
39 0     0 0   my ($self, $elem, $doc) = @_;
40 0           my $token = $elem;
41              
42 0   0       while ( $token and
43             $token->isa('PPI::Structure::Subscript') ) {
44 0 0 0       if ( $token->start and
45             $token->start->content eq '{' ) {
46 0           my $bareword = $token->schild(0)->schild(0);
47 0           my $old_content = $bareword->content;
48 0           $old_content =~ s{'}{\\'}g;
49            
50 0           my $new_content = "'" . $old_content . "'";
51            
52 0           $bareword->insert_after(
53             PPI::Token::Quote::Single->new($new_content)
54             );
55 0           $bareword->delete;
56             }
57 0           $token = $token->snext_sibling;
58             }
59              
60 0           return $self->transformation( $DESC, $EXPL, $elem );
61             }
62              
63             1;
64              
65             #-----------------------------------------------------------------------------
66              
67             __END__
68              
69             =pod
70              
71             =head1 NAME
72              
73             Perl::ToPerl6::Transformer::Variables::FormatHashKeys - Transform bareword hash keys into quoted hash keys
74              
75              
76             =head1 AFFILIATION
77              
78             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
79             distribution.
80              
81              
82             =head1 DESCRIPTION
83              
84             Anything enclosed in braces should be compilable code in Perl6, and unfortunately bareword hash keys such as C<%foo{a}> are interpreted as C<%foo{a()}>, so when the function a() can't be found, the block fails to compile:
85              
86             %foo{a} --> %foo{'a'}
87             %foo{'a'} --> %foo{'a'}
88              
89             Using angle brackets as in C<< %foo<a> >> would be more Perl6 style, but this is a change that most people will immediately understand.
90              
91             Transforms variables outside of comments, heredocs, strings and POD.
92              
93             =head1 CONFIGURATION
94              
95             This Transformer is not configurable except for the standard options.
96              
97             =head1 AUTHOR
98              
99             Jeffrey Goff <drforr@pobox.com>
100              
101             =head1 COPYRIGHT
102              
103             Copyright (c) 2015 Jeffrey Goff
104              
105             This program is free software; you can redistribute it and/or modify
106             it under the same terms as Perl itself.
107              
108             =cut
109              
110             ##############################################################################
111             # Local Variables:
112             # mode: cperl
113             # cperl-indent-level: 4
114             # fill-column: 78
115             # indent-tabs-mode: nil
116             # c-indentation-style: bsd
117             # End:
118             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :