Is it possible to convert a pod code which is save in a variable in a way that it behaves like a $=pod
when used with pod2text?
#!/usr/bin/env perl6
use v6;
use Pod::To::Text;
my $code = $*PROGRAM.slurp;
my $pod = $code.subst( / ^ .+ \n <?before '=begin pod'> /, '' );
# convert $pod so it works with pod2text like $=pod does
say pod2text $pod;
say "==============";
say pod2text $=pod;
=begin pod
=head1 NAME
Test pod
=head1 DESCRIPTION
This is a test.
=end pod
The $=pod
variable contains an Array
of values that are each an instance of (a sub-class of) Pod::Block
. So you need to reproduce that.
If you compile some P6 code with a P6 compiler then any Pod in it is supposed to be automatically compiled, and the $=pod
variable automatically initialized to contain the results of the Pod compilation. Using Rakudo:
=begin foo
foo-text
=end foo
say $=pod;
displays:
[Pod::Block::Named{:name("foo")}
Pod::Block::Para
foo-text
]
Having glanced over the relevant compiler modules (grammar, actions, compiling wrapper) I suspect it would take a fair amount of effort to understand it. It's possible that the fruit of that understanding will be the ability to use some of this code as is but I suspect it's at least equally likely you can't without refactoring the compiler code to some degree.
The following $pod
will also be accepted by pod2text
:
my $pod =
[Pod::Block::Named.new:
:name("foo"),
:contents[Pod::Block::Para.new:
:contents["foo-text"]]];
say $pod; # displays same result as for `$=pod` above
Presumably the solution you seek is somewhere between these extremes.
You can use Pod::Load
:
This is your program:
use v6;
use Pod::To::Text;
=begin pod
=head1 NAME
Test pod
=head1 DESCRIPTION
This is a test.
=end pod
And this is where you load it:
use Pod::Load;
use Pod::To::Text;
my $pod = load( "get-pod.p6" );
dd $pod;
say pod2text $pod;
Perl6 itself is parsing the Pod along with the rest of the program, so what @raiph has answered is also valid, and, for that matter, the $=pod you have used in your program.
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