Perl::Critic::DynamicPolicy - Base class for dynamic Policies


Perl::Critic::DynamicPolicy is intended to be used as a base class for Perl::Critic::Policy modules that wish to compile and/or execute the code that is being analyzed.

Policies that inherit from Perl::Critic::DynamicPolicy will fork the process each time the violates method is called. The child process is then free to compile the code and do other mischievous things without corrupting the symbol table of the parent process. When the analysis is complete, the child serializes any Perl::Critic::Violation objects that were created and sends them back to the parent across a pipe.

Any Policy that inherits from Perl::Critic::DynamicPolicy will also be marked as "unsafe" and is usually ignored by both Perl::Critic and perlcritic. To use a Policy that inherits from Perl::Critic::DynamicPolicy, you must set the -allow-unsafe switch in the Perl::Critic constructor or on the perlcritic command line.

In every other way, a Perl::Critic::DynamicPolicy behaves just like an ordinary Perl::Critic::Policy. For Policy authors, the main difference is that you must override the violates_dynamic method instead of the violates method. See Perl::Critic::DEVELOPER for a discussion of the other aspects of creating new Policies.


This list of methods is not exhaustive. It only covers the methods that are uniquely relevant to Perl::Critic::DynamicPolicy subclasses. See Perl::Critic::Policy and Perl::Critic::DEVELOPER for documentation about the other methods shared by all Policies.

violates( $doc, $elem )

In a typical Perl::Critic::Policy subclass, you would override the violates method to do whatever code analysis you want. But with Perl::Critic::DynamicPolicy, this method has already been overridden to perform the necessary pipe and fork operations that I described above. So instead, you need to override the violates_dyanmic method.

violates_dynamic( $doc, $elem )

Given a PPI::Element and a PPI::Document, returns one or more Perl::Critic::Violation objects if the $elem or <$doc> violates this Policy. If there are no violations, then it returns an empty list. This method will be called in a child process, so you can compile $doc without interfering with the parent process.

violates_dynamic is an abstract method and it will abort if you attempt to invoke it directly. It is the heart of your Perl::Critic::DynamicPolicy modules, and your subclass must override this method.


Returns false. Any Policy derived from this module is presumed to be unsafe. Perl::Critic and perlcritic users can only load Policies derived from this module if they use the -allow-unsafe switch.


Jeffrey Ryan Thalhammer <>


Copyright (c) 2007 Jeffrey Ryan Thalhammer. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.