Skip to content

Latest commit

 

History

History
467 lines (343 loc) · 14.6 KB

60.quoting-constructs.adoc

File metadata and controls

467 lines (343 loc) · 14.6 KB

The Q Lang

在 Raku 中, 字符串通常使用一些引号结构来表示. 这些引号结构中,最简单的就是 Q, 通过便捷方式 「…」Q 后跟着由任意一对儿分隔符包围着的文本. 大多数时候, 你需要的只是 '…'"…".

Literal strings: Q

Q[A literal string]
More plainly.
Q ^Almost any non-word character can be a delimiter!^
Q 「Delimiters can be repeated/nested if they are adjacent.」

分隔符能够嵌套, 但是在普通的 Q 形式中, 反斜线转义是不允许的. 换种说法就是, Q 字符串尽可能被作为字面量.

Qqqq 之后不允许立即使用一些分隔符。标识符中允许的任何字符都不允许使用,因为在这种情况下,引号结构和这些字符一起被解释为标识符。此外,( ) 是不允许的,因为它被解释为函数调用。如果你仍然希望使用这些字符作为分隔符,请用空格将它们与 Qqqq 分隔开。请注意,一些自然语言在字符串的右侧使用左分隔引号。Q 不支持这些,因为它依赖unicode 属性来区分左分隔符和右分隔符。

Q'this will not work!'
Q(this won't work either!)

上面对例子会产生错误。然而,下面这个能起作用:

Q (this is fine, because of space after Q)
Q 'and so is this'
Q<Make sure you <match> opening and closing delimiters>
Q{This is still a closing curly brace → \}

这些例子产生:

this is fine, because of space after Q
and so is this
Make sure you <match> opening and closing delimiters
This is still a closing curly brace → \

引号结构的行为可以用副词修改,后面的章节会详细解释。

Short

Long

Meaning

:x

:exec

Execute as command and return results

:w

:words

Split result on words (no quote protection)

:ww

:quotewords

Split result on words (with quote protection)

:q

:single

Interpolate \\, \qq[…​] and escaping the delimiter with \

:qq

:double

Interpolate with :s, :a, :h, :f, :c, :b

:s

:scalar

Interpolate $ vars

:a

:array

Interpolate @ vars

:h

:hash

Interpolate % vars

:f

:function

Interpolate & calls

:c

:closure

Interpolate {…​} expressions

:b

:backslash

Enable backslash escapes (\n, \qq, \$foo, etc)

:to

:heredoc

Parse result as heredoc terminator

:v

:val

Convert to allomorph if possible

Escaping: q

'Very plain';
q[This back\slash stays];
q[This back\\slash stays]; # Identical output
q{This is not a closing curly 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 形式的引号结构允许使用反斜线转义可能会结束字符串的字符. 反斜线自身也能被转义, 就像上面的第三个例子那样. 通常的形式是 '…​'q 后跟着分隔符, 但是它也能作为 Q 上的副词使用, 就像上面的第五个和最后一个例子那样.

这些例子产生:

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

\qq…​ 转义序列允许 [qq 插值] 的一部分字符串。当字符串中有 HTML 标记时,使用这个转义序列非常方便,可以避免将尖括号解释为散列键:

my $var = 'foo';
say '<code>$var</code> is <var>\qq[$var.uc()]</var>';
# OUTPUT: «<code>$var</code> is <var>FOO</var>␤»

Interpolation: qq

my $color = 'blue';
say "My favorite color is $color!" # My favorite color is blue!

qq 形式 — 通常使用双引号写成 — 允许变量的插值, 例如字符串中能写入变量, 以使变量的内容能插入到字符串中. 在 qq 引起字符串中, 也能转义变量.

say "The \$color variable contains the value '$color'";
# The $color variable contatins the value 'blue'

qq 的另外一种功能是使用花括号在字符串中插值 Raku 代码:

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 } m³!"

输出:

This room is 4 m by 3.5 m by 3 m.
Therefore its volume should be 42 m³!

默认情况下, 只有带有 $ 符号的变量才能正常插值. 这时, "documentation@raku.org" 不会插值 @raku 变量. 如果你确实想那么做, 在变量名后面添加一个 []:

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.

通常使用一个方法调用会更合适. 只有在 qq 引号中, 方法调用后面有圆括号, 就能进行插值:

say "@neighbors.join(', ') and I try our best to coexist peacefully."

输出:

Felix, Danielle, Lucinda and I try our best to coexist peacefully.

"@example.com" 产生 @example.com.

要调用子例程请使用 & 符号。

say "abc&uc("def")ghi";
# OUTPUT: «abcDEFghi␤»

后环缀操作符和 subscripts 也会被插值。

my %h = :1st; say "abc%h<st>ghi";
# OUTPUT: «abc1ghi␤»

要输入 unicode 序列,请使用 \x\x[] 加上字符的十六进制编码或字符列表。

my $s = "I \x2665 Raku!";
say $s;
# OUTPUT: «I ♥ Raku!␤»

$s = "I really \x[2661,2665,2764,1f495] Raku!";
say $s;
# OUTPUT: «I really ♡♥❤💕 Raku!␤»

您还可以在 \c[] 中使用 unicode 名称命名序列名称别名

my $s = "Camelia \c[BROKEN HEART] my \c[HEAVY BLACK HEART]!";
say $s;
# OUTPUT: «Camelia 💔 my ❤!␤»

对未定义值进行插值将引发控件异常,该异常可以在当前控件块中使用 CONTROL 捕获。

sub niler {Nil};
my Str $a = niler;
say("$a.html", "sometext");
say "alive"; # this line is dead code
CONTROL { .die };

Word quoting: qw

qw|! @ # $ % ^ & * \| < > | eqv '! @ # $ % ^ & * | < >'.words.list
q:w { [ ] \{ \} } eqv ('[', ']', '{', '}')
Q:w | [ ] { } | eqv ('[', ']', '{', '}')

:w 通常写作 qw, 把字符串分割为 "words" (单词). 在这种情景下, 单词被定义为由空格分割的一串非空白字符. q:wqw 继承了 q 的插值和转义语法, 还有单引号字符串分割符, 而 QwQ:w 继承了 Q 的非转义语法.

my @directions = 'left', 'right,', 'up', 'down';

这样读和写都更容易:

my @directions = qw|left right up down|;

Word quoting: <>

say <a b c> eqv ('a', 'b', 'c');   # OUTPUT: «True␤»
say <a b 42> eqv ('a', 'b', '42'); # OUTPUT: «False␤», the 42 became an IntStr allomorph
say < 42 > ~~ Int; # OUTPUT: «True␤»
say < 42 > ~~ Str; # OUTPUT: «True␤»

尖括号的引号类似于 qw,但有一个额外的特性,可以让你构造特定数字的同质异形体或字面量:

say <42 4/2 1e6 1+1i abc>.perl;
# OUTPUT: «(IntStr.new(42, "42"), RatStr.new(2.0, "4/2"), NumStr.new(1000000e0, "1e6"), ComplexStr.new(<1+1i>, "1+1i"), "abc")␤»

要构造 RatComplex 字面量,请在数字周围使用尖括号,不带任何额外的空格:

say <42/10>.^name;   # OUTPUT: «Rat␤»
say <1+42i>.^name;   # OUTPUT: «Complex␤»
say < 42/10 >.^name; # OUTPUT: «RatStr␤»
say < 1+42i >.^name; # OUTPUT: «ComplexStr␤»

42/101+42i 相比,不涉及除法(或加法)运算。这对于例程签名中的字面量很有用,例如:

sub close-enough-π (<355/113>) {
    say "Your π is close enough!"
}
close-enough-π 710/226; # OUTPUT: «Your π is close enough!␤»

# WRONG: can't do this, since it's a division operation

sub compilation-failure (355/113) {}

Word quoting with quote protection: qww

单词引用的 qw 格式将按字面意思处理引用字符,将它们保留在结果单词中:

say qw{"a b" c}.perl; # OUTPUT: «("\"a", "b\"", "c")␤»

因此,如果您希望在结果单词中保留引用的子字符串作为单个项,则需要使用 qww 变体:

say qww{"a b" c}.perl; # OUTPUT: «("a b", "c")␤»

Word quoting with interpolation: qqw

qw 形式的 word quoting 不会进行变量插值:

my $a = 42; say qw{$a b c};  # $a b c

因此, 如果你想在引号字符串中进行变量插值, 你需要使用 qqw 变体:

my $a = 42;
my @list = qqw{$a b c};
say @list;                # 42 b c

注意,变量插值发生在单词分割之前:

my $a = "a b";
my @list = qqw{$a c};
.say for @list; # OUTPUT: «a␤b␤c␤»

Word quoting with interpolation and quote protection: qqww

qqw 形式的单词引用会把引起的字符当作字面量,将引起的字符留在结果单词中:

my $a = 42; say qqw{"$a b" c}.perl;  # OUTPUT: «("\"42", "b\"", "c")␤»

因此,如果希望在结果单词中保留引起的子字符串为单个项,则需要使用 qqww 变体:

my $a = 42; say qqww{"$a b" c}.perl; # OUTPUT: «("42 b", "c")␤»

引号保护发生在插值之前,插值发生在分词之前,所以来自插值变量内部的引号只是字面引号字符:

my $a = "1 2";
say qqww{"$a" $a}.perl; # OUTPUT: «("1 2", "1", "2")␤»
my $b = "1 \"2 3\"";
say qqww{"$b" $b}.perl; # OUTPUT: «("1 \"2 3\"", "1", "\"2", "3\"")␤»

Word quoting with interpolation and quote protection: « »

这种引用方式类似于 qqww,但它具有构造 allomorphs 的额外好处(使其功能相当于 qq:ww:v)。与 «» 等价的 ASCII 是双尖括号 << >>

# Allomorph Construction
my $a = 42; say «  $a b c    ».perl;  # OUTPUT: «(IntStr.new(42, "42"), "b", "c")␤»
my $a = 42; say << $a b c   >>.perl;  # OUTPUT: «(IntStr.new(42, "42"), "b", "c")␤»

# Quote Protection
my $a = 42; say «  "$a b" c  ».perl;  # OUTPUT: «("42 b", "c")␤»
my $a = 42; say << "$a b" c >>.perl;  # OUTPUT: «("42 b", "c")␤»

Shell quoting: qx

要将字符串作为外部程序运行,不仅可以将字符串传递给 shellrun 函数,还可以执行 shell 引用。然而,有一些微妙之处需要考虑。qx 引号不插入变量。因此

my $world = "there";
say qx{echo "hello $world"}

仅仅打印 hello. 然而, 如果你在调用 raku 之前声明了一个环境变量, 这在 qx 里是可用的, 例如:

WORLD="there" raku
> say qx{echo "hello $WORLD"}

现在会打印 hello there.

调用 qx 会返回结果, 所以这个结果能被赋值给一个变量以便后来使用:

my $output = qx{echo "hello!"};
say $output;    # hello!

Shell quoting with interpolation: qqx

如果希望在外部命令中使用 Raku 变量的内容,那么应该使用 qqx shell 引用结构:

my $world = "there";
say qqx{echo "hello $world"};  # hello there

再一次, 外部命令的输出结果可以保存在一个变量中:

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␤ ...

有关执行外部命令的更好方法,请参见 runProc::Async

Heredocs: :to

一种方便的写多行字符串字面量的方式是 heredocs,它让你选择自己的分隔符:

say q:to/END/;
Here is
some multi-line
string
END

heredoc 的内容总是从下一行开始,所以你可以(也应该)完成这一行。

my $escaped = my-escaping-function(q:to/TERMINATOR/, language => 'html');
Here are the contents of the heredoc.
Potentially multiple lines.
TERMINATOR

如果终止分隔符缩进了, 同等数量的缩进会从字符串字面量上移除. 因此下面这个 heredoc

say q:to/END/;
    Here is
    some multi line
        string
    END

输出:

Here is
some multi line
    string

heredoc 包含了终止符之前的换行符。

要允许对变量进行插值,可以使用 qq 形式,但如果不是已定义变量的标识符,则必须转义元字符 {\$。例如:

my $f = 'db.7.3.8';
my $s = qq:to/END/;
option \{
    file "$f";
};
END
say $s;

会产生:

option {
    file "db.7.3.8";
};

您可以在同一行开始多个 heredoc。

my ($first, $second) = qq:to/END1/, qq:to/END2/;
  FIRST
  MULTILINE
  STRING
  END1
   SECOND
   MULTILINE
   STRING
   END2

Unquoting

字面量字符串允许使用转义序列插入内嵌的引用结构,例如:

my $animal="quaggas";
say 'These animals look like \qq[$animal]'; # OUTPUT: «These animals look like quaggas␤»
say 'These animals are \qqw[$animal or zebras]'; # OUTPUT: «These animals are quaggas or zebras␤»

在本例中,\qq 将做双引号内插,\qqw 文字内插。如上所述,转义任何其他引用结构都将以相同的方式进行,从而允许在字面量字符串中进行插值。

Regexes

有关在 regexes 中应用的引用的信息,请参阅正则表达式文档