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