File Coverage

blib/lib/Perl/ToPerl6/Transformer/Pragmas/RewritePragmas.pm
Criterion Covered Total %
statement 22 28 78.5
branch 0 2 0.0
condition 0 6 0.0
subroutine 9 13 69.2
pod 3 5 60.0
total 34 54 62.9


line stmt bran cond sub pod time code
1             package Perl::ToPerl6::Transformer::Pragmas::RewritePragmas;
2              
3 1     1   1906 use 5.006001;
  1         3  
4 1     1   5 use strict;
  1         3  
  1         19  
5 1     1   4 use warnings;
  1         1  
  1         22  
6 1     1   5 use Readonly;
  1         1  
  1         44  
7              
8 1     1   4 use Perl::ToPerl6::Utils qw{ :severities };
  1         2  
  1         49  
9 1     1   109 use Perl::ToPerl6::Utils::PPI qw{ is_version_number is_pragma };
  1         2  
  1         46  
10              
11 1     1   4 use base 'Perl::ToPerl6::Transformer';
  1         2  
  1         266  
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Delete unnecessary pragmas};
16             Readonly::Scalar my $EXPL =>
17             q{Pragmas such as 'strict', 'warnings', 'utf8' and 'version' are unnecessary or redundant};
18              
19             #-----------------------------------------------------------------------------
20              
21             my %map = (
22             v6 => 1,
23             constant => 1, # 'use constant FOO => 1' --> 'constant FOO = 1' later
24             base => 1, # 'use base "Foo::Mommy' --> 'class Foo is Foo::Mommy' later
25             parent => 1, # 'use parent "Foo::Mommy' --> 'class Foo is Foo::Mommy' later
26             );
27              
28             #-----------------------------------------------------------------------------
29              
30 1     1 0 3 sub supported_parameters { return () }
31 1     1 1 6 sub default_necessity { return $NECESSITY_HIGHEST }
32 0     0 1   sub default_themes { return qw( core ) }
33             sub applies_to {
34             return sub {
35             $_[1]->isa('PPI::Statement::Include') and
36             ( is_version_number($_[1]->child(2)) or
37             is_pragma($_[1]->child(2)) ) and
38 0 0 0 0     not exists $map{$_[1]->child(2)->content}
      0        
39             }
40 0     0 1   }
41              
42             #-----------------------------------------------------------------------------
43              
44             #
45             # 'use strict;' --> ''
46             # 'use warnings;' --> ''
47             # 'no strict;' --> ''
48             # 'no warnings;' --> ''
49             # 'use 5.6;' --> ''
50             # 'use v6;' --> 'use v6;'
51             # 'use constant;' --> 'use constant;'
52             #
53             sub transform {
54 0     0 0   my ($self, $elem, $doc) = @_;
55              
56 0           $elem->remove;
57              
58 0           return $self->transformation( $DESC, $EXPL, $elem );
59             }
60              
61             1;
62              
63             #-----------------------------------------------------------------------------
64              
65             __END__
66              
67             =pod
68              
69             =head1 NAME
70              
71             Perl::ToPerl6::Transformer::Pragma::RewritePragmas - Remove unnecessary pragmas
72              
73              
74             =head1 AFFILIATION
75              
76             This Transformer is part of the core L<Perl::ToPerl6|Perl::ToPerl6>
77             distribution.
78              
79              
80             =head1 DESCRIPTION
81              
82             Removes uneeded Perl5 pragmas. More specifically, it removes all core pragmas except C<v6>, C<constant>, C<base> and C<parent>. The C<v6> pragma remains untouched, C<constant> will be transformed later into <constant FOO = 1>, C<base> and C<parent> are added to the class declaration:
83              
84             use strict; --> ''
85             no strict 'refs'; --> ''
86             use warnings; --> ''
87             use constant FOO => 1; --> use constant FOO => 1;
88             use base qw(Foo); --> use base qw(Foo);
89              
90             Transforms pragmas outside of comments, heredocs, strings and POD.
91              
92             The 'constant', 'base' and 'parent' pragmas are left untouched for later transformers, so that they can do their Perl6 things.
93              
94             =head1 CONFIGURATION
95              
96             This Transformer is not configurable except for the standard options.
97              
98             =head1 AUTHOR
99              
100             Jeffrey Goff <drforr@pobox.com>
101              
102             =head1 COPYRIGHT
103              
104             Copyright (c) 2015 Jeffrey Goff
105              
106             This program is free software; you can redistribute it and/or modify
107             it under the same terms as Perl itself.
108              
109             =cut
110              
111             ##############################################################################
112             # Local Variables:
113             # mode: cperl
114             # cperl-indent-level: 4
115             # fill-column: 78
116             # indent-tabs-mode: nil
117             # c-indentation-style: bsd
118             # End:
119             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :