Tom Wyant > Perl-Critic-Policy-Variables-ProhibitUnusedVarsStricter > Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter



Annotate this POD

View/Report Bugs
Module Version: 0.003   Source  


Perl::Critic::Policy::Variables::ProhibitUnusedVarsStricter - Don't ask for storage you don't need.


This Policy is stand-alone, and is not part of the core Perl::Critic.


Unused variables clutter code and require the reader to do mental bookkeeping to figure out if the variable is actually used or not.

Right now, this only looks for lexical variables which are unused other than in the statement that declares them.

    my $x;          # not ok, assuming no other appearances.
    my @y = ();     # not ok, assuming no other appearances.
    our $z;         # ok, global.
    local $w;       # ok, global.

This policy is a variant on the core policy Perl::Critic::Policy::Variables::ProhibitUnusedVariables which attempts to be more strict in its checking of whether a variable is used. The specific differences are:

* An attempt is made to take into account the scope of the declaration.

* An attempt is made to find variables which are interpolated into double-quotish strings (including regexes) and here documents.

* An attempt is made to find variables which are used in regular expression (?{...}) and (??{...}) constructions, and in the replacement portion of s/.../.../e.

This policy intentionally does not report variables as unused if the code takes a reference to the variable, even if it is otherwise unused. For example things like

    \( my $foo = 'bar' )
    \do{ my $foo => 'bar' }

will not be reported as a violation even if $foo is otherwise unused. The reason is that this is an idiom for making a reference to a mutable string when all you have is an immutable string. This policy does not check to see if anything is done with the reference.

This policy also does not detect unused variables declared inside various odd corners such as

    "@{[ ... ]}"
    ( $foo, my $bar ) = ( 1, 2 )

Most of these are because the PPI parse of the original document does not include the declarations. The list assignment is missed because PPI does not parse it as containing a PPI::Statement::Variable. However, variables used inside such construction will be detected.


This policy supports the following configuration items:


By default, this policy prohibits unused subroutine arguments -- that is, unused variables on the right-hand side of such simple assignments as

    my ( $foo ) = @_;
    my $bar     = shift;
    my $baz     = shift @_;
    my $burfle  = $_[0];

If you wish to allow unused variables in this case, you can add a block like this to your .perlcriticrc file:

    allow_unused_subroutine_arguments = 1


By default, this policy allows otherwise-unused variables if the code takes a reference to the variable when it is created. If you wish to declare a violation in this case, you can add a block like this to your .perlcriticrc file:

    prohibit_reference_only_variables = 1


You may wish to allow variables to be unused when computed in certain ways. For example, you might want to allow place-holder variables in a list computed by stat() or unpack(). Or you may be doing end-of-scope detection with something like my $foo = Scope::Guard->new( \&end_of_scope ). To ignore all these, you can add a block like this to your .perlcriticrc file:

    allow_if_computed_by = stat unpack Scope::Guard

This property takes as its value a whitespace-delimited list of class or subroutine names. Nothing complex is done to implement this -- the policy simply looks at the first word after the equals sign, if any.


Thomas R. Wyant, III wyant at cpan dot org


Copyright (C) 2012-2014 Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For more details, see the full text of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.

syntax highlighting: