1

I'm new to perl and I found this expressions bit difficult to understand

$a =~ s/\'/\'\"\'\"\'/g

Can someone help me understand what this piece of code does?

Turbo Sullivan
  • 827
  • 1
  • 7
  • 9

4 Answers4

2

It is not clear which part of it is a problem. Here is a brief mention of each.

The expression $str =~ s/pattern/replacement/ is a regular expression, replacing the part of string $str that is matched by the "pattern", by the "replacement". With /g at the end it does so for all instances of the "pattern" found in the string. There is far, far more to it -- see the tutorial, a quick start, the reference, and full information in perlre and perlop.

Some characters have a special meaning, in particular when used in the "pattern". A common set is .^$*+?()[{\|, but this depends on the flavor of regex. Also, whatever is used as a delimiter (commonly /) has to be escaped as well. See documentation and for example this post. If you want to use any one of those characters as a literal character to be found in the string, they have to be escaped by the backslash. (Also see about escaped sequences.) In your example, the ' is escaped, and so are the characters used as the replacement.

The thing is, these in fact don't need to be escaped. So this works

use strict;
use warnings;

my $str = "a'b'c";
$str =~ s/'/'"'/g;
print "$str\n";

It prints the desired a'"'b'"'c. Note that you still may escape them and it will work. One situation where ' is indeed very special is in a one liner, where it delimits the whole piece of code to submit to Perl via shell, to be run. (However, merely escaping it there does not work.) Altogether, the expression you show does the replacement just as in the answer by Jens, but with a twist that is not needed.

A note on a related feature: you can use nearly anything for delimiters, not necessarily just /. So this |patt|repl| is fine, and nicely even this is: {patt}{repl} (unless they are used inside). This is sometimes extremely handy, improving readability a lot. For one thing, then you don't have to escape the / itself inside the regex.

I hope this helps a little.

Community
  • 1
  • 1
zdim
  • 64,580
  • 5
  • 52
  • 81
1

All the \ in that are useless (but harmless), so it is equivalent to s/'/'"'"'/g. It replaces every ' with '"'"' in the string.

This is often used for shell quoting. See for example https://stackoverflow.com/a/24869016/17389

Community
  • 1
  • 1
ysth
  • 96,171
  • 6
  • 121
  • 214
0

This peace of Code replace every singe Quote with '"'"'

Jens
  • 67,715
  • 15
  • 98
  • 113
0

Can be simplified by removing the needless back slashes $a =~ s/'/"'"'/g

Every ' will we replaced with "'"'

Tim Potapov
  • 431
  • 2
  • 12
  • When you type the code like this, not in a `code` block, so that it uses variable-width fonts, you can immediately see why someone would have put in the needless backslashes. Because what is displayed in your answer here is nearly impossible to discern with all the `'` and `"` bunched up like that. – Paul L May 16 '16 at 12:54