-1

I am using someone else's text parsing perl code and I'd like to make some modifications. Could someone explain what =~ symbol is doing?

$xmlfile =~ s/value="{(.*?)}"/'value="'.&subst($1).'"'/ge;

EDIT:

So I found this thread that explains part of the regex string.

http://www.perlmonks.org/?node_id=24640

Tayyar R
  • 655
  • 6
  • 22
  • 1
    http://perldoc.perl.org/perlre.html – Marc B May 05 '14 at 05:34
  • 2
    do you understand the rest of the line? – Thilo May 05 '14 at 05:34
  • Tip: you can search for funky syntax on this site. Use the search string `[perl] "=~"` and you'd have had your answer. – Mat May 05 '14 at 05:35
  • @Thilo Not really. I understand that it is doing some kind of string matching and runs subst() function on the matched string, but I don't really understand the details. Explanation would be appreciated. Thanks! – Tayyar R May 05 '14 at 19:54

2 Answers2

1

By default, matching, substitution, or transliteration act on $_; to use them on another variable, the binding operator =~ is used. In your case, the variable the substitution will be performed on is $xmlfile.

ysth
  • 96,171
  • 6
  • 121
  • 214
  • So is $xmlfile passed into $1? Or whatever is matched by s/value="{(.*?)}" is passed into $1? – Tayyar R May 05 '14 at 19:55
  • `$1` is what is captured by the `(.*?)` so is what is found inside the curly braces. $xmlfile is the string it will be looking through and substituting in. – ysth May 05 '14 at 20:33
0

According to perlop:

Binary "=~" binds a scalar expression to a pattern match. Certain operations search or modify the string $_ by default. This operator makes that kind of operation work on some other string. The right argument is a search pattern, substitution, or transliteration. The left argument is what is supposed to be searched, substituted, or transliterated instead of the default $_.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47