http://qs321.pair.com?node_id=11100403


in reply to Why do I need a local variable in map function

s/// returns the number of substitutions made, so that's what that map block is returning. Although it's generally not considered good practice to modify the original list by modifying or assigning to $_ in a map block, the first version could be written as map { $_ = quotemeta($_) . '$'; s/\\\*/.*/g; $_ } to achieve the desired result. Alternatively, as of Perl 5.14, you can say s///r to have the regex return the modified string, e.g. map { my $x = quotemeta($_) . '$'; $x =~ s/\\\*/.*/gr }.

Update: Note that your second regex includes /g, while your first one does not; I've added it to both my examples.

Update 2: Or even just map { quotemeta($_).'$' =~ s/\\\*/.*/gr } or map { "\Q$_\E\$" =~ s/\\\*/.*/gr }

Replies are listed 'Best First'.
Re^2: Why do I need a local variable in map function
by Anonymous Monk on May 23, 2019 at 09:35 UTC
    Thanks for the perfect answer.