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