[Israel.pm] catching $a, $b unnecessary my and maybe other things

Mikhael Goikhman migo at homemail.com
Sun Jun 6 14:03:19 PDT 2004

On 06 Jun 2004 16:37:22 -0200, Gabor Szabo wrote:
> There are two issues that might or might not relate to each other.

I don't think there is any relation between these two issues.

> sub f {
> 	my $x = shift;
> 	my $y = "unknown";
> 	if ($x < 0) {
> 		$y = "negative";
> 	} elsif ($x > 0) {
> 		my $y = "positive";
> 	}
> 	return $y;
> }
> 2)
> 		my $y = "positive";
> within the elsif is actually a bug.

This is a bug in the program, just like a typo in the string value would
be such bug too. In both cases the code is valid, so "use warnings" can't
help here.

> In this case it is quite obvious to a reader that the $y within this
> block is used only once. Can such thing be caught programmaticaly ?

There is a good reason why   my $var = "value";   never produces a "used
only once" warning. Only undeclared symbol table variables are covered by
this warning. Declared "my" and "our" variables are excluded not to annoy
an otherwise perfectly working code.

Think about this and many other similar examples:

	my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =

And suppose you only use $mon and $year in your code. You really don't
want to get seven used-only-once warnings here. Another common example
is "my $self = shift;" when the method does not really use the object.

BTW, if we speak about this example, of course you may use something like
the following starting with perl 5.004:

	my (undef, undef, undef, undef, $mday, $mon) = localtime();

but this is less readable in my opinion.


perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'

More information about the Perl mailing list