The Q Lang
在 Perl 6 中, 字符串通常使用一些引號結(jié)構(gòu)來表示. 這些引號結(jié)構(gòu)中,最簡單的就是 Q
, 通過便捷方式 ?…?
或 Q
后跟著由任意一對兒分隔符包圍著的文本. 大多數(shù)時候, 你需要的只是 '…'
或 "…"
.
Literal strings: Q
Q[A literal string]
?More plainly.?
Q ^Almost any non-word character can be a delimiter!^
分隔符能夠嵌套, 但是在普通的 Q 形式中, 反斜線轉(zhuǎn)義是不允許的. 換種說法就是, Q 字符串盡可能被作為字面量.
Q<Make sure you <match> opening and closing delimiters>
Q{This is still a closing brace → \}
這些例子產(chǎn)生:
A literal string
More plainly.
Almost any non-word character can be a delimiter!
Make sure you <match> opening and closing delimiters
This is still a closing brace → \
Escaping: q
'Very plain'
q[This back\slash stays]
q[This back\\slash stays] # Identical output
q{This is not a closing brace → \}, but this is → }
Q :q $There are no backslashes here, only lots of \$\$\$!$
'(Just kidding. There\'s no money in that string)'
'No $interpolation {here}!'
Q:q#Just a literal "\n" here#
q
形式的引號結(jié)構(gòu)允許使用反斜線轉(zhuǎn)義可能會結(jié)束字符串的字符. 反斜線自身也能被轉(zhuǎn)義, 就像上面的第三個例子那樣. 通常的形式是 '...'
或 q
后跟著分隔符, 但是它也能作為 Q 上的副詞使用, 就像上面的第五個和最后一個例子那樣.
這些例子產(chǎn)生:
Very plain
This back\slash stays
This back\slash stays
This is not a closing brace → } but this is →
There are no backslashes here, only lots of $$$!
(Just kidding. There's no money in that string)
No $interpolation {here}!
Just a literal "\n" here
Interpolation: qq
my $color = 'blue';
say "My favorite color is $color!" # My favorite color is blue!
qq
形式 -- 通常使用雙引號寫成 -- 允許變量的插值, 例如字符串中能寫入變量, 以使變量的內(nèi)容能插入到字符串中. 在 qq
引起字符串中, 也能轉(zhuǎn)義變量.
say "The \$color variable contains the value '$color'";
# The $color variable contatins the value 'blue'
qq
的另外一種功能是使用花括號在字符串中插值 Perl 6 代碼:
my ($x, $y, $z) = 4, 3.5, 3;
say "This room is $x m by $y m by $z m."
say "Therefore its volume should be { $x * $y * $z } m3!"
輸出:
This room is 4 m by 3.5 m by 3 m.
Therefore its volume should be 42 m3!
默認(rèn)情況下, 只有帶有 '$' 符號的變量才能正常插值. 這時, "documentation@perl6.org"
不會插值 @perl6
變量. 如果呢確實想那么做, 在變量名后面添加一個 []
:
my @neighbors = "Felix", "Danielle", "Lucinda";
say "@neighbors[] and I try our best to coexist peacefully."
輸出:
Felix Danielle Lucinda and I try our best to coexist peacefully.
通常使用一個方法調(diào)用會更合適. 只有在 qq 引號中, 方法調(diào)用后面有圓括號, 就能進(jìn)行插值:
say "@neighbors.join(', ') and I try our best to coexist peacefully."
輸出:
Felix, Danielle, Lucinda and I try our best to coexist peacefully.
而 "@example.com"
產(chǎn)生 @example.com
.
Word quoting: qw
<a b c> eqv ('a', 'b', 'c')
qw|! @ # $ % ^ & * \| < > | eqv '! @ # $ % ^ & | < >'.words
Q:w { [ ] \{ \} } eqv ('[', ']', '{', '}')
:w
通常寫作 <...>
或 qw
, 把字符串分割為"words" (單詞). 在這種情景下, 單詞被定義為由空格分割的一串非空白字符. q:w
和 qw
繼承了 q
的插值和轉(zhuǎn)義語法, 還有單引號字符串分割符, 而 Qw
和 Q:w
繼承了 Q
的非轉(zhuǎn)義語法.
my @directions = 'left', 'right,', 'up', 'down';
這樣讀和寫都更容易:
my @directions = <left right up down>;
Word quoting with interpolation: qqw
qw
形式的 word quoting 不會進(jìn)行變量插值:
my $a = 42; say qw{$a b c}; # $a b c
因此, 如果你想在引號字符串中進(jìn)行變量插值, 你需要使用 qqw
變體:
my $a = 42;
my @list = qqw{$a b c};
say @list; # 42 b c
或者同樣的:
my $a = 42;
my @list = ?$a b c?;
say @list; # 42 b c
Shell quoting: qx
把一個字符串作為一個外部程序運(yùn)行, 在 Perl 6 中反引號不再用于 shell quoting, 并且 qx 不再插值 Perl 變量, 因此:
my $world = "there";
say qx{echo "hello $world"}
僅僅打印 hello. 然而, 如果你在調(diào)用 perl6 之前聲明了一個環(huán)境變量, 這在 qx 里是可用的, 例如:
WORLD="there" perl6
> say qx{echo "hello $WORLD"}
現(xiàn)在會打印 hello there.
調(diào)用 qx
會返回結(jié)果, 所以這個結(jié)果能被賦值給一個變量以便后來使用:
my $output = qx{echo "hello!"};
say $output; # hello!
Shell quoting with interpolation: qqx
帶插值的 Shell quoting:
my $world = "there";
say qqx{echo "hello $world"}; # hello there
再一次, 外部命令的輸出結(jié)果可以保存在一個變量中:
my $word = "cool";
my $option = "-i";
my $file = "/usr/share/dict/words";
my $output = qqx{grep $option $word $file};
# runs the command: grep -i cool /usr/share/dict/words
say $output; # Cooley
Cooley's
Coolidge
Coolidge's
cool
...
Heredocs: :to
heredocs
是多行字符串字面量的便捷方式, 你能選擇自己的分隔符:
say q:to/END/;
Here is
some multi-line
string
END
heredoc 的內(nèi)容從下一行開始.
my $escaped = my-escaping-function(q:to/TERMINATOR/, language => 'html');
Here are the contents of the heredoc.
Potentially multiple lines.
TERMINATOR
如果終止分隔符縮進(jìn)了, 同等數(shù)量的縮進(jìn)會從字符串字面量上移除. 因此下面這個 heredoc
say q:to/END/;
Here is
some multi line
string
END
輸出:
Here is
some multi line
string