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

After looking around for a while and poking at the code myself, I still don't understand just what is going on with this line:
my ($old, $new, $reset) = map "\e[${_}m", 91, 92, 0;
Beyond the fact that this is variable assignment, I don't know what to make of it. It doesn't appear to be a common Perl trick, or if it is I have yet to see another example of it to compare against. Can anyone explain? Thank you.

Replies are listed 'Best First'.
Re^12: Immediately writing the results of search-and-replace
by afoken (Chancellor) on Aug 12, 2022 at 07:12 UTC
    my ($old, $new, $reset) = map "\e[${_}m", 91, 92, 0;

    Left-hand side: Three variables, $old, $new, $reset.

    Right-hand side: A map expression iterating over the three values 91, 92, and 0. map evaluates the expression "\e[${_}m" for each of the three values, resulting in the list "\e[91m", "\e[92m", "\e[0m".

    Finally, the three element RHS list is assigned to the LHS list of variables.

    The values are ANSI terminal escape sequences for setting text attributes, see https://en.wikipedia.org/wiki/ANSI_escape_code#SGR. 91 selects bright red foreground, 92 selects bright green foreground, 0 resets back to defaults.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^12: Immediately writing the results of search-and-replace
by AnomalousMonk (Archbishop) on Aug 12, 2022 at 07:49 UTC
    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:  <%-{-{-{-<

      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.


        🦛