Finding unused variables in a Perl script

A

Andreas Krause

I'm looking for a module or program to find all declared but unused variables
in a Perl script.

The best thing I could find yet is B::Xref, which shows (in different parts
of its output) the lines, where a variable is defined and the lines and
subroutines, where it is used.

To use this info, I would have to process the output file to scan for all
usages for each variable to find the unused ones. Very cumbersome...

Any recommendations for a better approach?

andi
 
G

Guest

: I'm looking for a module or program to find all declared but unused variables
: in a Perl script.

Ever tried perl -w?

Oliver.
 
M

Matt Garrish

: I'm looking for a module or program to find all declared but unused
variables
: in a Perl script.

Ever tried perl -w?

Yuck, are you secretly advocating making all your variables global?

use warnings;
$x;
my $y;

---

Useless use of a variable in void context at e:\scripts\undec.pl line 2.
Name "main::x" used only once: possible typo at e:\scripts\undec.pl line 2.


Matt
 
A

Andreas Krause

I'm looking for a module or program to find all declared but unused
Sure :)
Yuck, are you secretly advocating making all your variables global?

Not at all. I just want to clean some programs from old variables, which
are no longer used but still declared.
use warnings;
$x;
my $y;

Useless use of a variable in void context at e:\scripts\undec.pl line 2.
Name "main::x" used only once: possible typo at e:\scripts\undec.pl line 2.

This just gives me *undeclared but used*, not *unused but declared* variables.
If it would complain about $y, I would be happy...

andi
 
M

Matt Garrish

Andreas Krause said:
Sure :)


Not at all. I just want to clean some programs from old variables, which
are no longer used but still declared.


This just gives me *undeclared but used*, not *unused but declared*
variables.
If it would complain about $y, I would be happy...

That was my point. Lexically scoped variables will not be picked up by the
warnings pragma, only globals. What you want can't be done, to my knowledge
anyway.

Matt
 
D

Dr.Ruud

Matt Garrish schreef:
[find all declared but unused variables]
can't be done, to my knowledge anyway.

It may be possible for a source that doesn't use eval.
 
G

Guest

: > $x;
: > my $y;
: >
: > Useless use of a variable in void context at e:\scripts\undec.pl line 2.
: > Name "main::x" used only once: possible typo at e:\scripts\undec.pl line 2.

: This just gives me *undeclared but used*, not *unused but declared* variables.
: If it would complain about $y, I would be happy...

Sorry for the useless -w hint; I found out about this behaviour only after
I had posted my message and written a minimal test script.

Oliver.

PS: Perhaps a small varprof.pl utility would be nice.

1. Collect all variables, no matter whether declared or not
2. Count them. $count==1 should, in very naive thinking, indicate
that they appear only once.
3. Slightly more sophistication is obtained when variables are
tested for:
- do they receive assignments?
- do they appear on the RHS of assignments and equations?
- are they used in print and similar statements?
 
A

Anno Siegel

Dr.Ruud said:
Matt Garrish schreef:
[find all declared but unused variables]
can't be done, to my knowledge anyway.

It may be possible for a source that doesn't use eval.

That's a small set. Every program that "use"s something uses eval.

Anno
 
D

Dr.Ruud

Anno Siegel schreef:
Dr.Ruud:
Matt Garrish:
[find all declared but unused variables]
can't be done, to my knowledge anyway.

It may be possible for a source that doesn't use eval.

That's a small set. Every program that "use"s something uses eval.

I see some light between my "source" and your "program", but please
explain.

Does a "use strict" involve "eval"? I skimmed "use()" and "require()"
and "perlmod" and such, but didn't find a clue.
 
A

Anno Siegel

Dr.Ruud said:
Anno Siegel schreef:
Dr.Ruud:
Matt Garrish:
[find all declared but unused variables]
can't be done, to my knowledge anyway.

It may be possible for a source that doesn't use eval.

That's a small set. Every program that "use"s something uses eval.

I see some light between my "source" and your "program", but please
explain.

Does a "use strict" involve "eval"?

Yes, the first time it is used.
I skimmed "use()" and "require()"
and "perlmod" and such, but didn't find a clue.

All code interpretation in Perl is through eval. The source code is
read into a string and eval'ed.

Use of a variable can be hidden in a string which is to be eval'ed or
another source which is to be use'd. In both cases it's invisible to
a variable tracing program.

Anno
 
D

Dr.Ruud

Anno Siegel schreef:
Dr.Ruud:

Yes, the first time it is used.

But is that the same (main?) instance of eval() that interprets the
source that contains the use() (like with #include in C), or is it a new
(child?) eval()? A require() could just do some basic checks, and hand
over the (pre-parsed) source when successful.

The Subject is still "Finding unused variables", I still assume it is
practical.

All code interpretation in Perl is through eval. The source code is
read into a string and eval'ed.

Use of a variable can be hidden in a string which is to be eval'ed or
another source which is to be use'd. In both cases it's invisible to
a variable tracing program.

That's what I think I said, several postings ago.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top