Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^12: Immediately writing the results of search-and-replace

by AnomalousMonk (Archbishop)
on Aug 12, 2022 at 07:49 UTC ( [id://11146115]=note: print w/replies, xml ) Need Help??


in reply to Re^11: Immediately writing the results of search-and-replace
in thread Immediately writing the results of search-and-replace

my ($old, $new, $reset) = map "\e[${_}m", 91, 92, 0;

... doesn't appear to be a common Perl trick ...

It's a very common (and very valuable) Perl "trick". See map and its cousin grep, and see List::Util for many useful functions inspired by the basic behavior of these two built-ins (i.e., iterate over a list and produce another list). As for other examples of the use of this type of function... well, keep your eyes open and I think you'll start to see quite a few, especially in code on this and similar websites. See also Map: The Basics in Tutorials.

Update: Per a /msg from hippo, double-quote "trick" to emphasize that it's not really a trick, but common usage. Also, another minor wording change for clarity.


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

Replies are listed 'Best First'.
Re^13: Immediately writing the results of search-and-replace
by elemkeh (Acolyte) on Aug 12, 2022 at 13:40 UTC
    Thank you. It's not the behavior of map or grep that was/is mysterious to me, but the particular use of "\e[${_}m", which looks like some manner of regex (which it isn't), and I couldn't figure out what it was supposed to be doing to the numbers. I got hung up on the presence of an open square bracket but no closing square bracket, and the ${_} which looks like some sort of default variable distinct from $_, but for which I could find no definition. If I understand right from afoken's comment, then ${_} substitutes in the values from the provided list, which is what I would have expected $_ to do in a map. Is there anywhere the ${_} form is documented so I could read up on it? I had read the various docs you pointed to and didn't find it there. If it is not a form of its own, why use the curly braces instead of just $_?

      $_ and ${_} are the same. You are free to enclose the name of any variable in curly brackets after its sigil. In this case they are required because the use is to append an m immediately after the $_ and if you were to write $_m then that would look for a scalar variable with name _m which doesn't exist. eg:

      $ perl -wE 'say "$_m" for (1 .. 5)' Use of uninitialized value $_m in string at -e line 1. Use of uninitialized value $_m in string at -e line 1. Use of uninitialized value $_m in string at -e line 1. Use of uninitialized value $_m in string at -e line 1. Use of uninitialized value $_m in string at -e line 1. $ perl -wE 'say "${_}m" for (1 .. 5)' 1m 2m 3m 4m 5m $

      HTH.


      🦛

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found