in reply to Warning about unused lexical variables

There's been only one followup thus far in clpmisc (that I can see):

You could use perl's backend to dump the lexical variables and the line numbers on which they appear:

$ nl -ba foo.pl 1 #!/usr/bin/perl 2 use strict; 3 use warnings; 4 5 my $x; 6 my $y; 7 8 $y = 42; 9 print "$y\n"; 10 $ perl -MO=Xref foo.pl File foo.pl Subroutine (definitions) Package Internals &HvREHASH s0 &SvREADONLY s0 &SvREFCNT s0 &hash_seed s0 &hv_clear_placeholders s0 &rehash_seed s0 Package PerlIO &get_layers s0 Package Regexp &DESTROY s0 Package UNIVERSAL &VERSION s0 &can s0 &isa s0 Subroutine (main) Package (lexical) $x i5 $y i6, 8, 9 foo.pl syntax OK

This indicates that $x only appears on line 5.

Replies are listed 'Best First'.
Re^2: Warning about unused lexical variables
by blazar (Canon) on Sep 07, 2007 at 10:30 UTC
    There's been only one followup thus far in clpmisc (that I can see):

    There have been other two:

    Jürgen Exner

    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.

    Jim Gibson

    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.

    But both $x and $y appear in two separate instances within their scope. Contrast that with:

    my ($x, $y) = (1,2); if (<condition>) { call_my_sub($x) } else { call_my_sub($z) }

    $y and $z both only appear once, so it looks like there has been a typo. That is the type of error that could be caught with an analysis of lexical variables.

    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.

    A dynamic analysis is not needed.


    Update: one last entry...

    Peter J. Holzer

    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) }