1Perl::Critic::Policy(3)User Contributed Perl DocumentatioPnerl::Critic::Policy(3)
2
3
4
6 Perl::Critic::Policy - Base class for all Policy modules.
7
9 Perl::Critic::Policy is the abstract base class for all Policy objects.
10 If you're developing your own Policies, your job is to implement and
11 override its methods in a subclass. To work with the Perl::Critic
12 engine, your implementation must behave as described below. For a
13 detailed explanation on how to make new Policy modules, please see the
14 Perl::Critic::DEVELOPER document included in this distribution.
15
17 This is considered to be a public class. Any changes to its interface
18 will go through a deprecation cycle.
19
21 "new( ... )"
22 Don't call this. As a Policy author, do not implement this. Use
23 the "initialize_if_enabled()" method for your Policy setup. See
24 the developer documentation for more.
25
26 "initialize_if_enabled( $config )"
27 This receives an instance of Perl::Critic::PolicyConfig as a
28 parameter, and is only invoked if this Policy is enabled by the
29 user. Thus, this is the preferred place for subclasses to do any
30 initialization.
31
32 Implementations of this method should return a boolean value
33 indicating whether the Policy should continue to be enabled. For
34 most subclasses, this will always be $TRUE. Policies that depend
35 upon external modules or other system facilities that may or may
36 not be available should test for the availability of these
37 dependencies and return $FALSE if they are not.
38
39 "prepare_to_scan_document( $document )"
40 The parameter is about to be scanned by this Policy. Whatever this
41 Policy wants to do in terms of preparation should happen here.
42 Returns a boolean value indicating whether the document should be
43 scanned at all; if this is a false value, this Policy won't be
44 applied to the document. By default, does nothing but return
45 $TRUE.
46
47 " violates( $element, $document ) "
48 Given a PPI::Element and a PPI::Document, returns one or more
49 Perl::Critic::Violation objects if the $element violates this
50 Policy. If there are no violations, then it returns an empty list.
51 If the Policy encounters an exception, then it should "croak" with
52 an error message and let the caller decide how to handle it.
53
54 "violates()" is an abstract method and it will abort if you attempt
55 to invoke it directly. It is the heart of all Policy modules, and
56 your subclass must override this method.
57
58 " violation( $description, $explanation, $element ) "
59 Returns a reference to a new "Perl::Critic::Violation" object. The
60 arguments are a description of the violation (as string), an
61 explanation for the policy (as string) or a series of page numbers
62 in PBP (as an ARRAY ref), a reference to the PPI element that
63 caused the violation.
64
65 These are the same as the constructor to Perl::Critic::Violation,
66 but without the severity. The Policy itself knows the severity.
67
68 " new_parameter_value_exception( $option_name, $option_value, $source,
69 $message_suffix ) "
70 Create a
71 Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue
72 for this Policy.
73
74 " throw_parameter_value_exception( $option_name, $option_value,
75 $source, $message_suffix ) "
76 Create and throw a
77 Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue.
78 Useful in parameter parser implementations.
79
80 " get_long_name() "
81 Return the full package name of this policy.
82
83 " get_short_name() "
84 Return the name of this policy without the "Perl::Critic::Policy::"
85 prefix.
86
87 " is_enabled() "
88 Answer whether this policy is really active or not. Returns a true
89 value if it is, a false, yet defined, value if it isn't, and an
90 undefined value if it hasn't yet been decided whether it will be.
91
92 " applies_to() "
93 Returns a list of the names of PPI classes that this Policy cares
94 about. By default, the result is "PPI::Element". Overriding this
95 method in Policy subclasses should lead to significant performance
96 increases.
97
98 " default_maximum_violations_per_document() "
99 Returns the default maximum number of violations for this policy to
100 report per document. By default, this not defined, but subclasses
101 may override this.
102
103 " get_maximum_violations_per_document() "
104 Returns the maximum number of violations this policy will report
105 for a single document. If this is not defined, then there is no
106 limit. If "set_maximum_violations_per_document()" has not been
107 invoked, then "default_maximum_violations_per_document()" is
108 returned.
109
110 " set_maximum_violations_per_document() "
111 Specify the maximum violations that this policy should report for a
112 document.
113
114 " default_severity() "
115 Returns the default severity for violating this Policy. See the
116 $SEVERITY constants in Perl::Critic::Utils for an enumeration of
117 possible severity values. By default, this method returns
118 $SEVERITY_LOWEST. Authors of Perl::Critic::Policy subclasses
119 should override this method to return a value that they feel is
120 appropriate for their Policy. In general, Polices that are widely
121 accepted or tend to prevent bugs should have a higher severity than
122 those that are more subjective or cosmetic in nature.
123
124 " get_severity() "
125 Returns the severity of violating this Policy. If the severity has
126 not been explicitly defined by calling "set_severity", then the
127 "default_severity" is returned. See the $SEVERITY constants in
128 Perl::Critic::Utils for an enumeration of possible severity values.
129
130 " set_severity( $N ) "
131 Sets the severity for violating this Policy. Clients of
132 Perl::Critic::Policy objects can call this method to assign a
133 different severity to the Policy if they don't agree with the
134 "default_severity". See the $SEVERITY constants in
135 Perl::Critic::Utils for an enumeration of possible values.
136
137 " default_themes() "
138 Returns a sorted list of the default themes associated with this
139 Policy. The default method returns an empty list. Policy authors
140 should override this method to return a list of themes that are
141 appropriate for their policy.
142
143 " get_themes() "
144 Returns a sorted list of the themes associated with this Policy.
145 If you haven't added themes or set the themes explicitly, this
146 method just returns the default themes.
147
148 " set_themes( @THEME_LIST ) "
149 Sets the themes associated with this Policy. Any existing themes
150 are overwritten. Duplicate themes will be removed.
151
152 " add_themes( @THEME_LIST ) "
153 Appends additional themes to this Policy. Any existing themes are
154 preserved. Duplicate themes will be removed.
155
156 " get_abstract() "
157 Retrieve the abstract for this policy (the part of the NAME section
158 of the POD after the module name), if it is available.
159
160 " get_raw_abstract() "
161 Retrieve the abstract for this policy (the part of the NAME section
162 of the POD after the module name), if it is available, in the
163 unparsed form.
164
165 " parameter_metadata_available() "
166 Returns whether information about the parameters is available.
167
168 " get_parameters() "
169 Returns a reference to an array containing instances of
170 Perl::Critic::PolicyParameter.
171
172 Note that this will return an empty list if the parameters for this
173 policy are unknown. In order to differentiate between this
174 circumstance and the one where this policy does not take any
175 parameters, it is necessary to call
176 "parameter_metadata_available()".
177
178 "set_format( $format )"
179 Class method. Sets the format for all Policy objects when they are
180 evaluated in string context. The default is "%p\n". See
181 "OVERLOADS" for formatting options.
182
183 "get_format()"
184 Class method. Returns the current format for all Policy objects
185 when they are evaluated in string context.
186
187 "to_string()"
188 Returns a string representation of the policy. The content of the
189 string depends on the current value returned by "get_format()".
190 See "OVERLOADS" for the details.
191
192 "is_safe()"
193 Answer whether this Policy can be used to analyze untrusted code,
194 i.e. the Policy doesn't have any potential side effects.
195
196 This method returns a true value by default.
197
198 An "unsafe" policy might attempt to compile the code, which, if you
199 have "BEGIN" or "CHECK" blocks that affect files or connect to
200 databases, is not a safe thing to do. If you are writing a such a
201 Policy, then you should override this method to return false.
202
203 By default Perl::Critic will not run unsafe policies.
204
206 When your Policy module first "use"s Perl::Critic::Violation, it will
207 try and extract the DESCRIPTION section of your Policy module's POD.
208 This information is displayed by Perl::Critic if the verbosity level is
209 set accordingly. Therefore, please include a DESCRIPTION section in
210 the POD for any Policy modules that you author. Thanks.
211
213 Perl::Critic::Violation overloads the "" operator to produce neat
214 little messages when evaluated in string context.
215
216 Formats are a combination of literal and escape characters similar to
217 the way "sprintf" works. If you want to know the specific formatting
218 capabilities, look at String::Format. Valid escape characters are:
219
220 %P Name of the Policy module.
221
222 %p Name of the Policy without the "Perl::Critic::Policy::" prefix.
223
224 %a The policy abstract.
225
226 %O List of supported policy parameters. Takes an option of a format
227 string for "to_formatted_string" in Perl::Critic::PolicyParameter.
228 For example, this can be used like "%{%n - %d\n}O" to get a list of
229 parameter names followed by their descriptions.
230
231 %U A message stating that the parameters for the policy are unknown if
232 "parameter_metadata_available()" returns false. Takes an option of
233 what the message should be, which defaults to "Cannot
234 programmatically discover what parameters this policy takes.". The
235 value of this option is interpolated in order to expand the
236 standard escape sequences ("\n", "\t", etc.).
237
238 %S The default severity level of the policy.
239
240 %s The current severity level of the policy.
241
242 %T The default themes for the policy.
243
244 %t The current themes for the policy.
245
246 %V The default maximum number of violations per document of the
247 policy.
248
249 %v The current maximum number of violations per document of the
250 policy.
251
253 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
254
256 Copyright (c) 2005-2011 Imaginative Software Systems. All rights
257 reserved.
258
259 This program is free software; you can redistribute it and/or modify it
260 under the same terms as Perl itself. The full text of this license can
261 be found in the LICENSE file included with this module.
262
263
264
265perl v5.32.0 2020-07-28 Perl::Critic::Policy(3)