Xah Lee, 2005-01-09, 2011-07-22
Strings are enclosed using single quote or double quote. e.g.
a="this " b='and that' print a, b
You can use \n for linebreak, and \t for tab, etc.
a="this\nthat\n" # use \n for line break b='more\nthings' # single quote works too print a, b
To quote a string of multiple lines, use triple quotes. Example:
d="""this will be printed in 3 lines""" print d
You can add r to front for “raw string”. That is, backslash characters will be interpreted as is, not as escapes.
c=r"this\n and that" print c # prints a single line
Summary:
r in front of your quoting char, like this: r"hot".r in front.Use single quote for literal string.
# use single quote for literal string $a='this and that'; print $a; # result will contain line break
To have characters \n for newline and \t for tab, use double quote.
$a="this\nand that"; print $a; # result will contain line break
When double quote is used, variables inside the string will be evaluated.
$a=4; $b = "this is $a"; print $b; # prints “this is 4”
Basically, perl has 2 modes of strings: single quote and double quote. In single quote mode, everything is literal. In double quote mode, backslash is a char escape mechanism, and variables inside it will be evaluated.
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 [].
# the following are all identical $a = q(this 'n' that); $b = q[this 'n' that]; $c = "this 'n' that"; $d = 'this \'n\' that'; print $a, "\n"; print $b, "\n"; print $c, "\n"; print $d, "\n";
Similarly, double quote is equivalent with qq().
# perl $a=q(here, everything is literal, $what or \n or ' or " or not.); $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";blog comments powered by Disqus