Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make behave a pod saved in a variable like `$=pod`?

Tags:

raku

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
like image 668
sid_com Avatar asked May 13 '19 07:05

sid_com


2 Answers

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.

like image 80
raiph Avatar answered Oct 19 '22 01:10

raiph


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.

like image 28
jjmerelo Avatar answered Oct 19 '22 01:10

jjmerelo