【发布时间】:2010-11-24 16:34:47
【问题描述】:
perl6/Rakudo 是否有与 perl5 的 __DATA__ 或 __END__ 部分等效的内容?
【问题讨论】:
perl6/Rakudo 是否有与 perl5 的 __DATA__ 或 __END__ 部分等效的内容?
【问题讨论】:
引用S26:
命名的 Perldoc 块,其 typename 是 DATA 是 Perl 6 的等价物 Perl 5
__DATA__部分。这 不同之处在于 =DATA 块是 只是普通的 Pod 块,可能会出现 源文件中的任何位置,并且作为 根据需要多次。 Synopsis 2 描述了新的 Perl 6 接口 内联数据。
理论上你应该能够做这样的事情(如果它关闭,请有人修复语法):
use v6;
=begin DATA
Foo
=end DATA
say @=DATA;
In practice it seems Rakudo 还不支持。
【讨论】:
谨慎选择性地引用当前S02设计文档:
不再有任何特殊的 DATA 流——任何 Pod 块在 当前文件可以通过 Pod 对象访问...
您必须自己将 [Pod block] 内容拆分为行。
[推测] 也可以将 Pod 对象视为 IO::Handle,逐行读取 Pod 信息(如 DATA Perl 5 中的文件句柄,但适用于任何 Pod 块)。
因此,您可以在脚本文件中定义任意数量的 Pod 块,而不是通过读取文件句柄访问每个文件的单个 DATA 部分;它们在编译时存储在$=pod 变量中;您从该变量中读取;那些称为“数据”的数据是 Perl 5 的 DATA 的等价物。
今天有效。我稍后会展示。但首先我需要谈谈今天不起作用的东西。
上面的引用是高度选择性的。省略的文本谈到 P6 自动创建一个名称为 $=foo 的变量,对应于名称为 'foo' 的 Pod 块。这是 Pod 块的一般尚未实现的功能,而不仅仅是数据块。
Pod 设计文档S26 的“数据块”部分谈到数据块比普通的旧 Pod 块做一些更有趣的事情。这也尚未实施。
那么,现在让我们继续讨论今天可以做的事情:
=foo This is a Pod block. A single line one. This Pod block's name is 'foo'.
=begin qux
This is another syntax for defining a Pod block.
It allows for multi line content.
This block's name is 'qux'.
=end qux
=data A data block -- a Pod block with the name 'data'.
# Data blocks are P6's version of P5's __DATA__.
# But you can have multiple data blocks:
=begin data
Another data block.
This time a multi line one.
=end data
$=pod.grep(*.name eq 'data').map(*.contents[0].contents.say);
打印出来:
A data block -- a Pod block with the name 'data'.
Another data block. This time a multi line one.
所以,它有点工作。但它显然需要更多的糖。
顺便说一句,如果最后一个 FP 样式行没有意义,这里有一个命令式等价物:
for @$=pod {
if .name eq 'data' {
say .contents[0].contents
}
};
【讨论】:
在完全实施之前,作为一种解决方法,您可以使用 heredocs。
for data().lines -> $line {
put $line;
}
sub data {
return q:to/END/;
Foo, bar, baz
1, 2, 3
END
}
输出
Foo, bar, baz 1, 2, 3
【讨论】:
要获取数据数组,同时将数据放在程序底部以提高可读性,这里是@Christopher Bottoms 答案的变体:
my @txts = data();
dd @txts;
# this works too
my %stuff = hashdata();
dd %stuff;
# a lot of lines
sub data() {
return ( q:to/LINE1/,
Phasellus dictum, nunc id vestibulum rhoncus, mauris massa tempus nibh,
nec tincidunt nisi tellus et arcu. Phasellus vulputate consectetur
vulputate. Quisque viverra commodo velit ac tincidunt. Nulla et est sem.
Mauris gravida, nulla rutrum pharetra dapibus, eros velit feugiat nibh,
nec iaculis purus urna ut diam. Praesent molestie felis a turpis gravida
placerat. Duis sagittis pulvinar risus non aliquet. Nunc quis purus
tempor, mattis nunc eu, porta ligula. Suspendisse dictum sit amet urna
dapibus suscipit.
LINE1
q:to/LINE2/,
Praesent molestie felis a turpis gravida
placerat. Duis sagittis pulvinar risus non aliquet. Nunc quis purus
tempor, mattis nunc eu, porta ligula. Suspendisse dictum sit amet urna
dapibus suscipit.
LINE2
q:to/LINE3/);
Quisque viverra commodo velit ac tincidunt. Nulla et est sem.
Mauris gravida, nulla rutrum pharetra dapibus, eros velit feugiat nibh,
nec iaculis purus urna ut diam. Praesent molestie felis a turpis gravida
placerat.
LINE3
}
sub hashdata() { # a hash works too.
return ( 'p' => q:to/PDATA/,
Some multiline data
in some lines
PDATA
'q' => q:to/QDATA/,
More data in
multiple lines
QDATA
'r' => q:to/RDATA/
Note that indentation depends on the position of the
ending token.
Also, the punctuation following the regex is the punctuation
following the expression. So a comma after each of the
p and q, but not needed after the r
RDATA
)
}
【讨论】: