代码之家  ›  专栏  ›  技术社区  ›  sid_com

Perl6是否支持相当于Perl5的“数据”和“结束”部分的内容?

  •  14
  • sid_com  · 技术社区  · 14 年前

    Perl6/Rakudo是否有与Perl5相同的东西? __DATA__ __END__ 部分?

    4 回复  |  直到 6 年前
        1
  •  12
  •   zoul    14 年前

    引用 S26 :

    名为perldoc的块,其类型名为 数据是Perl6等价于 Perl 5 __DATA__ 第节。这个 区别在于=数据块 只是普通的豆荚块,可能会出现 源文件中的任何位置,以及 根据需要多次。 Synopsis 2 描述新的Perl6接口 内联数据。

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

    use v6;
    
    =begin DATA
    Foo
    =end DATA
    
    say @=DATA;
    

    In practice it seems 拉库多还不支持这一点。

        2
  •  11
  •   raiph    8 年前

    仔细地有选择地引用电流 S02 设计文件:

    不再有任何特殊的数据流--在 可以通过pod对象访问当前文件…

    您必须自己将[pod block]内容拆分为行。

    [推测]也可以将pod对象视为 IO::处理,逐行读取POD信息(如数据 Perl5中的文件句柄,但用于任何pod块)。

    所以,不是单身 数据 通过读取文件句柄访问的每个文件的节,您可以在脚本文件中定义任意数量的pod块;它们存储在 $=pod 在编译时是变量;您从该变量中读取;而被称为“数据”的变量与Perl5的等价。 数据 .

    这在今天有效。我马上就给你看。但首先,我需要谈谈今天不起作用的事情。

    上面的报价是高度选择性的。省略的文本讨论了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
      }
    };
    
        3
  •  3
  •   Christopher Bottoms zerkms    7 年前

    在这一点完全实现之前,您可以使用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
    
        4
  •  3
  •   Richard Hainsworth    6 年前

    要获取一个数据数组,同时将数据放在程序的底部以帮助可读性,下面是@Christopher Bottoms answer的变体:

    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
             )
    }
    
    推荐文章