As an example, I have used the absolute value function abs but I could have used abs($_). Which is best? Does it matter?

Specifically on this point: others have pointed out that there is no difference functionally, so that leaves style and clarity to drive the choice.

In my case, I hardly ever use the abs function (despite the fact that most of the code I write is maths-related). Because of that, I would choose always to write $abs = abs($_); rather than $abs = abs; - for the latter, I'd have to stop and think a few deciseconds longer any time I wrote it or read it.

On the other hand, for functions that default to $_ that I use commonly, I'm much more likely to prefer the short form.

On the gripping hand, I try to avoid over-reliance on $_. Normal for loops are almost always improved by naming the variable. I mostly have $_ in "right-to-left" code - maps, greps, modifier-for - and there, again, I look to name a variable if I'm going to be using it several times:

# different $_ on left and right, but I find this perfectly clear $sum += $_ for /(\d)/g; # or my @saints = grep $_->is_saint, @users; # or my @users = map { my $name = $_; is_god($name) ? find_with_sacrifice($name, $spare_user) : is_saint($name) ? find_with_relic($name, $relic) : $spare_user ? find_user($name) : do { # save for later $spare_user = find_user($name); () } } @names;

In reply to Re: Newbie question by hv
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.