不perl6 / Rakudo有相当于的perl5的__DATA____END__部分东西吗?

有帮助吗?

解决方案

S26 :

  

命名的Perldoc块,其类型名是   DATA是Perl 6的等效的所述   Perl 5的__DATA__部分。该   不同的是,=数据块   只是经常波德块,然后可能会出现   内源文件的任何地方,并作为   需要多次。 概要2   介绍了新的Perl 6的界面   内联数据。

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

use v6;

=begin DATA
Foo
=end DATA

say @=DATA;

实际上似乎该Rakudo不支持,但

其他提示

要仔细选择地引用目前 S02 设计文件:

  

有不再有任何特殊数据流 - 在任何波德块   当前文件可以经由波德对象来访问...

     

您必须分裂[波德块]内容转换成线自己。

     

[投机]也可以是可能的治疗波德对象作为   IO ::把手,以读取信息波德线由行(如DATA   文件句柄在Perl 5,但对于任何波德块)。

所以,代替单一的 DATA 每个文件您通过读取文件句柄访问部分,可以定义任意数量的在脚本文件波德块;他们存储在编译时$=pod变量;您从可变的读取;和所谓的 '数据' 的那些是Perl的的等同物5层的 DATA

这工作今天。我将展示在某一时刻。但首先,我需要谈论的东西,今天的工作没有。

上面的引用是高度选择性的。消隐文字谈到了P6与对应于名称“富”波德块形式$=foo的名字自动创建一个变量。这是波德块,而不只是数据块的一般仍然未实现的功能。

的波德设计文档 S26 的“数据块”部分有关的数据块会谈做一些票友的东西比普通的老荚块。这尚未实现任一

那么,现在让我们来说明是什么可以做今天的举动:

=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
  }
};

作为变通,直到此得到充分执行,您可以使用here文档。

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下装回答的变化:

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
         )
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top