Warning about unused lexical variables

Discussion in 'Perl Misc' started by Peter J. Holzer, Sep 4, 2007.

  1. Occasionally I notice a lexical variable sticking around which isn't
    used any more and can/should be deleted, like in this (stupid) example:

    #!/usr/bin/perl
    use warnings;
    use strict;

    my $x;
    my $y;

    $y = 42;
    print "$y\n";
    __END__

    This can be made slightly more complex by changing "my $x" to

    my $x = compute_some_value();

    where $x is assigned a value which is subsequently never used.

    It would be nice if perl could warn about lexical variables which are
    never used in their scope after their initialization. Does anybody else
    find this useful, and if so, is there a reason (besides "life is short")
    why it hasn't been implemented?

    hp

    --
    _ | Peter J. Holzer | I know I'd be respectful of a pirate
    |_|_) | Sysadmin WSR | with an emu on his shoulder.
    | | | |
    __/ | http://www.hjp.at/ | -- Sam in "Freefall"
     
    Peter J. Holzer, Sep 4, 2007
    #1
    1. Advertising

  2. On Tue, 4 Sep 2007 12:25:30 +0200, "Peter J. Holzer"
    <> wrote:

    >It would be nice if perl could warn about lexical variables which are
    >never used in their scope after their initialization. Does anybody else
    >find this useful, and if so, is there a reason (besides "life is short")
    >why it hasn't been implemented?


    I don't have the slightest idea about how to answer your question, but
    I duplicated at <http://perlmonks.org/?node_id=636990>. I'll update
    this thread as replies get posted there.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 4, 2007
    #2
    1. Advertising

  3. On Tue, 04 Sep 2007 19:48:05 +0200, Michele Dondi
    <> wrote:

    >I don't have the slightest idea about how to answer your question, but
    >I duplicated at <http://perlmonks.org/?node_id=636990>. I'll update
    >this thread as replies get posted there.


    Here is what has been said:


    Re: Warning about unused lexical variables
    by toolic on Sep 04, 2007 at 20:11 GMT-2


    I also find this useful.

    A few weeks ago, something prompted me to run a Super Search on
    "unused variable". I did not find the answer to this question, but the
    most relevant node seemed to be "Devel::GC::Helper and unused
    variables" (http://perlmonks.org/?node_id=602205).

    Hopefully, a much wiser monk can enlighten us.

    ----------------------------------------------------------------------

    Re: Warning about unused lexical variables
    by sgt on Sep 04, 2007 at 20:27 GMT-2


    B::Xref o B::Lint (with some plugin) can help I guess

    ----------------------------------------------------------------------

    Re: Warning about unused lexical variables
    by kyle on Sep 04, 2007 at 21:19 GMT-2


    My (small) problem with this would be that the "unused" variable might
    still serve some purpose. Consider a compute_some_value() like one of
    these:

    sub pay_attention {
    die 'void context' if ! defined wantarray;
    return rand;
    }
    sub side_effect {
    print "I am happy.\n";
    return;
    }
    sub wait_for_destruction {
    return OnDestroyDo->new( \&side_effect );
    }

    In case that last one is not clear, the idea is that it returns an
    object that will perform some action when it is destroyed.

    Arguably a warning is still warranted in even these cases, but it
    might not be easy to tell when an assigned value is really
    superfluous.

    ----------------------------------------------------------------------

    Re^2: Warning about unused lexical variables
    by xdg on Sep 04, 2007 at 22:52 GMT-2


    > the "unused" variable might still serve some purpose


    Another example would be "anonymous" scalar references.

    my $scalar_ref = \(my $s);

    Or would taking a reference count as "using" it?

    ----------------------------------------------------------------------

    Re^2: Warning about unused lexical variables
    by bart on Sep 05, 2007 at 13:06 GMT-2


    That's right. Check this article on perl.com on what this could be
    used for: "Better Code Through Destruction"
    (http://www.perl.com/pub/a/2007/06/07/better-code-through-destruction.html).

    ----------------------------------------------------------------------

    Re: Warning about unused lexical variables
    by Rhandom on Sep 04, 2007 at 22:20 GMT-2


    At first glance it sounds like a great idea. And it is to a certain
    extent. But there is an existing system in Perl that does that very
    thing, although only with global variables.

    [paul@paul-laptop nqp]$ perl -we '$x = 1'
    Name "main::x" used only once: possible typo at -e line 1.

    Speaking from experience, this warning has never helped me. It has
    done the opposite. It has always been a hinderance. It has forced me
    to write things similar to the following:

    my $value = $SOME_PKG::SOME_VAL
    || $SOME_PKG::SOME_VAL; # warn clean

    Arguably, I should have a function in SOME_PKG that returns the value
    of $SOME_VAL. But there are many existing modules that don't provide
    accessors.

    Use strict catches all of my typos. A warning of this sort never has.

    Still - I could see a use for it, but I think I'd want it as an
    extra-extra pragma:

    use warnings qw(single_use_vars);

    ----------------------------------------------------------------------


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 5, 2007
    #3
  4. On Wed, 05 Sep 2007 18:53:28 +0200, Michele Dondi
    <> wrote:

    >Here is what has been said:


    Update:


    Re: Warning about unused lexical variables
    by bduggan on Sep 06, 2007 at 19:06 GMT-2


    I was looking for this recently, and found it mentioned in
    Perl::Critic::TODO (TBD::VariableNotUsed).


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 6, 2007
    #4
  5. Peter J. Holzer wrote:
    > It would be nice if perl could warn about lexical variables which are
    > never used in their scope after their initialization. Does anybody
    > else find this useful, and if so, is there a reason (besides "life is
    > short") why it hasn't been implemented?


    Useful? Yes.
    Easy to implement? No.

    Example:

    my ($x, $y) = (1,2);
    if (<some complex condition depending on the environment>) {
    call_my_sub($x)
    } else {
    call_my_sub($y)
    }

    In this example either $x or $y remains unused.
    However a static analysis at compile time cannot detect this.

    And neither can a dyamic analysis during runtime because that complex
    condition may evaluate to false only in an odd one-in-a-million situation.

    jue
     
    Jürgen Exner, Sep 6, 2007
    #5
  6. On 2007-09-06 18:58, Jürgen Exner <> wrote:
    > Peter J. Holzer wrote:
    >> It would be nice if perl could warn about lexical variables which are
    >> never used in their scope after their initialization. Does anybody
    >> else find this useful, and if so, is there a reason (besides "life is
    >> short") why it hasn't been implemented?

    >
    > Useful? Yes.
    > Easy to implement? No.


    I don't think it would be particularly hard to implement. That's a
    rather common feature in compilers which generate machine code (IIRC
    it's a byproduct of register allocation). But I don't know how expensive
    it is - since perl compiles the source code every time it is run, we
    want to avoid algorithms which can potentially take a long time.


    > Example:
    >
    > my ($x, $y) = (1,2);
    > if (<some complex condition depending on the environment>) {
    > call_my_sub($x)
    > } else {
    > call_my_sub($y)
    > }
    >
    > In this example either $x or $y remains unused.


    In any particular run, yes. But both branches are possible, so you can
    eliminate neither $x nor $y. So that code is ok, although it would
    probably be cleaner to rearrange it as:

    if (<some complex condition depending on the environment>) {
    my $x = 1;
    call_my_sub($x)
    } else {
    my $y = 2;
    call_my_sub($y)
    }

    hp


    --
    _ | Peter J. Holzer | I know I'd be respectful of a pirate
    |_|_) | Sysadmin WSR | with an emu on his shoulder.
    | | | |
    __/ | http://www.hjp.at/ | -- Sam in "Freefall"
     
    Peter J. Holzer, Sep 7, 2007
    #6
    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. Joakim Hove

    Lexical variables - speed penalty?

    Joakim Hove, Oct 7, 2004, in forum: C Programming
    Replies:
    3
    Views:
    263
    CBFalconer
    Oct 7, 2004
  2. Antoninus Twink

    Re: best way to suppress "unused" warning?

    Antoninus Twink, Apr 25, 2008, in forum: C Programming
    Replies:
    6
    Views:
    334
    Ian Collins
    Apr 29, 2008
  3. Replies:
    0
    Views:
    459
  4. Daniel Fetchinson

    warning for unused packages/modules/objects

    Daniel Fetchinson, Jun 27, 2008, in forum: Python
    Replies:
    0
    Views:
    250
    Daniel Fetchinson
    Jun 27, 2008
  5. Rui Maciel
    Replies:
    14
    Views:
    2,539
    debra h
    Oct 1, 2010
Loading...

Share This Page