warnings::unused - Produces warnings when unused variables are detected
This document describes warnings::unused version 0.06.
use warnings::unused; # installs the check routine as 'once' use warnings 'once'; # enables the check routine # or use warnings; # enables all warnings, # including this module sub foo{ my($x, $y) = @_; # WARN: Unused variable my $x return $y * 2; } sub bar{ my $x; # WARN our $y; # OK, it's global no warnings 'once'; my $unused; # OK, the unused warnings are disabled } # Or from commmand line: # perl -Mwarnings::unused=-global -e 'use Foo'
Note: The author no longer maintain this module. Consider Test::Vars if you detect unused vars as a unit test.
This pragmatic module extends lexical warnings to complain about unused variables.
It produces warnings when a my variable or state variable is unused aside from its declaration.
my
state
Given you write a subroutine like this:
sub f{ my($x, $y, $z) = @_; $y++; # used return sub{ $z }; # used }
The code above will be complained about $x, because it is used nowhere aside from its declaration.
$x
You should write f() like this:
f()
sub f{ my(undef, $y, $z) = @_; $y++; # used return sub{ $z }; # used }
Here, one will see the obvious intention to ignore the first argument of f().
The check routine works only at the compile time, so it affect nothing about the run time.
use warnings::unused
use warnings::unused -lexical
Installs the unused check routine with lexical scope.
unused
use warnings::unused -global
Installs the unused check routine with global scope, where this pragma checks all programs.
use/no warnings 'once';
Enables/Disables the unused warnings.
Note that the once warning is defined by default, so you can use it anywhare, even if warnings::unused is not loaded.
once
warnings::unused
This module cannot deal with cases where a variable appears only declared but correctly used. For example:
my $a = \my $used1; # only its delcaration but used my $b = \do{ my $used2 }; # ditto.
And more complicated (and silly) cases:
my $ref_to_foo_or_bar = \do{ if(g()){ my $foo; # used if g() returns true. } else{ my $bar; # used if g() returns false. } };
To avoid unexpected warnings, you can use the no warnings 'once' directive.
no warnings 'once'
Perl 5.8.1 or later, and a C compiler.
perllexwarn.
warnings::method.
B::Lint.
Perl::Critic.
Perl::Critic::Policy::Variables::ProhibitUnusedVariables.
Goro Fuji <gfuji(at)cpan.org>
Copyright (c) 2008-2009, Goro Fuji <gfuji(at)cpan.org>. Some rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install warnings::unused, copy and paste the appropriate command in to your terminal.
cpanm
cpanm warnings::unused
CPAN shell
perl -MCPAN -e shell install warnings::unused
For more information on module installation, please visit the detailed CPAN module installation guide.