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

Let's say I have the following code:
use strict; while (my $something) { my ($a, $b); testFunc($a, $b); } sub testFunc { my ($a, $b) = @_; ... do something ... }
My question is this: Is it a bad practice to use $a and $b in both the subroutine, and for sending data to the subroutine? I think it would be OK because they are in different scopes. Does it become a matter of personal preference, or is there an underlying reason this is a bad idea?

Thanks

Replies are listed 'Best First'.
(jeffa) Re: Scalars within Subroutines (scope)
by jeffa (Bishop) on Jul 19, 2002 at 15:39 UTC
    The reason it is bad is not because you use the same names, but because you are using the special variables $a and $b. Now, there is really no harm, but you still should not use these variables as lexical variables. They are reserved for sort.

    There is nothing wrong with using the same names, however, because they are indeed in different scopes.

    Update: i should add that when you declare a subroutine below declared variables, those variables are visible to the scope of that sub:

    my $foo = 'bar'; baz(); sub baz { print $foo, "\n"; print $qux, "\n"; } my $qux = 'bar';
    Try moving the declaration of $qux above baz() and see what happens after running that code. However, note that when you declare a variable named $foo or $qux inside sub baz with my, they are different variables.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Scalars within Subroutines (scope)
by simeon2000 (Monk) on Jul 19, 2002 at 15:41 UTC
    Personally, I wouldn't use $a and $b because they are the default variables to compare in sort blocks.

    But as far as using the same names for variables in function1 and function2, as long as you properly scope them, there is no problem whatsoever.

    Technically, if your chief concern is clobbering your variables defined earlier, as long as you use warnings (as we all do, right?) with "my" scoping, perl will give you a heads up if you DO attempt to shoot yourself in the foot in this manner:)

Re: Scalars within Subroutines (scope)
by broquaint (Abbot) on Jul 19, 2002 at 15:44 UTC
    Using variables of the same name in different scopes is fine as they are *new* variables within that scope (if they're lexical of course) e.g
    my($foo,$bar) = qw(one two); print "($foo, $bar)\n"; { # new scope, new variables my($foo,$bar) = qw(ichi ni); print "($foo, $bar)\n"; } print "($foo, $bar)\n"; __output__ (one, two) (ichi, ni) (one, two)
    But I'd highly recommend against using $a and $b as variable names as they are special to sort().
    HTH

    _________
    broquaint

Re: Scalars within Subroutines (scope)
by Anonymous Monk on Jul 19, 2002 at 17:58 UTC
    Thanks for the information. The $a $b was my mistake, I guess I should pay more attention to what I am doing :). Does anyone have a preferred way of doing things in this example?

    Thanks