Wow.

You are matching (first part of the s///egsi):

<([^>]*)>
, which means "the content of every tag", i.e. the act aid = "s". Due to the round parentheses, what you match is put inside $1. Moreover, due to the gsi modifiers, the substitution will be applied to the whole string (g), without caring of case (i) and trating newlines as any other character (s).

The interesting part comes with the "e" modifier. This tells Perl that the "replacement" part of the substitution is an expression, not a bunch of characters. The espression is the following:

'<' . ${($_=$1)=~s|\s*\=\s*|=|gsi,\$_} . '>'
that is the concatenation of three parts: the enclosing angle brackets and the "content". When you use the ${ EXPRESSION } construct, you are de-referencing a reference to a scalar, so: EXPRESSION is:
($_ = $1) =~ s|\s*\=\s*|=|gsi , \$_
that is a canned sequence of sub-expressions. The last is what "returned", and you can notice that it is a reference to a scalar (i.e. a reference to \$_), which is what we want (see first bullet). The first sub-expression is an assignment followed by yet another substitution, using the | character as a separator.

The assignment is necessary because $1 is read-only. So you use the $_ variable as a temporary copy, that you can modify via the substitution.

The substitution can be rewritten as:

s{\s*\=\s*} {=}gsi
which means that any "sequence of zero or more spaces, followed by an equal sign, followed by a sequence of zero or more spaces" is replaced by "an equal sign". Note, again, the use of the g modifier (which applies the substitution to the entire string) and of the s modifier ("treat newline as any other char"). The i modifier is not needed in this case, but it doesn't hurt. Why do the trick <c>${ ... , \$_ }, then? Because it lets you embed code to define the "contents" part without the need to have separate statements (I guess).

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

In reply to Re: Need explanation: what \$_ and ${} do by polettix
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.