How to make behave a pod saved in a variable like `$=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.

Letting the Rakudo compiler do all the work

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.

Working from the ground up

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

A solution?

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.

Tags:

Raku