Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Perl6 support something equivalent to Perl5's __DATA__ and __END__ sections?

Tags:

perl

raku

rakudo

Does perl6/Rakudo have something equivalent to perl5's __DATA__ or __END__ sections?

like image 264
sid_com Avatar asked Nov 24 '10 16:11

sid_com


3 Answers

To carefully selectively quote the current S02 design document:

There is no longer any special DATA stream--any Pod block in the current file can be accessed via a Pod object ...

You have to split [Pod block] contents into lines yourself.

[Speculative] It may also be possible to treat a Pod object as an IO::Handle, to read the Pod information line-by-line (like the DATA filehandle in Perl 5, but for any Pod block).

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:

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

This prints:

A data block -- a Pod block with the name 'data'.
Another data block. This time a multi line one.

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:

for @$=pod {
  if .name eq 'data' {
    say .contents[0].contents
  }
};
like image 28
raiph Avatar answered Oct 23 '22 03:10

raiph


Quote S26:

Named Perldoc blocks whose typename is DATA are the Perl 6 equivalent of the Perl 5 __DATA__ section. The difference is that =DATA blocks are just regular Pod blocks and may appear anywhere within a source file, and as many times as required. Synopsis 2 describes the new Perl 6 interface for inline data.

In theory you should be able to do something like this (somebody please fix the syntax if it’s off):

use v6;

=begin DATA
Foo
=end DATA

say @=DATA;

In practice it seems that Rakudo does not support that, yet.

like image 190
zoul Avatar answered Oct 23 '22 04:10

zoul


As a work-around until this gets fully implemented, you can use heredocs.

for data().lines -> $line {
    put $line;
}

sub data {
    return q:to/END/;
           Foo, bar, baz
           1, 2, 3
           END
}

Outputs

Foo, bar, baz
1, 2, 3
like image 3
Christopher Bottoms Avatar answered Oct 23 '22 05:10

Christopher Bottoms