Perl: Quote Strings
2 major ways to quote strings strings: single quote and double quote.
Single Quote, Literal String
Use single quote for literal string.
# use single quote for literal string $a = 'this and that'; print $a; # prints 2 lines
Double Quote, interpolated string
To have characters \n
for newline and \t
for tab, use double quote.
- Backslash is char escape.
- Variables are evaluated.
$a = "this\nand that"; print $a; # prints 2 lines
When double quote is used, variables inside the string will be evaluated.
$a = 4; $b = "this is $a"; print $b; # prints 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";