Perl6(又名 Raku)是否支持与 Perl5 的 __DATA__ 和 __END__ 部分等效的内容?
perl6/Rakudo 是否有与 perl5 的 __DATA__
或 __END__
部分等效的内容?
Does perl6/Rakudo have something equivalent to perl5's __DATA__
or __END__
sections?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
引用 S26:
理论上你应该能够做这样的事情(如果语法关闭,请有人修复语法):
实践中Rakudo 似乎还不支持这一点。
Quote S26:
In theory you should be able to do something like this (somebody please fix the syntax if it’s off):
In practice it seems that Rakudo does not support that, yet.
要仔细选择性地引用当前的 S02 设计文档:
因此,您可以在脚本文件中定义任意数量的 Pod 块,而不是通过读取文件句柄来访问每个文件的单个 DATA 部分;它们在编译时存储在
$=pod
变量中;你从该变量中读取;所谓的“data”相当于 Perl 5 的 DATA。这在今天有效。我稍后会展示这一点。但首先我需要谈谈今天不起作用的东西。
上面的引用是高度选择性的。省略的文本谈到 P6 自动创建一个名称为
$=foo
形式的变量,对应于名称为“foo”的 Pod 块。这是 Pod 块的一个尚未实现的通用功能,而不仅仅是数据块。Pod 设计文档 S26 的“数据块”部分讨论了数据块做一些更奇特的事情比普通的旧 Pod 块更重要的东西。这也尚未实施。
那么,现在让我们继续讨论今天可以做的事情:
这会打印:
所以,它有点有效。但它显然需要更多的糖。
顺便说一句,如果最后 FP 风格的行没有意义,这里有一个等价的命令式:
To carefully selectively quote the current S02 design document:
So, instead of the single DATA section per file which you access by reading a filehandle, you define any number of Pod blocks in your script file; they're stored in the
$=pod
variable at compile time; you read from that variable; and the ones called 'data' are the equivalents of Perl 5's DATA.This works today. I'll show that in a moment. But first I need to talk about stuff that does not work today.
The quoting above was highly selective. The elided text talked about P6 automatically creating a variable with a name of the form
$=foo
corresponding to Pod blocks with the name 'foo'. This is a general still unimplemented feature of Pod blocks, not just data blocks.The "data block" section of the Pod design doc S26 talks about data blocks doing some fancier stuff than plain old Pod blocks. This has not yet been implemented either.
So, now let's move on to what can be done today:
This prints:
So, it sorta works. But it clearly needs a lot more sugar.
By the way, if the last FP style line didn't make sense, here's an imperative equivalent:
作为完全实现之前的解决方法,您可以使用此处文档。
输出
As a work-around until this gets fully implemented, you can use heredocs.
Outputs
要获取数据数组,同时将数据放在程序底部以帮助提高可读性,这里是 @Christopher Bottoms 答案的变体:
To get an array of data, while putting the data at the bottom of the program to help with readability, here is an variation of @Christopher Bottoms answer:
是。
根据此2023 perl6.users 电子邮件对话,您以以下方式结束程序/文档:
=finish
然后:“将
=finish
之后的任何内容放入可以使用$=finish
(也未记录)将其拉入 Raku 程序的字符串。”因此,您可以使用以下方式引用该字符串(在程序主体内):
$ =finish
该链接进一步阐述:
“引入了
=finish
,而不是 Perl 的__DATA__
。”“
=finish
是一个POD6 规范中未记录的部分......很快就会记录下来。”https://www.nntp.perl.org/group/perl.perl6.users/2023/06/ msg10997.html
YES.
According to this 2023 perl6.users email conversation, you end your program/document with:
=finish
Then: "Anything after
=finish
is put in string that can be pulled into a Raku program with$=finish
(also undocumented)."So you can reference the string (within the body of your program) using:
$=finish
That link further elaborates:
"
=finish
was introduced instead of Perl's__DATA__
.""
=finish
is an undocumented part of the POD6 specification ... . It will be documented soon."https://www.nntp.perl.org/group/perl.perl6.users/2023/06/msg10997.html