Does perl6/Rakudo have something equivalent to perl5's __DATA__
or __END__
sections?
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
}
};
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With