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