${} is a syntactic construct that dereferences a scalar reference. Its inside can be any arbitrarily complex expression. \ is used to make references, so \$_ returns a reference to $_. See perlreftut.

So in your code, $1 is assigned to $_ (because $1 cannot be modified but $_ can), then s|\s*\=\s*|=|gsi removes the spaces around any equals signs in $_, and finally a reference to $_ is returned which immediately gets dereferenced to insert its value.

This is quite obfuscated, particularly due to the massively annoying choice of | as the regex delimiter. I wonder why the original programmer used /e at all. Doing something like ${ $var = do_something(), \$var } is a common trick to embed code in contexts where it would not otherwise be possible – f.ex., you can use this to put short snippets of code into heredocs. But with /e, this is completely unnecessary, you can simply write the above code like so:

$x =~ s{ < ([^>]*) > }{ $_ = $1; s{ \s* \= \s* }{=}gsix; "<$_>" }gisex +;

Note that I threw in an /x in both cases, so that I could use whitespace to make the patterns more readable. See perlretut (particularly, Building a regexep).

Makeshifts last the longest.


In reply to Re: Need explanation: what \$_ and ${} do by Aristotle
in thread Need explanation: what \$_ and ${} do by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.