Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by ikegami (Pope)
on Jul 09, 2020 at 03:03 UTC ( #11119060=note: print w/replies, xml ) Need Help??


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 Im 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 $_.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11119060]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2021-01-23 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?