in reply to Localize $_ in grep

Anytime you substitute inside of a grep or map block, you have to do this:
my @m_h = grep {local($_);s/tw/tr/g; /^t.*/ig;$_} @foo;
That is, put the variable you are substituted as the last item in the block. Otherwise, you return the return of the substitute, which is usually 1 or the number of matches (i always forget :/).

In your case, you don't want grep - you want map (actually, you do want grep ... see 2nd update below):

my @m_h = map {s/tw/tr/g;$_} @foo;
Notice that you don't need local.

UPDATE:
Oops, this still changes the contents of @foo ... hmmm, how about a simple copy?

my @m_h = @foo; s/tw/tr/g for @m_h;
Should be all you need. No?

UPDATE 2:
After looking at ctilmes reply, how about this:

my @m_h = grep /^t/i, @foo; s/tw/tr/g for @m_h;

Hope this helps. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: Localize $_ in grep
by Anonymous Monk on Apr 19, 2012 at 04:00 UTC

    Since perl 5.13.2 s/// and tr/// support the /r flag, which returns a copy instead of the number of substitutions. The /r feature is called Non-destructive substitution

    $ perl -MData::Dump -e " @f = 1..4; dd [ map { s/^/F/r; } @f ]; dd\@f; + " ["F1" .. "F4"] [1 .. 4]