It is, in my humble opinion, best practice to collect up the incoming parameters of a subroutine as soon as humanly possible so while reading into the function/method, it's easy to see what is what (the length of the variable name isn't my normal style, I've just made it readable for the example):

sub oddDigitSum { my @signed_integers_to_work_on = @_; my @ans; for (@signed_integers_to_work_on) { ...

To further, I always check my input parameters, even if a built-in will throw an exception on them anyway (the regex isn't always my normal way of checking, but it's good as an illustrative example):

sub oddDigitSum { my @supplied_signed_integers = @_; my @ans; for my $signed_int (@supplied_signed_integers) { if ($signed_int !~ /^-?\d+$/) { die "Parameter '$signed_int' isn't a signed integer"; ...

For me, I use the implicit $_ variable when my loop is exceedingly short in lines of code, or the complexity of the loop is only one level deep and very clear to understand. Your for() loop is a good example of where I'd use $_ and not bother declaring and defining a named scoped variable for each value being iterated as I did in an above example. If there was even one more operation in that for() loop though, I'd break it up with named variables, just so its easier to a future reader (often me) which operation modified the list.


In reply to Re: Newbie question by stevieb
in thread Newbie question by oldB51

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.