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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|