ambrus has asked for the wisdom of the Perl Monks concerning the following question:

Dear fellow monks, When I use local vars, but forget the my declaration, use strict warns me, but it does not complain about $a because of its role in sort (archaic form). But $a is quite a usual variable name (all single letters in [a-z] are). Is there a way then to capture undeclared $a's?

Replies are listed 'Best First'.
Re: use strict;$a=2
by dragonchild (Archbishop) on Oct 20, 2003 at 13:41 UTC
    Is there a way then to capture undeclared $a's?

    Yeah - code reviews. In other words, don't use $a as a variable. In fact, the only single-letter variables that should be used, IMHO, are $i, $j, $k, $x, $y, and $z. Those have the patina of traditional usage. And, even then, they shouldn't be used.

    If you're looping over some array, try and use foreach my $foo (@foos) instead of foreach my $i (0 .. $#foos) (unless you need to access the index instead of just the element).

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      What about $m and $n?

      :)

Re: use strict;$a=2
by liz (Monsignor) on Oct 20, 2003 at 13:32 UTC
    $a and $b are special, because you can use them inside a sort (without warnings, with strict active):
    use strict; my @list; @list = sort {$a <=> $b} @list;

    Offhand I wouldn't know a way to catch the use of undeclared lexical $a and $b, except maybe tieing global $a and $b to something that would bomb if a STORE or FETCH would be done. But that only can work if you don't use sort(), I would think.

    Liz

Re: use strict;$a=2
by edan (Curate) on Oct 20, 2003 at 13:38 UTC

    Whilst I can't answer your actual question, I feel compelled to point out that using single-letter variable names is really bad programming practice, since it makes your code hard to understand. The only time I use a single letter if within a sort function, or if I need a for(;;) loop for some strange reason, and I'll use $i for the iterator. Try to make the extra effort to give your variables meaningful names...

    --
    3dan

Re: use strict;$a=2
by Anonymous Monk on Oct 20, 2003 at 13:54 UTC
      Thanks. The topic is detailed there.
Re: use strict;$a=2
by ambrus (Abbot) on Oct 22, 2003 at 10:39 UTC
    I think it is right to use single-letters as locals. I belive that the best programming style is to always use short (50 lines max) and simple functions with no more than 7 locals. (Compare linux coding convention.) This way, it is no probem to use single-letter variables, as each function does only one thing so it is easy to understand and see how it works and what each variable does. (This is more true to C then Perl of course.)