File Coverage

blib/lib/Perl/ToPerl6.pm
Criterion Covered Total %
statement 49 114 42.9
branch 0 34 0.0
condition 1 12 8.3
subroutine 16 25 64.0
pod 6 6 100.0
total 72 191 37.7


line stmt bran cond sub pod time code
1             package Perl::ToPerl6;
2              
3 1     1   15 use 5.006001;
  1         4  
4 1     1   4 use strict;
  1         2  
  1         25  
5 1     1   5 use warnings;
  1         1  
  1         30  
6              
7 1     1   4 use English qw(-no_match_vars);
  1         2  
  1         7  
8 1     1   399 use Readonly;
  1         1  
  1         37  
9              
10 1     1   5 use Exporter 'import';
  1         1  
  1         26  
11              
12 1     1   5 use File::Spec;
  1         1  
  1         19  
13 1     1   5 use List::MoreUtils qw< firstidx >;
  1         5  
  1         15  
14 1     1   672 use Scalar::Util qw< blessed >;
  1         1  
  1         40  
15              
16 1     1   505 use Perl::ToPerl6::Exception::Configuration::Generic;
  1         2  
  1         45  
17 1     1   635 use Perl::ToPerl6::Config;
  1         3  
  1         35  
18 1     1   6 use Perl::ToPerl6::Transformation;
  1         2  
  1         53  
19 1     1   696 use Perl::ToPerl6::Document;
  1         3  
  1         34  
20 1     1   613 use Perl::ToPerl6::Statistics;
  1         3  
  1         30  
21 1     1   6 use Perl::ToPerl6::Utils qw< :characters hashify shebang_line >;
  1         1  
  1         70  
22              
23             #-----------------------------------------------------------------------------
24              
25             our $VERSION = '0.040';
26              
27             #-----------------------------------------------------------------------------
28              
29             Readonly::Array our @EXPORT_OK => qw(transform);
30              
31             #=============================================================================
32             # PUBLIC methods
33              
34             sub new {
35 1     1 1 3972 my ( $class, %args ) = @_;
36 1         3 my $self = bless {}, $class;
37 1   33     18 $self->{_config} = $args{-config} || Perl::ToPerl6::Config->new( %args );
38 1         11 $self->{_stats} = Perl::ToPerl6::Statistics->new();
39 1         5 return $self;
40             }
41              
42             #-----------------------------------------------------------------------------
43              
44             sub config {
45 0     0 1   my $self = shift;
46 0           return $self->{_config};
47             }
48              
49             #-----------------------------------------------------------------------------
50              
51             sub apply_transform {
52 0     0 1   my ( $self, @args ) = @_;
53             #Delegate to Perl::ToPerl6::Config
54 0           return $self->config()->apply_transform( @args );
55             }
56              
57             #-----------------------------------------------------------------------------
58              
59             sub transformers {
60 0     0 1   my $self = shift;
61              
62             #Delegate to Perl::ToPerl6::Config
63 0           return $self->config()->transformers();
64             }
65              
66             #-----------------------------------------------------------------------------
67              
68             sub statistics {
69 0     0 1   my $self = shift;
70 0           return $self->{_stats};
71             }
72              
73             #-----------------------------------------------------------------------------
74              
75             sub transform {
76              
77             #-------------------------------------------------------------------
78             # This subroutine can be called as an object method or as a static
79             # function. In the latter case, the first argument can be a
80             # hashref of configuration parameters that shall be used to create
81             # an object behind the scenes. Note that this object does not
82             # persist. In other words, it is not a singleton.
83             #
84             # In addition, if it is called with a trailing 'doc => \$ref'
85             # named argument, the reference is populated with the serialized document.
86             # This is only really needed for test suites.
87             #
88             # Here are some of the ways this subroutine might get called:
89             #
90             # #Object style...
91             # $mogrify->transform( $code );
92             # $mogrify->transform( $code, doc => \$my_doc );
93             #
94             # #Functional style...
95             # transform( $code );
96             # transform( {}, $code );
97             # transform( {-foo => bar}, $code );
98             # transform( {-foo => bar}, $code, doc => \$my_doc );
99             #------------------------------------------------------------------
100              
101 0 0   0 1   my ( $self, $source_code ) = @_ >= 2 ? @_ : ( {}, $_[0] );
102 0 0         $self = ref $self eq 'HASH' ? __PACKAGE__->new(%{ $self }) : $self;
  0            
103 0 0         return if not defined $source_code; # If no code, then nothing to do.
104              
105 0           my $config = $self->config();
106 0 0 0       my $doc =
107             blessed($source_code) && $source_code->isa('Perl::ToPerl6::Document')
108             ? $source_code
109             : Perl::ToPerl6::Document->new(
110             '-source' => $source_code,
111             '-program-extensions' => [$config->program_extensions_as_regexes()],
112             );
113              
114 0 0         if ( 0 == $self->transformers() ) {
115 0           Perl::ToPerl6::Exception::Configuration::Generic->throw(
116             message => 'There are no enabled transformers.',
117             )
118             }
119              
120 0           my @transformations = $self->_gather_transformations($doc);
121              
122             # Never thought I'd be smuggling myself in one of these.
123             #
124 0 0 0       if ( $_[-2] and $_[-2] eq 'doc' ) {
125 0           ${$_[-1]} = $doc->serialize;
  0            
126             }
127 0 0         unless( ref $source_code ) {
128 0 0         if ( $config->in_place() ) {
129 0 0         open my $fh, '>', $source_code
130             or die "Could not overwrite '$source_code': $!";
131 0           print $fh $doc->serialize;
132 0           close $fh;
133             }
134             else {
135 0 0         open my $fh, '>', $source_code . '.pl6'
136             or die "Could not write to '$source_code.pl6': $!";
137 0           print $fh $doc->serialize;
138 0           close $fh;
139             }
140             }
141 0           return @transformations;
142             }
143              
144             #=============================================================================
145             # PRIVATE methods
146              
147             sub _gather_transformations {
148 0     0     my ($self, $doc) = @_;
149              
150             # Disable exempt code lines, if desired
151 0 0         if ( not $self->config->force() ) {
152 0           $doc->process_annotations();
153             }
154              
155             # Evaluate each transformer
156 0           my @transformers = $self->config->transformers();
157 0           my @ordered_transformers = _futz_with_transformer_order(@transformers);
158 0           my @transformations = map { _transform($_, $doc) } @ordered_transformers;
  0            
159              
160             # Accumulate statistics
161 0           $self->statistics->accumulate( $doc, \@transformations );
162              
163             # If requested, rank transformations by their necessity and return the top N.
164 0 0 0       if ( @transformations && (my $top = $self->config->top()) ) {
165 0 0         my $limit = @transformations < $top ? $#transformations : $top-1;
166 0           @transformations = Perl::ToPerl6::Transformation::sort_by_necessity(@transformations);
167 0           @transformations = ( reverse @transformations )[ 0 .. $limit ]; #Slicing...
168             }
169              
170             # Always return transformations sorted by location
171 0           return Perl::ToPerl6::Transformation->sort_by_location(@transformations);
172             }
173              
174             #=============================================================================
175             # PRIVATE functions
176              
177             sub _transform {
178 0     0     my ($transformer, $doc) = @_;
179              
180 0 0         return if not $transformer->prepare_to_scan_document($doc);
181              
182 0           my @transformations = ();
183              
184             TYPE:
185 0           for my $type ( $transformer->applies_to() ) {
186 0           my @elements;
187 0 0         if ($type eq 'PPI::Document') {
188 0           @elements = ($doc);
189             }
190             else {
191 0 0         @elements = @{ $doc->find($type) || [] };
  0            
192             }
193              
194             ELEMENT:
195 0           for my $element (@elements) {
196              
197             # Evaluate the transformer on this $element. A transformer may
198             # return zero or more transformations. We only want the
199             # transformations that occur on lines that have not been
200             # disabled.
201              
202             VIOLATION:
203 0           for my $transformation ( $transformer->transform( $element, $doc ) ) {
204              
205 0           my $line = $transformation->location()->[0];
206 0 0         if ( $doc->line_is_disabled_for_transformer($line, $transformer) ) {
207 0           $doc->add_suppressed_transformation($transformation);
208 0           next VIOLATION;
209             }
210              
211 0           push @transformations, $transformation;
212             }
213             }
214             }
215              
216 0           return @transformations;
217             }
218              
219             #-----------------------------------------------------------------------------
220              
221             sub _futz_with_transformer_order {
222             # The ProhibitUselessNoCritic transformer is another special transformer. It
223             # deals with the transformations that *other* Transformers produce. Therefore
224             # it needs to be run *after* all the other Transformers. TODO: find
225             # a way for Transformers to express an ordering preference somehow.
226              
227 0     0     my @transformer_objects = @_;
228 0           my $magical_transformer_name = 'Perl::ToPerl6::Transformer::Miscellanea::ProhibitUselessNoCritic';
229 0     0     my $idx = firstidx {ref $_ eq $magical_transformer_name} @transformer_objects;
  0            
230 0           push @transformer_objects, splice @transformer_objects, $idx, 1;
231 0           return @transformer_objects;
232             }
233              
234             #-----------------------------------------------------------------------------
235              
236             1;
237              
238              
239              
240             __END__
241              
242             =pod
243              
244             =for stopwords DGR INI-style API -params refactored ActivePerl ben Jore
245             Dolan's Twitter Alexandr Ciornii Ciornii's downloadable
246              
247             =head1 NAME
248              
249             Perl::ToPerl6 - Critique Perl source code for best-practices.
250              
251              
252             =head1 SYNOPSIS
253              
254             use Perl::ToPerl6;
255             my $file = shift;
256             my $mogrify = Perl::ToPerl6->new();
257             my @transformations = $mogrify->transform($file);
258             print @transformations;
259              
260              
261             =head1 DESCRIPTION
262              
263             Perl::ToPerl6 is an extensible framework for creating and applying coding
264             standards to Perl source code. Essentially, it is a static source code
265             analysis engine. Perl::ToPerl6 is distributed with a number of
266             L<Perl::ToPerl6::Transformer> modules that attempt to enforce various coding
267             guidelines. Most Transformer modules are based on Damian Conway's book B<Perl Best
268             Practices>. However, Perl::ToPerl6 is B<not> limited to PBP and will even
269             support Transformers that contradict Conway. You can enable, disable, and
270             customize those Polices through the Perl::ToPerl6 interface. You can also
271             create new Transformer modules that suit your own tastes.
272              
273             For a command-line interface to Perl::ToPerl6, see the documentation for
274             L<perlmogrify>. If you want to integrate Perl::ToPerl6 with your build process,
275             L<Test::Perl::ToPerl6> provides an interface that is suitable for test
276             programs. Also, L<Test::Perl::ToPerl6::Progressive> is useful for gradually
277             applying coding standards to legacy code. For the ultimate convenience (at
278             the expense of some flexibility) see the L<mogrification> pragma.
279              
280             If you'd like to try L<Perl::ToPerl6> without installing anything, there is a
281             web-service available at L<http://perlmogrify.com>. The web-service does not
282             yet support all the configuration features that are available in the native
283             Perl::ToPerl6 API, but it should give you a good idea of what it does.
284              
285             Also, ActivePerl includes a very slick graphical interface to Perl-ToPerl6
286             called C<perlmogrify-gui>. You can get a free community edition of ActivePerl
287             from L<http://www.activestate.com>.
288              
289              
290             =head1 INTERFACE SUPPORT
291              
292             This is considered to be a public class. Any changes to its interface will go
293             through a deprecation cycle.
294              
295              
296             =head1 CONSTRUCTOR
297              
298             =over
299              
300             =item C<< new( [ -profile => $FILE, -necessity => $N, -detail => $N, -theme => $string, -include => \@PATTERNS, -exclude => \@PATTERNS, -top => $N, -in_place => $B, -only => $B, -profile-strictness => $PROFILE_STRICTNESS_{WARN|FATAL|QUIET}, -force => $B, -verbose => $N ], -color => $B, -pager => $string, -mogrification-fatal => $B) >>
301              
302             =item C<< new() >>
303              
304             Returns a reference to a new Perl::ToPerl6 object. Most arguments are just
305             passed directly into L<Perl::ToPerl6::Config>, but I have described them here
306             as well. The default value for all arguments can be defined in your
307             F<.perlmogrifyrc> file. See the L<"CONFIGURATION"> section for more
308             information about that. All arguments are optional key-value pairs as
309             follows:
310              
311             B<-profile> is a path to a configuration file. If C<$FILE> is not defined,
312             Perl::ToPerl6::Config attempts to find a F<.perlmogrifyrc> configuration file in
313             the current directory, and then in your home directory. Alternatively, you
314             can set the C<PERLMOGRIFY> environment variable to point to a file in another
315             location. If a configuration file can't be found, or if C<$FILE> is an empty
316             string, then all Transformers will be loaded with their default configuration.
317             See L<"CONFIGURATION"> for more information.
318              
319             B<-necessity> is the minimum necessity level. Only Transformer modules that have a
320             necessity greater than C<$N> will be applied. Necessity values are integers
321             ranging from 1 (least severe transformations) to 5 (most severe transformations). The
322             default is 5. For a given C<-profile>, decreasing the C<-necessity> will
323             usually reveal more Transformer transformations. You can set the default value for this
324             option in your F<.perlmogrifyrc> file. Users can redefine the necessity level
325             for any Transformer in their F<.perlmogrifyrc> file. See L<"CONFIGURATION"> for
326             more information.
327              
328             If it is difficult for you to remember whether necessity "5" is the most or
329             least restrictive level, then you can use one of these named values:
330              
331             NECESSITY NAME ...is equivalent to... NECESSITY NUMBER
332             --------------------------------------------------------
333             -necessity => 'gentle' -necessity => 5
334             -necessity => 'stern' -necessity => 4
335             -necessity => 'harsh' -necessity => 3
336             -necessity => 'cruel' -necessity => 2
337             -necessity => 'brutal' -necessity => 1
338              
339             The names reflect how severely the code is mogrified: a C<gentle>
340             mogrification reports only the most severe transformations, and so on down to a
341             C<brutal> mogrification which reports even the most minor transformations.
342              
343             B<-theme> is special expression that determines which Transformers to apply
344             based on their respective themes. For example, the following would load only
345             Transformers that have a 'bugs' AND 'core' theme:
346              
347             my $mogrify = Perl::ToPerl6->new( -theme => 'bugs && core' );
348              
349             Unless the C<-necessity> option is explicitly given, setting C<-theme> silently
350             causes the C<-necessity> to be set to 1. You can set the default value for
351             this option in your F<.perlmogrifyrc> file. See the L<"POLICY THEMES"> section
352             for more information about themes.
353              
354              
355             B<-include> is a reference to a list of string C<@PATTERNS>. Transformer modules
356             that match at least one C<m/$PATTERN/ixms> will always be loaded, irrespective
357             of all other settings. For example:
358              
359             my $mogrify = Perl::ToPerl6->new(-include => ['layout'] -necessity => 4);
360              
361             This would cause Perl::ToPerl6 to apply all the C<CodeLayout::*> Transformer modules
362             even though they have a necessity level that is less than 4. You can set the
363             default value for this option in your F<.perlmogrifyrc> file. You can also use
364             C<-include> in conjunction with the C<-exclude> option. Note that C<-exclude>
365             takes precedence over C<-include> when a Transformer matches both patterns.
366              
367             B<-exclude> is a reference to a list of string C<@PATTERNS>. Transformer modules
368             that match at least one C<m/$PATTERN/ixms> will not be loaded, irrespective of
369             all other settings. For example:
370              
371             my $mogrify = Perl::ToPerl6->new(-exclude => ['strict'] -necessity => 1);
372              
373             This would cause Perl::ToPerl6 to not apply the C<RequireUseStrict> and
374             C<ProhibitNoStrict> Transformer modules even though they have a necessity level that
375             is greater than 1. You can set the default value for this option in your
376             F<.perlmogrifyrc> file. You can also use C<-exclude> in conjunction with the
377             C<-include> option. Note that C<-exclude> takes precedence over C<-include>
378             when a Transformer matches both patterns.
379              
380             B<-single-transformer> is a string C<PATTERN>. Only one transformer that matches
381             C<m/$PATTERN/ixms> will be used. Transformers that do not match will be
382             excluded. This option has precedence over the C<-necessity>, C<-theme>,
383             C<-include>, C<-exclude>, and C<-only> options. You can set the default value
384             for this option in your F<.perlmogrifyrc> file.
385              
386             B<-top> is the maximum number of Transformations to return when ranked by their
387             necessity levels. This must be a positive integer. Transformations are still
388             returned in the order that they occur within the file. Unless the C<-necessity>
389             option is explicitly given, setting C<-top> silently causes the C<-necessity>
390             to be set to 1. You can set the default value for this option in your
391             F<.perlmogrifyrc> file.
392              
393             B<-in_place> is a boolean value. If set to a true value, Perl::ToPerl6 will
394             replace the existing Perl source with its transformed equivalent. This of
395             course overwrites the file's contents, and should be done sparingly, if at all.
396             If set to a false value (which is the default), then Perl::ToPerl6 creates a
397             new file, adding '.pl6' to the existing filename. You can set the default
398             value for this option in your F<.perlmogrifyrc> file.
399              
400             B<-only> is a boolean value. If set to a true value, Perl::ToPerl6 will only
401             choose from Transformers that are mentioned in the user's profile. If set to a
402             false value (which is the default), then Perl::ToPerl6 chooses from all the
403             Transformers that it finds at your site. You can set the default value for this
404             option in your F<.perlmogrifyrc> file.
405              
406             B<-profile-strictness> is an enumerated value, one of
407             L<Perl::ToPerl6::Utils::Constants/"$PROFILE_STRICTNESS_WARN"> (the default),
408             L<Perl::ToPerl6::Utils::Constants/"$PROFILE_STRICTNESS_FATAL">, and
409             L<Perl::ToPerl6::Utils::Constants/"$PROFILE_STRICTNESS_QUIET">. If set to
410             L<Perl::ToPerl6::Utils::Constants/"$PROFILE_STRICTNESS_FATAL">, Perl::ToPerl6
411             will make certain warnings about problems found in a F<.perlmogrifyrc> or file
412             specified via the B<-profile> option fatal. For example, Perl::ToPerl6 normally
413             only C<warn>s about profiles referring to non-existent Transformers, but this
414             value makes this situation fatal. Correspondingly,
415             L<Perl::ToPerl6::Utils::Constants/"$PROFILE_STRICTNESS_QUIET"> makes
416             Perl::ToPerl6 shut up about these things.
417              
418             B<-detail> can be an integer (from 0 to 5). If set to a non-zero value, all
419             transformations of a necessity equal to or B<less> than B<-detail> will be
420             reported on. You can set the default value for this option in your
421             F<.perlmogrifyrc> file.
422              
423             B<-force> is a boolean value that controls whether Perl::ToPerl6 observes the
424             magical C<"## no mogrify"> annotations in your code. If set to a true value,
425             Perl::ToPerl6 will analyze all code. If set to a false value (which is the
426             default) Perl::ToPerl6 will ignore code that is tagged with these annotations.
427             See L<"BENDING THE RULES"> for more information. You can set the default
428             value for this option in your F<.perlmogrifyrc> file.
429              
430             B<-verbose> can be a positive integer (from 1 to 11), or a literal format
431             specification. See L<Perl::ToPerl6::Transformation|Perl::ToPerl6::Transformation> for an
432             explanation of format specifications. You can set the default value for this
433             option in your F<.perlmogrifyrc> file.
434              
435             B<-color> and B<-pager> are not used by Perl::ToPerl6 but is provided for the
436             benefit of L<perlmogrify|perlmogrify>.
437              
438             B<-mogrification-fatal> is not used by Perl::ToPerl6 but is provided for the
439             benefit of L<mogrification|mogrification>.
440              
441             B<-color-necessity-highest>, B<-color-necessity-high>, B<-color-necessity-
442             medium>, B<-color-necessity-low>, and B<-color-necessity-lowest> are not used by
443             Perl::ToPerl6, but are provided for the benefit of L<perlmogrify|perlmogrify>.
444             Each is set to the Term::ANSIColor color specification to be used to display
445             transformations of the corresponding necessity.
446              
447             B<-files-with-transformations> and B<-files-without-transformations> are not used by
448             Perl::ToPerl6, but are provided for the benefit of L<perlmogrify|perlmogrify>, to
449             cause only the relevant filenames to be displayed.
450              
451             =back
452              
453              
454             =head1 METHODS
455              
456             =over
457              
458             =item C<transform( $source_code )>
459              
460             Runs the C<$source_code> through the Perl::ToPerl6 engine using all the
461             Transformers that have been loaded into this engine. If C<$source_code> is a
462             scalar reference, then it is treated as a string of actual Perl code. If
463             C<$source_code> is a reference to an instance of L<PPI::Document>, then that
464             instance is used directly. Otherwise, it is treated as a path to a local file
465             containing Perl code. This method returns a list of
466             L<Perl::ToPerl6::Transformation> objects for each transformation of the loaded
467             Transformers.
468             The list is sorted in the order that the Transformations appear in the code. If
469             there are no transformations, this method returns an empty list.
470              
471             =item C<< apply_transform( -transformer => $transformer_name, -params => \%param_hash ) >>
472              
473             Creates a Transformer object and loads it into this ToPerl6. If the object cannot
474             be instantiated, it will throw a fatal exception. Otherwise, it returns a
475             reference to this ToPerl6.
476              
477             B<-transformer> is the name of a L<Perl::ToPerl6::Transformer> subclass module. The
478             C<'Perl::ToPerl6::Transformer'> portion of the name can be omitted for brevity.
479             This argument is required.
480              
481             B<-params> is an optional reference to a hash of Transformer parameters. The
482             contents of this hash reference will be passed into to the constructor of the
483             Transformer module. See the documentation in the relevant Transformer module for a
484             description of the arguments it supports.
485              
486             =item C< transformers() >
487              
488             Returns a list containing references to all the Transformer objects that have been
489             loaded into this engine. Objects will be in the order that they were loaded.
490              
491             =item C< config() >
492              
493             Returns the L<Perl::ToPerl6::Config> object that was created for or given to
494             this ToPerl6.
495              
496             =item C< statistics() >
497              
498             Returns the L<Perl::ToPerl6::Statistics> object that was created for this
499             ToPerl6. The Statistics object accumulates data for all files that are
500             analyzed by this ToPerl6.
501              
502             =back
503              
504              
505             =head1 FUNCTIONAL INTERFACE
506              
507             For those folks who prefer to have a functional interface, The C<transform>
508             method can be exported on request and called as a static function. If the
509             first argument is a hashref, its contents are used to construct a new
510             Perl::ToPerl6 object internally. The keys of that hash should be the same as
511             those supported by the C<Perl::ToPerl6::new()> method. Here are some examples:
512              
513             use Perl::ToPerl6 qw(transform);
514              
515             # Use default parameters...
516             @transformations = transform( $some_file );
517              
518             # Use custom parameters...
519             @transformations = transform( {-necessity => 2}, $some_file );
520              
521             # As a one-liner
522             %> perl -MPerl::ToPerl6=transform -e 'print transform(shift)' some_file.pm
523              
524             None of the other object-methods are currently supported as static
525             functions. Sorry.
526              
527              
528             =head1 CONFIGURATION
529              
530             Most of the settings for Perl::ToPerl6 and each of the Transformer modules can be
531             controlled by a configuration file. The default configuration file is called
532             F<.perlmogrifyrc>. Perl::ToPerl6 will look for this file in the current
533             directory first, and then in your home directory. Alternatively, you can set
534             the C<PERLMOGRIFY> environment variable to explicitly point to a different file
535             in another location. If none of these files exist, and the C<-profile> option
536             is not given to the constructor, then all the modules that are found in the
537             Perl::ToPerl6::Transformer namespace will be loaded with their default
538             configuration.
539              
540             The format of the configuration file is a series of INI-style blocks that
541             contain key-value pairs separated by '='. Comments should start with '#' and
542             can be placed on a separate line or after the name-value pairs if you desire.
543              
544             Default settings for Perl::ToPerl6 itself can be set B<before the first named
545             block.> For example, putting any or all of these at the top of your
546             configuration file will set the default value for the corresponding
547             constructor argument.
548              
549             necessity = 3 #Integer or named level
550             in_place = 0 #Zero or One
551             only = 1 #Zero or One
552             force = 0 #Zero or One
553             detail = 0 #Integer or named level
554             verbose = 4 #Integer or format spec
555             top = 50 #A positive integer
556             theme = (pbp || security) && bugs #A theme expression
557             include = NamingConventions ClassHierarchies #Space-delimited list
558             exclude = Variables Modules::RequirePackage #Space-delimited list
559             mogrification-fatal = 1 #Zero or One
560             color = 1 #Zero or One
561             pager = less #pager to pipe output to
562              
563             The remainder of the configuration file is a series of blocks like this:
564              
565             [Perl::ToPerl6::Transformer::Category::TransformerName]
566             necessity = 1
567             set_themes = foo bar
568             add_themes = baz
569             arg1 = value1
570             arg2 = value2
571              
572             C<Perl::ToPerl6::Transformer::Category::TransformerName> is the full name of a
573             module that implements the transformer. The Transformer modules distributed
574             with Perl::ToPerl6 have been grouped into categories according to token type.
575             For brevity, you can omit the C<'Perl::ToPerl6::Transformer'> part of the
576             module name.
577              
578             C<necessity> is the level of importance you wish to assign to the Transformer.
579             All Transformer modules are defined with a default necessity value ranging from
580             1 (least severe) to 5 (most severe). However, you may disagree with the default
581             necessity and choose to give it a higher or lower necessity, based on your own
582             coding philosophy. You can set the C<necessity> to an integer from 1 to 5, or
583             use one of the equivalent names:
584              
585             NECESSITY NAME ...is equivalent to... NECESSITY NUMBER
586             ----------------------------------------------------
587             gentle 5
588             stern 4
589             harsh 3
590             cruel 2
591             brutal 1
592              
593             The names reflect how severely the code is mogrified: a C<gentle>
594             mogrification reports only the most severe transformations, and so on down to a
595             C<brutal> mogrification which reports even the most minor transformations.
596              
597             C<set_themes> sets the theme for the Transformer and overrides its default theme. The argument is a string of one or more whitespace-delimited alphanumeric
598             words. Themes are case-insensitive. See L<"POLICY THEMES"> for more
599             information.
600              
601             C<add_themes> appends to the default themes for this Transformer. The argument
602             is a string of one or more whitespace-delimited words. Themes are case-
603             insensitive. See L<"POLICY THEMES"> for more information.
604              
605             The remaining key-value pairs are configuration parameters that will be passed
606             into the constructor for that Transformer. The constructors for most
607             Transformer objects do not support arguments, and those that do should have
608             reasonable defaults. See the documentation on the appropriate Transformer
609             module for more details.
610              
611             Instead of redefining the necessity for a given Transformer, you can completely
612             disable a Transformer by prepending a '-' to the name of the module in your
613             configuration file. In this manner, the Transformer will never be loaded,
614             regardless of the C<-necessity> given to the Perl::ToPerl6 constructor.
615              
616             A simple configuration might look like this:
617              
618             #--------------------------------------------------------------
619             # I think these are really important, so always load them
620              
621             [TestingAndDebugging::RequireUseStrict]
622             necessity = 5
623              
624             [TestingAndDebugging::RequireUseWarnings]
625             necessity = 5
626              
627             #--------------------------------------------------------------
628             # I think these are less important, so only load when asked
629              
630             [Variables::ProhibitPackageVars]
631             necessity = 2
632              
633             [ControlStructures::ProhibitPostfixControls]
634             allow = if unless # My custom configuration
635             necessity = cruel # Same as "necessity = 2"
636              
637             #--------------------------------------------------------------
638             # Give these transformers a custom theme. I can activate just
639             # these transformers by saying `perlmogrify -theme larry`
640              
641             [Modules::RequireFilenameMatchesPackage]
642             add_themes = larry
643              
644             [TestingAndDebugging::RequireTestLables]
645             add_themes = larry curly moe
646              
647             #--------------------------------------------------------------
648             # I do not agree with these at all, so never load them
649              
650             [-NamingConventions::Capitalization]
651             [-ValuesAndExpressions::ProhibitMagicNumbers]
652              
653             #--------------------------------------------------------------
654             # For all other Transformers, I accept the default necessity,
655             # so no additional configuration is required for them.
656              
657             For additional configuration examples, see the F<perlmogrifyrc> file that is
658             included in this F<examples> directory of this distribution.
659              
660             Damian Conway's own Perl::ToPerl6 configuration is also included in this
661             distribution as F<examples/perlmogrifyrc-conway>.
662              
663              
664             =head1 THE POLICIES
665              
666             A large number of Transformer modules are distributed with Perl::ToPerl6. They are
667             described briefly in the companion document L<Perl::ToPerl6::TransformerSummary> and
668             in more detail in the individual modules themselves. Say C<"perlmogrify -doc
669             PATTERN"> to see the perldoc for all Transformer modules that match the regex
670             C<m/PATTERN/ixms>
671              
672             There are a number of distributions of additional transformers on CPAN. If
673             L<Perl::ToPerl6> doesn't contain a transformer that you want, some one may have
674             already written it. See the L</"SEE ALSO"> section below for a list of some
675             of these distributions.
676              
677              
678             =head1 POLICY THEMES
679              
680             Each Transformer is defined with one or more "themes". Themes can be used to
681             create arbitrary groups of Transformers. They are intended to provide an
682             alternative mechanism for selecting your preferred set of Transformers. For
683             example, you may wish disable a certain subset of Transformers when analyzing
684             test programs. Conversely, you may wish to enable only a specific subset of
685             Transformers when analyzing modules.
686              
687             The Transformers that ship with Perl::ToPerl6 have been broken into the
688             following themes. This is just our attempt to provide some basic logical
689             groupings. You are free to invent new themes that suit your needs.
690              
691             THEME DESCRIPTION
692             --------------------------------------------------------------------------
693             core All transformers that ship with Perl::ToPerl6
694             pbp Transformers that come directly from "Perl Best Practices"
695             bugs Transformers that that prevent or reveal bugs
696             maintenance Transformers that affect the long-term health of the code
697             cosmetic Transformers that only have a superficial effect
698             complexity Transformers that specificaly relate to code complexity
699             security Transformers that relate to security issues
700             tests Transformers that are specific to test programs
701              
702              
703             Any Transformer may fit into multiple themes. Say C<"perlmogrify -list"> to get a
704             listing of all available Transformers and the themes that are associated with each
705             one. You can also change the theme for any Transformer in your F<.perlmogrifyrc>
706             file. See the L<"CONFIGURATION"> section for more information about that.
707              
708             Using the C<-theme> option, you can create an arbitrarily complex rule that
709             determines which Transformers will be loaded. Precedence is the same as regular
710             Perl code, and you can use parentheses to enforce precedence as well.
711             Supported operators are:
712              
713             Operator Alternative Example
714             -----------------------------------------------------------------
715             && and 'pbp && core'
716             || or 'pbp || (bugs && security)'
717             ! not 'pbp && ! (portability || complexity)'
718              
719             Theme names are case-insensitive. If the C<-theme> is set to an empty string,
720             then it evaluates as true all Transformers.
721              
722              
723             =head1 BENDING THE RULES
724              
725             Perl::ToPerl6 takes a hard-line approach to your code: either you comply or you
726             don't. In the real world, it is not always practical (nor even possible) to
727             fully comply with coding standards. In such cases, it is wise to show that
728             you are knowingly violating the standards and that you have a Damn Good Reason
729             (DGR) for doing so.
730              
731             To help with those situations, you can direct Perl::ToPerl6 to ignore certain
732             lines or blocks of code by using annotations:
733              
734             require 'LegacyLibaray1.pl'; ## no mogrify
735             require 'LegacyLibrary2.pl'; ## no mogrify
736              
737             for my $element (@list) {
738              
739             ## no mogrify
740              
741             $foo = ""; #Violates 'ProhibitEmptyQuotes'
742             $barf = bar() if $foo; #Violates 'ProhibitPostfixControls'
743             #Some more evil code...
744              
745             ## use mogrify
746              
747             #Some good code...
748             do_something($_);
749             }
750              
751             The C<"## no mogrify"> annotations direct Perl::ToPerl6 to ignore the remaining
752             lines of code until a C<"## use mogrify"> annotation is found. If the C<"## no
753             mogrify"> annotation is on the same line as a code statement, then only that
754             line of code is overlooked. To direct perlmogrify to ignore the C<"## no
755             mogrify"> annotations, use the C<--force> option.
756              
757             A bare C<"## no mogrify"> annotation disables all the active Transformers.
758             If you wish to disable only specific Transformers, add a list of Transformer
759             names as arguments, just as you would for the C<"no strict"> or C<"no warnings">
760             pragmas. For example, this would disable the C<ProhibitEmptyQuotes> and
761             C<ProhibitPostfixControls> transformers until the end of the block or until the
762             next C<"## use mogrify"> annotation (whichever comes first):
763              
764             ## no mogrify (EmptyQuotes, PostfixControls)
765              
766             # Now exempt from ValuesAndExpressions::ProhibitEmptyQuotes
767             $foo = "";
768              
769             # Now exempt ControlStructures::ProhibitPostfixControls
770             $barf = bar() if $foo;
771              
772             # Still subjected to ValuesAndExpression::RequireNumberSeparators
773             $long_int = 10000000000;
774              
775             Since the Transformer names are matched against the C<"## no mogrify"> arguments as
776             regular expressions, you can abbreviate the Transformer names or disable an entire
777             family of Transformers in one shot like this:
778              
779             ## no mogrify (NamingConventions)
780              
781             # Now exempt from NamingConventions::Capitalization
782             my $camelHumpVar = 'foo';
783              
784             # Now exempt from NamingConventions::Capitalization
785             sub camelHumpSub {}
786              
787             The argument list must be enclosed in parentheses and must contain one or more
788             comma-separated barewords (e.g. don't use quotes). The C<"## no mogrify">
789             annotations can be nested, and Transformers named by an inner annotation will be
790             disabled along with those already disabled an outer annotation.
791              
792             Some Transformers like C<Subroutines::ProhibitExcessComplexity> apply to an
793             entire block of code. In those cases, the C<"## no mogrify"> annotation must
794             appear on the line where the transformation is reported. For example:
795              
796             sub complicated_function { ## no mogrify (ProhibitExcessComplexity)
797             # Your code here...
798             }
799              
800             Transformers such as C<Documentation::RequirePodSections> apply to the entire
801             document, in which case transformations are reported at line 1.
802              
803             Use this feature wisely. C<"## no mogrify"> annotations should be used in the
804             smallest possible scope, or only on individual lines of code. And you should
805             always be as specific as possible about which Transformers you want to disable
806             (i.e. never use a bare C<"## no mogrify">). If Perl::ToPerl6 complains about
807             your code, try and find a compliant solution before resorting to this feature.
808              
809              
810             =head1 THE L<Perl::ToPerl6> PHILOSOPHY
811              
812             Coding standards are deeply personal and highly subjective. The goal of
813             Perl::ToPerl6 is to help you write code that conforms with a set of best
814             practices. Our primary goal is not to dictate what those practices are, but
815             rather, to implement the practices discovered by others. Ultimately, you make
816             the rules -- Perl::ToPerl6 is merely a tool for encouraging consistency. If
817             there is a transformer that you think is important or that we have overlooked, we
818             would be very grateful for contributions, or you can simply load your own
819             private set of transformers into Perl::ToPerl6.
820              
821              
822             =head1 EXTENDING THE MOGRIFIER
823              
824             The modular design of Perl::ToPerl6 is intended to facilitate the addition of
825             new Transformers. You'll need to have some understanding of L<PPI>, but most
826             Transformer modules are pretty straightforward and only require about 20 lines of
827             code. Please see the L<Perl::ToPerl6::DEVELOPER> file included in this
828             distribution for a step-by-step demonstration of how to create new Transformer
829             modules.
830              
831             If you develop any Transformer modules, feel free to add a pull request on
832             GitHub, L<http://github.com/drforr/Perl-Mogrify.git>.
833              
834              
835             =head1 PREREQUISITES
836              
837             Perl::ToPerl6 requires the following modules:
838              
839             L<B::Keywords>
840              
841             L<Config::Tiny>
842              
843             L<Exception::Class>
844              
845             L<File::HomeDir>
846              
847             L<File::Spec>
848              
849             L<File::Spec::Unix>
850              
851             L<File::Which>
852              
853             L<IO::String>
854              
855             L<List::MoreUtils>
856              
857             L<List::Util>
858              
859             L<Module::Pluggable>
860              
861             L<PPI|PPI>
862              
863             L<Pod::PlainText>
864              
865             L<Pod::Select>
866              
867             L<Pod::Usage>
868              
869             L<Readonly>
870              
871             L<Scalar::Util>
872              
873             L<String::Format>
874              
875             L<Task::Weaken>
876              
877             L<Term::ANSIColor>
878              
879             L<Text::ParseWords>
880              
881             L<version|version>
882              
883              
884             =head1 CONTACTING THE DEVELOPMENT TEAM
885              
886             You are encouraged to subscribe to the mailing list; send a message to
887             L<mailto:users-subscribe@perlmogrify.tigris.org>. To prevent spam, you may be
888             required to register for a user account with Tigris.org before being allowed
889             to post messages to the mailing list. See also the mailing list archives at
890             L<http://perlmogrify.tigris.org/servlets/SummarizeList?listName=users>. At
891             least one member of the development team is usually hanging around in
892             L<irc://irc.perl.org/#perlmogrify> and you can follow Perl::ToPerl6 on Twitter,
893             at L<https://twitter.com/perlmogrify>.
894              
895             =head1 BUGS
896              
897             Scrutinizing Perl code is hard for humans, let alone machines. If you find
898             any bugs, particularly false-positives or false-negatives from a
899             Perl::ToPerl6::Transformer, please submit them at L<https://github.com/Perl-ToPerl6
900             /Perl-ToPerl6/issues>. Thanks.
901              
902             =head1 CREDITS
903              
904             Adam Kennedy - For creating L<PPI>, the heart and soul of L<Perl::ToPerl6>.
905              
906             Chris Dolan - For contributing the best features and Transformer modules.
907              
908             Andy Lester - Wise sage and master of all-things-testing.
909              
910             Elliot Shank - The self-proclaimed quality freak.
911              
912             Giuseppe Maxia - For all the great ideas and positive encouragement.
913              
914             Thanks also to the Perl Foundation for providing a grant to support Chris
915             Dolan's project to implement twenty PBP transformers.
916             L<http://www.perlfoundation.org/april_1_2007_new_grant_awards>
917              
918             =head1 AUTHOR
919              
920             Jeffrey Goff <drforr@pobox.com>
921              
922             =head1 AUTHOR Emeritus
923              
924             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
925              
926              
927             =head1 COPYRIGHT
928              
929             Copyright (c) 2015 Jeffrey Goff. All rights reserved.
930              
931             This program is free software; you can redistribute it and/or modify it under
932             the same terms as Perl itself. The full text of this license can be found in
933             the LICENSE file included with this module.
934              
935             =cut
936              
937             ##############################################################################
938             # Local Variables:
939             # mode: cperl
940             # cperl-indent-level: 4
941             # fill-column: 78
942             # indent-tabs-mode: nil
943             # c-indentation-style: bsd
944             # End:
945             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :