Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Finding unused variables in a Perl script

Reply
Thread Tools

Finding unused variables in a Perl script

 
 
Andreas Krause
Guest
Posts: n/a
 
      03-16-2006
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
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      03-19-2006
Andreas Krause <(E-Mail Removed)> wrote:
: I'm looking for a module or program to find all declared but unused variables
: in a Perl script.

Ever tried perl -w?

Oliver.

--
Dr. Oliver Corff e-mail: http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      03-19-2006

<(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)...
> Andreas Krause <(E-Mail Removed)> wrote:
> : 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:" used only once: possible typo at e:\scripts\undec.pl line 2.


Matt


 
Reply With Quote
 
Andreas Krause
Guest
Posts: n/a
 
      03-20-2006
>>> I'm looking for a module or program to find all declared but unused
>>> variables in a Perl script.

>>
>>Ever tried perl -w?


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:" 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
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      03-20-2006

"Andreas Krause" <(E-Mail Removed)> wrote in message
news:dvlrca$7ev$(E-Mail Removed)...
>>>> I'm looking for a module or program to find all declared but unused
>>>> variables in a Perl script.
>>>
>>>Ever tried perl -w?

>
> 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:" 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...
>


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


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-20-2006
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.

--
Affijn, Ruud

"Gewoon is een tijger."
echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'
 
Reply With Quote
 
Guest
Posts: n/a
 
      03-20-2006
Andreas Krause <(E-Mail Removed)> wrote:
: > $x;
: > my $y;
: >
: > Useless use of a variable in void context at e:\scripts\undec.pl line 2.
: > Name "main:" 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?


--
Dr. Oliver Corff e-mail: (E-Mail Removed)-berlin.de
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-19-2006
Dr.Ruud <rvtol+"`ls -al .`"@isolution.nl> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-19-2006
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.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-19-2006
Dr.Ruud <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding unused public methods and classes Chris Java 5 10-30-2006 03:21 PM
Unused Variables Rob T ASP .Net 4 11-19-2004 08:25 PM
OT: Utility for finding out unused members John Harrison C++ 4 07-16-2004 11:54 PM
Finding unused perl code Noah Perl 0 04-02-2004 08:44 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments