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.


In reply to Re: Confusion in naming variables in subroutines by pemungkah
in thread Confusion in naming variables in subroutines by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.