my @files = map {chomp;s/^\s+//; s/\+s$//; $_;} <$FILES>;

As always, I'd take the PBP advisories with a grain of salt. The list you are passing to map results from an exhaustive read from the file-handle <$FILES>, which happens to be built only there and now; how could "modifying the input list" have any side-effects here?

We now could ask ourselves about modifying the arguments to map by altering $_: "is it useful?" and "is it documented?" and "can it be avoided?". The answer is "yes" for all three questions. Here's the relevant snippet form perlfunc:

Note that $_ is an alias to the list value, so it can be used to modify the elements of the LIST. While this is useful and supported, it can cause bizarre results if the elements of LIST are not variables.

Then we could proceed to discuss whether that behavior is a feature of map or a bug by implementation glitch, labeled as a feature (which could be the case), and whether perl's behavior is consistent (which here is, I think, looking at for); but hasn't that been done many times before?

I can see no bad practice in rewriting a hash and a sorted list of its keys in one pass:

@sorted = sort keys %hash; %hash = map { my $v = $hash{$_}; s/ /_/g; $_, $v } @sorted;

But maybe that's just me. Maybe Best Practice would be programming perl as if it where python... ;-)


In reply to Re: PerlCritic, $_ and map by shmem
in thread PerlCritic, $_ and map by chexmix

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.