Perl: Quote Strings

By Xah Lee. Date: . Last updated: .

APOSTROPHE Delimiter, for Literal String

# use APOSTROPHE for literal string
$x = 'this and
 that';
print $x;
# prints 2 lines

QUOTATION MARK Delimiter, for Interpolated String

use QUOTATION MARK as delimiter, to create string that is interpolated.

INTERPOLATED string means:

$x = "this\nthat";
print $x;
# this
# that
$a = 4;
$b = "this is $a";
print $b;
# this is 4

String Quote Functions, q, qq

You can also use the syntax q(this n that), which is equivalent to 'this n that'.

The parenthesis can be curly brackets {} or square brackets []. It can also be / \ | @ and most others ASCII symbols.

# the following are all same
$a = q(it's good);
$b = q[it's good];
$c = q{it's good};
$d = q|it's good|;
$e =  "it's good";
$f =  'it\'s good';

Similarly, "…" is same as qq(…).

$a = q(everything is literal,
$what or \n ' ' " ");

$b = qq[here, variables $a will be
expanded, backslash act as escape \n (and "quotes" or parenthesis needn't be escaped).];

print $a, "\n";
print '-----------', "\n";
print $b, "\n";

Perl String