in reply to Need explanation: what \$_ and ${} do
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:
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:'<' . ${($_=$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.($_ = $1) =~ s|\s*\=\s*|=|gsi , \$_
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:
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).s{\s*\=\s*} {=}gsi
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
|
|---|