Finding unused variables in a Perl script

Discussion in 'Perl Misc' started by Andreas Krause, Mar 16, 2006.

  1. 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
    Andreas Krause, Mar 16, 2006
    #1
    1. Advertising

  2. Andreas Krause

    Guest Guest

    Andreas Krause <> 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: -berlin.de
    Guest, Mar 19, 2006
    #2
    1. Advertising

  3. Andreas Krause

    Matt Garrish Guest

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


    Matt
    Matt Garrish, Mar 19, 2006
    #3
  4. >>> 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::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
    Andreas Krause, Mar 20, 2006
    #4
  5. Andreas Krause

    Matt Garrish Guest

    "Andreas Krause" <> wrote in message
    news:dvlrca$7ev$...
    >>>> 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::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...
    >


    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
    Matt Garrish, Mar 20, 2006
    #5
  6. Andreas Krause

    Dr.Ruud Guest

    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,/'
    Dr.Ruud, Mar 20, 2006
    #6
  7. Andreas Krause

    Guest Guest

    Andreas Krause <> wrote:
    : > $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?


    --
    Dr. Oliver Corff e-mail: -berlin.de
    Guest, Mar 20, 2006
    #7
  8. Andreas Krause

    Anno Siegel Guest

    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.
    Anno Siegel, Apr 19, 2006
    #8
  9. Andreas Krause

    Dr.Ruud Guest

    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."
    Dr.Ruud, Apr 19, 2006
    #9
  10. Andreas Krause

    Anno Siegel Guest

    Dr.Ruud <> 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.
    Anno Siegel, Apr 19, 2006
    #10
  11. Andreas Krause

    Dr.Ruud Guest

    Anno Siegel schreef:
    > Dr.Ruud:


    >> Does a "use strict" involve "eval"?

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


    *yawn* <g>


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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 19, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,794
  2. Noah

    Finding unused perl code

    Noah, Apr 2, 2004, in forum: Perl
    Replies:
    0
    Views:
    1,197
  3. Rob T

    Unused Variables

    Rob T, Nov 19, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    5,248
    Rob T
    Nov 19, 2004
  4. John Harrison
    Replies:
    4
    Views:
    370
    Ali Cehreli
    Jul 17, 2004
  5. Chris
    Replies:
    5
    Views:
    2,222
Loading...

Share This Page