Login
Replace the CONSTRUCTOR section of the ProhibitQuotedWordLists POD
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / CodeLayout / RequireTidyCode.pm
CommitLineData
dff08b70
JRT
1#######################################################################
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6########################################################################
7
59b05e08
JRT
8package Perl::Critic::Policy::CodeLayout::RequireTidyCode;
9
10use strict;
11use warnings;
bf159007 12use English qw(-no_match_vars);
59b05e08 13use Perl::Critic::Utils;
59b05e08
JRT
14use base 'Perl::Critic::Policy';
15
a7340650 16our $VERSION = 0.22;
59b05e08 17
dff08b70
JRT
18#----------------------------------------------------------------------------
19
59b05e08 20my $desc = q{Code is not tidy};
5dc3db7a 21my $expl = [ 33 ];
59b05e08
JRT
22
23#----------------------------------------------------------------------------
24
6bf9b465 25sub default_severity { return $SEVERITY_LOWEST }
7a6b5c70 26sub default_themes { return qw(pbp cosmetic) }
faa35de4 27sub applies_to { return 'PPI::Document' }
dff08b70 28
02558daa
CD
29#---------------------------------------------------------------------------
30
31sub new {
32 my ($class, %args) = @_;
33 my $self = bless {}, $class;
34
35 #Set configuration if defined
36 $self->{_perltidyrc} = $args{perltidyrc};
37 if (defined $self->{_perltidyrc} && $self->{_perltidyrc} eq $EMPTY)
38 {
39 $self->{_perltidyrc} = \$EMPTY;
40 }
41
42 return $self;
43}
44
dff08b70 45#----------------------------------------------------------------------------
59b05e08
JRT
46
47sub violates {
48 my ( $self, $elem, $doc ) = @_;
bf159007
JRT
49
50 # If Perl::Tidy is missing, silently pass this test
51 eval { require Perl::Tidy; };
52 return if $EVAL_ERROR;
59b05e08 53
05bf01cd 54 # Perl::Tidy seems to produce slightly different output, depending
871ec032 55 # on the trailing whitespace in the input. As best I can tell,
fd9d73d0
JRT
56 # Perl::Tidy will truncate any extra trailing newlines, and if the
57 # input has no trailing newline, then it adds one. But when you
58 # re-run it through Perl::Tidy here, that final newline gets lost,
59 # which causes the policy to insist that the code is not tidy.
60 # This only occurs when Perl::Tidy is writing the output to a
61 # scalar, but does not occur when writing to a file. I may
62 # investigate further, but for now, this seems to do the trick.
871ec032 63
02558daa 64 my $source = $doc->serialize();
fd9d73d0 65 $source =~ s{ \s+ \Z}{\n}mx;
871ec032 66
44b74300
CD
67 # Remove the shell fix code from the top of program, if applicable
68 my $shebang_re = qr/\#![^\015\012]+[\015\012]+/xms;
69 my $shell_re = qr/eval [ ] 'exec [ ] [^\015\012]* [ ] \$0 [ ] \${1\+"\$@"}'
70 [ \t]*[\012\015]+ [ \t]*if[^\015\012]+[\015\012]+/xms;
71 $source =~ s/\A ($shebang_re) $shell_re /$1/xms;
72
59b05e08 73 my $dest = $EMPTY;
59b05e08
JRT
74 my $stderr = $EMPTY;
75
871ec032 76
6bf9b465
JRT
77 # Perl::Tidy gets confused if @ARGV has arguments from
78 # another program. Also, we need to override the
79 # stdout and stderr redirects that the user may have
80 # configured in their .perltidyrc file.
81 local @ARGV = qw(-nst -nse); ## no critic
ee99f507 82
d5fc0818
JRT
83 # Trap Perl::Tidy errors, just in case it dies
84 eval {
0a6f07d0
AL
85 Perl::Tidy::perltidy(
86 source => \$source,
87 destination => \$dest,
88 stderr => \$stderr,
02558daa 89 defined $self->{_perltidyrc} ? (perltidyrc => $self->{_perltidyrc}) : (),
d5fc0818
JRT
90 );
91 };
92
93 if ($stderr || $EVAL_ERROR) {
59b05e08
JRT
94
95 # Looks like perltidy had problems
44b74300 96 return $self->violation( 'perltidy had errors!!', $expl, $elem );
59b05e08
JRT
97 }
98
47639f94 99 if ( $source ne $dest ) {
2c6df011 100 return $self->violation( $desc, $expl, $elem );
59b05e08
JRT
101 }
102
103 return; #ok!
104}
105
1061;
107
6bf9b465
JRT
108#----------------------------------------------------------------------------
109
59b05e08
JRT
110__END__
111
112=pod
113
114=head1 NAME
115
116Perl::Critic::Policy::CodeLayout::RequireTidyCode
117
118=head1 DESCRIPTION
119
120Conway does make specific recommendations for whitespace and
121curly-braces in your code, but the most important thing is to adopt a
122consistent layout, regardless of the specifics. And the easiest way
123to do that is to use L<Perl::Tidy>. This policy will complain if
124you're code hasn't been run through Perl::Tidy.
125
02558daa
CD
126=head1 CONSTRUCTOR
127
128This Policy accepts an additional key-value pair in the constructor.
129The key must be C<perltidyrc> and the value is the filename of a
130Perl::Tidy configuration file. The default is C<undef>, which tells
131Perl::Tidy to look in it's default location. Users of Perl::Critic
132can configure this in their F<.perlcriticrc> file like this:
133
134 [CodeLayout::RequireTidyCode]
135 perltidyrc = /usr/share/perltidy.conf
136
137As a special case, setting C<perltidyrc> to the empty string tells
138Perl::Tidy not to load any configuration file at all and just use
139Perl::Tidy's own default style.
140
141 [CodeLayout::RequireTidyCode]
142 perltidyrc =
143
59b05e08
JRT
144=head1 NOTES
145
df249cc5
JRT
146L<Perl::Tidy> is not included in the Perl::Critic distribution. The
147latest version of Perl::Tidy can be downloaded from CPAN. If
148Perl::Tidy is not installed, this policy is silently ignored.
59b05e08
JRT
149
150=head1 SEE ALSO
151
152L<Perl::Tidy>
153
59b05e08
JRT
154=head1 AUTHOR
155
156Jeffrey Ryan Thalhammer <thaljef@cpan.org>
157
158=head1 COPYRIGHT
159
c3c88e54 160Copyright (c) 2005-2006 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
161
162This program is free software; you can redistribute it and/or modify
163it under the same terms as Perl itself. The full text of this license
164can be found in the LICENSE file included with this module.
165
166=cut
737d3b65
CD
167
168# Local Variables:
169# mode: cperl
170# cperl-indent-level: 4
171# fill-column: 78
172# indent-tabs-mode: nil
173# c-indentation-style: bsd
174# End:
dbb78cdc 175# ex: set ts=8 sts=4 sw=4 expandtab :