Login
Get rid of the "## Please see file perltidy.ERR" message in "Build test"
[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
8d36cd6f 17our $VERSION = '0.18';
59b05e08
JRT
18$VERSION = eval $VERSION; ## no critic
19
dff08b70
JRT
20#----------------------------------------------------------------------------
21
59b05e08 22my $desc = q{Code is not tidy};
5dc3db7a 23my $expl = [ 33 ];
59b05e08
JRT
24
25#----------------------------------------------------------------------------
26
6bf9b465 27sub default_severity { return $SEVERITY_LOWEST }
dff08b70
JRT
28sub applies_to { return 'PPI::Document' }
29
30#----------------------------------------------------------------------------
59b05e08
JRT
31
32sub violates {
33 my ( $self, $elem, $doc ) = @_;
bf159007
JRT
34
35 # If Perl::Tidy is missing, silently pass this test
36 eval { require Perl::Tidy; };
37 return if $EVAL_ERROR;
59b05e08 38
871ec032
JRT
39 # Perl::Tidy seems to produce slightly different output, depeding
40 # on the trailing whitespace in the input. As best I can tell,
fd9d73d0
JRT
41 # Perl::Tidy will truncate any extra trailing newlines, and if the
42 # input has no trailing newline, then it adds one. But when you
43 # re-run it through Perl::Tidy here, that final newline gets lost,
44 # which causes the policy to insist that the code is not tidy.
45 # This only occurs when Perl::Tidy is writing the output to a
46 # scalar, but does not occur when writing to a file. I may
47 # investigate further, but for now, this seems to do the trick.
871ec032 48
5bf96118 49 my $source = $doc->content();
fd9d73d0 50 $source =~ s{ \s+ \Z}{\n}mx;
871ec032 51
59b05e08 52 my $dest = $EMPTY;
59b05e08
JRT
53 my $stderr = $EMPTY;
54
871ec032 55
6bf9b465
JRT
56 # Perl::Tidy gets confused if @ARGV has arguments from
57 # another program. Also, we need to override the
58 # stdout and stderr redirects that the user may have
59 # configured in their .perltidyrc file.
60 local @ARGV = qw(-nst -nse); ## no critic
ee99f507 61
d5fc0818
JRT
62 # Trap Perl::Tidy errors, just in case it dies
63 eval {
0a6f07d0
AL
64 Perl::Tidy::perltidy(
65 source => \$source,
66 destination => \$dest,
67 stderr => \$stderr,
d5fc0818
JRT
68 );
69 };
70
71 if ($stderr || $EVAL_ERROR) {
59b05e08
JRT
72
73 # Looks like perltidy had problems
74 $desc = q{perltidy had errors!!};
75 }
76
47639f94 77 if ( $source ne $dest ) {
2c6df011 78 return $self->violation( $desc, $expl, $elem );
59b05e08
JRT
79 }
80
81 return; #ok!
82}
83
841;
85
6bf9b465
JRT
86#----------------------------------------------------------------------------
87
59b05e08
JRT
88__END__
89
90=pod
91
92=head1 NAME
93
94Perl::Critic::Policy::CodeLayout::RequireTidyCode
95
96=head1 DESCRIPTION
97
98Conway does make specific recommendations for whitespace and
99curly-braces in your code, but the most important thing is to adopt a
100consistent layout, regardless of the specifics. And the easiest way
101to do that is to use L<Perl::Tidy>. This policy will complain if
102you're code hasn't been run through Perl::Tidy.
103
104=head1 NOTES
105
df249cc5
JRT
106L<Perl::Tidy> is not included in the Perl::Critic distribution. The
107latest version of Perl::Tidy can be downloaded from CPAN. If
108Perl::Tidy is not installed, this policy is silently ignored.
59b05e08
JRT
109
110=head1 SEE ALSO
111
112L<Perl::Tidy>
113
114
115=head1 AUTHOR
116
117Jeffrey Ryan Thalhammer <thaljef@cpan.org>
118
119=head1 COPYRIGHT
120
c3c88e54 121Copyright (c) 2005-2006 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
122
123This program is free software; you can redistribute it and/or modify
124it under the same terms as Perl itself. The full text of this license
125can be found in the LICENSE file included with this module.
126
127=cut