in reply to Confusion in naming variables in subroutines

Using the same names is perfectly acceptable, as long as the names make sense in the context where they're used. I'd actually argue against $temp_length: is this a temporary variable that is needed so that the subroutine can perform its algorithm, and we'll be using the $length in the outer scope as well, or is it something else?

I personally just pass all the variables I need in, assign @_ to a list of variables on the first line of the subroutine, and use the names that seem most appropriate to describe what I'm doing. This may mean that in, for instance, a module that munges strings, that I have $string in several different subroutines.

Whatever keeps the algorithm clear in the reader's mind is what you want to use.

  • Comment on Re: Confusion in naming variables in subroutines

Replies are listed 'Best First'.
Re^2: Confusion in naming variables in subroutines
by rkrieger (Friar) on Jan 24, 2007 at 11:51 UTC

    This is more a side note, I'll admit.
    If you expect the number of arguments to grow, you may want to consider passing those arguments in the form of a hash and call routines with named parameters.

    That way, you will not be bitten by having to remember the exact order in which you need to pass arguments. I find it's easier on others using my code.