Skip to content

Instantly share code, notes, and snippets.

@MARTIMM
Last active June 8, 2018 17:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MARTIMM/516b3ade0500428cedc28ae86fad6a1b to your computer and use it in GitHub Desktop.
Save MARTIMM/516b3ade0500428cedc28ae86fad6a1b to your computer and use it in GitHub Desktop.
Use perl6 '=begin comment' pod blocks as data blocks
#!/usr/bin/env perl6
#`{{
Perl6 datablock are not yet implemented so $=data will generate an error.
After some testing I also found that the data between e.g. '=begin data' and
'=end data' are scooped up into one single string without any newline (\n).
This, I find, is an important character needed to separate lines. so
=begin data
a 1
b 2
=end data
is stored somewhere in the pod structure as "a 1 b 2" which is not easily
processable later on.
Studying the comment structures however, the newline character is kept in
those structures. The below code processes such a structure and adds some keys to
the comments to have some control on what to process
e.g.
=begin comment :!comment :some-key
# two datalines of interesting data
foo 1
bar 2
=end comment
This can then be called by get-data('some-key') to get the data in the comment
blocks. Here, three things are important;
1) :!comment is checked by the sub so it must be there. It is also a sign to
the reader that this block is not used to show comments.
2) :some-key is the key to use to search for data. More than one data block
is then possible and other (real) comments are not processed.
3) The sub is filtering out empty lines and comment lines (starting with '#')
These are the last few lines in the sub. You can leave that out if you like
}}
use v6;
# Program returns;
#Data line: foo 1
#Data line: bar 2
my Str $foo-data = get-data('fooDataBlock');
for $foo-data.lines -> $line {
say "Data line: $line";
}
# data block
=begin comment :!comment :fooDataBlock
# test van data
foo 1
# second line
bar 2
=end comment
# the sub that returns the data from the block
sub get-data ( Str:D $content-key --> Str ) {
my Str $content;
# search through the pod structure
for @$=pod -> $pd {
# search for 1) pod comment block, 2) :!comment and 3) the users key
if $pd ~~ Pod::Block::Comment and # comment block
$pd.config<comment>:exists and # must have comment key
!$pd.config<comment> and # and comment must be False
$pd.config{$content-key}:exists { # and the selection key must be there
$content = $pd.contents[0];
last;
}
}
# remove comments
$content ~~ s:g/\s* '#' .*? $$//;
# remove empty lines
$content ~~ s:g/^^ \s* $$//;
$content ~~ s:g/\n\n+/\n/;
$content ~~ s:g/^\n+//;
$content ~~ s:g/\n+$//;
$content
}
@MARTIMM
Copy link
Author

MARTIMM commented Jun 8, 2018

changed the test of the pod block a bit. It failed on real comment blocks showing uninitialized value %!config ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment