1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/branches/Perl-Critic-With-Param-Validation/lib/Perl/Critic/PolicyListing.pm $
3 # $Date: 2006-12-13 21:35:21 -0800 (Wed, 13 Dec 2006) $
6 ##############################################################################
8 package Perl::Critic::ProfilePrototype;
14 use English qw(-no_match_vars);
16 use Perl::Critic::Config qw{};
17 use Perl::Critic::Policy qw{};
18 use Perl::Critic::Utils qw{ :characters };
19 use overload ( q{""} => 'to_string' );
21 our $VERSION = '1.088';
23 #-----------------------------------------------------------------------------
26 my ($class, %args) = @_;
27 my $self = bless {}, $class;
29 my $policies = $args{-policies} || [];
30 $self->{_policies} = [ sort _by_type @{ $policies } ];
32 my $comment_out_parameters = $args{'-comment-out-parameters'};
33 if (not defined $comment_out_parameters) {
34 $comment_out_parameters = 1;
36 $self->{_comment_out_parameters} = $comment_out_parameters;
38 my $configuration = $args{'-config'};
39 if (not $configuration) {
40 $configuration = Perl::Critic::Config->new(-profile => $EMPTY);
42 $self->{_configuration} = $configuration;
48 #-----------------------------------------------------------------------------
53 return $self->{_policies};
56 sub _comment_out_parameters {
59 return $self->{_comment_out_parameters};
65 return $self->{_configuration};
68 #-----------------------------------------------------------------------------
73 return $self->_comment_out_parameters() ? q{# } : $EMPTY;
76 #-----------------------------------------------------------------------------
81 my $prefix = $self->_line_prefix();
82 my $configuration = $self->_configuration();
84 my $prototype = "# Globals\n";
86 $prototype .= $prefix;
87 $prototype .= q{severity = };
88 $prototype .= $configuration->severity();
91 $prototype .= $prefix;
92 $prototype .= q{force = };
93 $prototype .= $configuration->force();
96 $prototype .= $prefix;
97 $prototype .= q{only = };
98 $prototype .= $configuration->only();
101 $prototype .= $prefix;
102 $prototype .= q{profile-strictness = };
103 $prototype .= $configuration->profile_strictness();
106 $prototype .= $prefix;
107 $prototype .= q{color = };
108 $prototype .= $configuration->color();
111 $prototype .= $prefix;
112 $prototype .= q{top = };
113 $prototype .= $configuration->top();
116 $prototype .= $prefix;
117 $prototype .= q{verbose = };
118 $prototype .= $configuration->verbose();
121 $prototype .= $prefix;
122 $prototype .= q{include = };
123 $prototype .= join $SPACE, $configuration->include();
126 $prototype .= $prefix;
127 $prototype .= q{exclude = };
128 $prototype .= join $SPACE, $configuration->exclude();
131 $prototype .= $prefix;
132 $prototype .= q{single-policy = };
133 $prototype .= join $SPACE, $configuration->single_policy();
136 $prototype .= $prefix;
137 $prototype .= q{theme = };
138 $prototype .= $configuration->theme()->rule();
141 Perl::Critic::Policy::set_format( $self->_proto_format() );
143 return $prototype . "\n" . join q{}, map { "$_" } @{ $self->_get_policies() };
146 #-----------------------------------------------------------------------------
148 # About "%{\\n%\\x7b# \\x7df\n${prefix}%n = %D\\n}O" below:
150 # The %0 format for a policy specifies how to format parameters.
151 # For a parameter %f specifies the full description.
153 # The problem is that both of these need to take options, but String::Format
154 # doesn't allow nesting of {}. So, to get the option to the %f, the braces
155 # are hex encoded. I.e., assuming that comment_out_parameters is in effect,
156 # the parameter sees:
158 # \n%{# }f\n# %n = %D\n
163 my $prefix = $self->_line_prefix();
165 return <<"END_OF_FORMAT";
168 ${prefix}set_themes = %t
169 ${prefix}add_themes =
170 ${prefix}severity = %s
171 ${prefix}maximum_violations_per_document = %v
172 %{\\n%\\x7b# \\x7df\\n${prefix}%n = %D\\n}O%{${prefix}Cannot programmatically discover what parameters this policy takes.\\n}U
178 #-----------------------------------------------------------------------------
180 sub _by_type { return ref $a cmp ref $b }
190 Perl::Critic::ProfilePrototype - Generate an initial Perl::Critic profile.
194 This is a helper class that generates a prototype of a L<Perl::Critic> profile
195 (e.g. a F<.perlcriticrc> file. There are no user-serviceable parts here.
201 =item C<< new( -policies => \@POLICY_OBJECTS ) >>
203 Returns a reference to a new C<Perl::Critic::ProfilePrototype> object.
213 Returns a string representation of this C<ProfilePrototype>. See
214 L<"OVERLOADS"> for more information.
220 When a L<Perl::Critic::ProfilePrototype> is evaluated in string context, it
221 produces a multi-line summary of the policy name, default themes, and default
222 severity for each L<Perl::Critic::Policy> object that was given to the
223 constructor of this C<ProfilePrototype>. If the Policy supports an additional
224 parameters, they will also be listed (but commented-out). The format is
225 suitable for use as a F<.perlcriticrc> file.
229 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
233 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
235 This program is free software; you can redistribute it and/or modify
236 it under the same terms as Perl itself. The full text of this license
237 can be found in the LICENSE file included with this module.
243 # cperl-indent-level: 4
245 # indent-tabs-mode: nil
246 # c-indentation-style: bsd
248 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :