Login
Fixed bug where we passed the $doc instead of the $elem to the
[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
JRT
13use Perl::Critic::Utils;
14use Perl::Critic::Violation;
15use base 'Perl::Critic::Policy';
16
c3166b47 17our $VERSION = '0.15';
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
JRT
38
39 my $source = "$doc";
40 my $dest = $EMPTY;
59b05e08
JRT
41 my $stderr = $EMPTY;
42
6bf9b465
JRT
43 # Perl::Tidy gets confused if @ARGV has arguments from
44 # another program. Also, we need to override the
45 # stdout and stderr redirects that the user may have
46 # configured in their .perltidyrc file.
47 local @ARGV = qw(-nst -nse); ## no critic
ee99f507 48
d5fc0818
JRT
49 # Trap Perl::Tidy errors, just in case it dies
50 eval {
51 Perl::Tidy::perltidy(
52 source => \$source,
53 destination => \$dest,
54 stderr => \$stderr,
55 );
56 };
57
58 if ($stderr || $EVAL_ERROR) {
59b05e08
JRT
59
60 # Looks like perltidy had problems
61 $desc = q{perltidy had errors!!};
62 }
63
47639f94 64 if ( $source ne $dest ) {
6bf9b465
JRT
65 my $sev = $self->get_severity();
66 return Perl::Critic::Violation->new( $desc, $expl, $doc, $sev );
59b05e08
JRT
67 }
68
69 return; #ok!
70}
71
721;
73
6bf9b465
JRT
74#----------------------------------------------------------------------------
75
59b05e08
JRT
76__END__
77
78=pod
79
80=head1 NAME
81
82Perl::Critic::Policy::CodeLayout::RequireTidyCode
83
84=head1 DESCRIPTION
85
86Conway does make specific recommendations for whitespace and
87curly-braces in your code, but the most important thing is to adopt a
88consistent layout, regardless of the specifics. And the easiest way
89to do that is to use L<Perl::Tidy>. This policy will complain if
90you're code hasn't been run through Perl::Tidy.
91
92=head1 NOTES
93
94Since L<Perl::Tidy> is not widely deployed, this is the only policy in
95the L<Perl::Critic> distribution that is not enabled by default. To
96enable it, put this line in your F<.perlcriticrc> file:
97
98 [CodeLayout::RequireTidyCode]
99
100=head1 SEE ALSO
101
102L<Perl::Tidy>
103
104
105=head1 AUTHOR
106
107Jeffrey Ryan Thalhammer <thaljef@cpan.org>
108
109=head1 COPYRIGHT
110
c3c88e54 111Copyright (c) 2005-2006 Jeffrey Ryan Thalhammer. All rights reserved.
59b05e08
JRT
112
113This program is free software; you can redistribute it and/or modify
114it under the same terms as Perl itself. The full text of this license
115can be found in the LICENSE file included with this module.
116
117=cut