Perl6 是否支持与 Perl5 的 __DATA__ 和 __END__ 部分等效的内容?

问题描述 投票:0回答:5

perl6/Rakudo 是否有与 perl5 的

__DATA__
__END__
部分等效的内容?

perl raku rakudo
5个回答
14
投票

引用S26

命名 Perldoc 块,其类型名为 DATA 是 Perl 6 的等价物 Perl 5

__DATA__
部分。这 区别在于 =DATA 块是 只是普通的 Pod 块,可能会出现 源文件中的任何位置,以及 根据需要多次。 剧情简介2 描述了新的 Perl 6 接口 内联数据。

理论上你应该能够做这样的事情(如果语法关闭,请有人修复它):

use v6;

=begin DATA
Foo
=end DATA

say @=DATA;

实际上,Rakudo 似乎还不支持这一点。


13
投票
仔细选择性地引用当前的

S02设计文档:

不再有任何特殊的数据流——任何 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 } };
    

4
投票
作为在完全实现之前的解决方法,您可以使用此处文档。

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

    

4
投票
要获取数据数组,同时将数据放在程序底部以帮助提高可读性,这里是 @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 ) }
    

0
投票

是的。

根据

this对话,您以以下方式结束程序/文档:

=finish


然后:

=finish

之后的任何内容都放入字符串中,可以使用
$=finish
(也未记录)将其拉入Raku程序。

因此您可以使用以下方式引用字符串(在程序主体内):

$=finish


该链接进一步详细说明:

=finish

 被引入而不是 Perl 的 
__DATA__
。”

https://www.nntp.perl.org/group/perl.perl6.users/2023/06/msg10997.html

© www.soinside.com 2019 - 2024. All rights reserved.