File Coverage

blib/lib/Perl/ToPerl6/Transformer/Variables/QuoteHashKeys.pm
Criterion Covered Total %
statement 19 37 51.3
branch 0 6 0.0
condition 0 18 0.0
subroutine 8 12 66.6
pod 3 5 60.0
total 30 78 38.4


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::Variables::QuoteHashKeys;
2              
3 1     1   708 use 5.006001;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         18  
5 1     1   4 use warnings;
  1         2  
  1         20  
6 1     1   5 use Readonly;
  1         3  
  1         39  
7              
8 1     1   5 use Perl::ToPerl6::Utils qw{ :severities };
  1         1  
  1         97  
9              
10 1     1   108 use base 'Perl::ToPerl6::Transformer';
  1         2  
  1         417  
11              
12             #-----------------------------------------------------------------------------
13              
14             Readonly::Scalar my $DESC => q{Transform %x{a} to %x{'a'} depending upon mode};
15             Readonly::Scalar my $EXPL =>
16             q{Perl6 assumes that braces are code blocks, so any content must be compilable};
17              
18             #-----------------------------------------------------------------------------
19              
20             sub supported_parameters {
21             return ( {
22 1     1 0 8 name => 'perl6_mode',
23             description => q{Controls whether <> or {''} are used},
24             default_string => 0,
25             behavior => 'boolean'
26             } )
27             }
28 1     1 1 5 sub default_necessity { return $NECESSITY_HIGHEST }
29 0     0 1   sub default_themes { return qw( core ) }
30             sub applies_to {
31             return sub {
32 0 0 0 0     $_[1]->isa('PPI::Structure::Subscript') and
      0        
      0        
      0        
33             $_[1]->start->content eq '{' and
34             $_[1]->finish->content eq '}' and
35             ( $_[1]->sprevious_sibling->isa('PPI::Token::Symbol') or
36             $_[1]->sprevious_sibling->isa('PPI::Token::Operator') ) and
37             not $_[1]->schild(0)->schild(0)->isa('PPI::Token::Quote')
38             }
39 0     0 1   }
40              
41             #-----------------------------------------------------------------------------
42              
43             sub transform {
44 0     0 0   my ($self, $elem, $doc) = @_;
45 0           my $token = $elem;
46              
47 0   0       while ( $token and
48             $token->isa('PPI::Structure::Subscript') ) {
49 0 0 0       if ( $token->start and
50             $token->start->content eq '{' ) {
51 0 0         if ( $self->{_perl6_mode} ) {
52 0           $token->start->set_content('<');
53 0           $token->finish->set_content('>');
54             }
55             else {
56 0           my $bareword = $token->schild(0)->schild(0);
57 0           my $old_content = $bareword->content;
58 0           $old_content =~ s{'}{\\'}g;
59            
60 0           my $new_content = "'" . $old_content . "'";
61            
62 0           $bareword->insert_after(
63             PPI::Token::Quote::Single->new($new_content)
64             );
65 0           $bareword->delete;
66             }
67             }
68 0           $token = $token->snext_sibling;
69             }
70              
71 0           return $self->transformation( $DESC, $EXPL, $elem );
72             }
73              
74             1;
75              
76             #-----------------------------------------------------------------------------
77              
78             __END__
79              
80             =pod
81              
82             =head1 NAME
83              
84             Perl::ToPerl6::Transformer::Variables::QuoteHashKeys - Transform bareword hash keys into quoted hash keys
85              
86              
87             =head1 AFFILIATION
88              
89             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
90             distribution.
91              
92              
93             =head1 DESCRIPTION
94              
95             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:
96              
97             %foo{a} --> %foo{'a'}
98             %foo{'a'} --> %foo{'a'}
99              
100             The transformer also supports a C<perl6_mode> option, which replaces C<%foo{a}> with C<< %foo<a> >> style "pointy blocks". The default, though, is to a more perl5ish style.
101              
102             Transforms variables outside of comments, heredocs, strings and POD.
103              
104             =head1 CONFIGURATION
105              
106             This Transformer is not configurable except for the standard options.
107              
108             =head1 AUTHOR
109              
110             Jeffrey Goff <drforr@pobox.com>
111              
112             =head1 COPYRIGHT
113              
114             Copyright (c) 2015 Jeffrey Goff
115              
116             This program is free software; you can redistribute it and/or modify
117             it under the same terms as Perl itself.
118              
119             =cut
120              
121             ##############################################################################
122             # Local Variables:
123             # mode: cperl
124             # cperl-indent-level: 4
125             # fill-column: 78
126             # indent-tabs-mode: nil
127             # c-indentation-style: bsd
128             # End:
129             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :