there:
I’m new to Perl, and get a string concatenation problem of it. I have two strings below:
my $string1 = "hello\U\Q \t\n\f\b\aWorld" . "\n" . "\E";
my $string2 = "hello\U\Q \t\n\f\b\aWorld\n\E";
They are looked the same to me, until i print them out.
$string1 looks like this:
hello\ \ \
\
\WORLD
and with a bell ring.
$string2 is this one:
hello\ \ \
\
\WORLD\
with the same bell ring, and a backslash at the tail.
Why does $string2 get a backslash at its end but $string1 doesn’t?
When you use
\Qyou’re telling it to quote (put a backslash in front of) all the characters that aren’t matched by\w. The result is that you’re getting a backslash added every time there’s a backslash in your code. e.g.\acreates the bell sound but your string gets a backslash added in. When you use\Qit behaves this way until you reach\Eor get to the end of the string.When you create
$string1you actually have 3 separate strings that you’re adding together so they’re evaluated separately. The result is that only the first of the 3 is affected by the\Q.In the second example the
\n\Eresults in\\in the string. When you print this out it results in the trailing backslash you’re seeing.Hope that makes sense.