in reply to Perl::Critic says don't modify $_ in list functions and other things

Here's the problem with your approach:

my @a = qw( a b c ); my @b = map { $_ = uc($_); $_ } @a; say "@a"; # A B C <-- You clobbered @a!!! say "@b"; # A B C

Use List::MoreUtils's apply instead of map.

my @a = qw( a b c ); my @b = apply { $_ = uc($_);} @a; say "@a"; # a b c say "@b"; # A B C

Replies are listed 'Best First'.
Re^2: Perl::Critic says don't modify $_ in list functions and other things
by Your Mother (Archbishop) on Jul 09, 2020 at 08:40 UTC

    I hope I’m not being obtuse and about to embarrass myself but…

    # Why this? my @b = apply { $_ = uc($_);} @a; # Instead of this, or a more verbose version of it? my @b = map uc, @a;

      I'll take the liberty of answering for ikegami and guess that
          my @b = apply { $_ = uc($_);} @a;
      is just a quick example of the use of apply put together to be consistent with the preceding code
          my @b = map { $_ = uc($_); $_ } @a;
      which was itself just a quick example put together to demonstrate a particular problem. I'd be very surprised if ikegami suggested it | either for production code.


      Give a man a fish:  <%-{-{-{-<

      Of course, but the OP isn't actually using $_ = uc($_), and your solution wouldn't help the OP. $_ = uc($_) is just a placeholder for a large block of code that possibly modifies $_.