4

I have a bunch of repeated text in my perlpod documentation. I could of course create a separate section and reference it, but I was wondering if there's a way to enter the text once somewhere and have it inserted in multiple places?

I don't think this is possible, but thought I'd ask to make sure I'm not missing anything.

Or - perhaps there's a better perl documentation technique?

hepcat72
  • 890
  • 4
  • 22
  • 2
    I'm not recommending this (I prefer documentation to be statically written, since it is not as dynamic as code) but [Pod::Weaver](https://metacpan.org/pod/Pod::Weaver) can generate pod and can be invoked with [Dist::Zilla](https://metacpan.org/pod/Dist::Zilla::Plugin::PodWeaver) or [App::podweaver](https://metacpan.org/pod/App::podweaver). – Grinnz Nov 05 '19 at 16:56
  • 1
    You could also generate the pod from a template. Some template language (e.g. Template-Toolkit) are pretty rich and could definitely achieve what you want. But it wouldn't have the feel of code. – ikegami Nov 05 '19 at 20:09
  • As a reader, seeing the same text over and over again annoys me. As a writer, I think you're doing something wrong. Looking for a technical solution to a bad idea is also a bad idea. – brian d foy Jun 01 '20 at 21:59
  • @brian d foy - If you're reading perlpod as a book instead of as a lookup reference, I'd agree. – hepcat72 Jun 01 '20 at 22:02

2 Answers2

7

As you've realised, Pod is (deliberately) a very simple markup language. It doesn't have any particularly complicated feature and one of the things it is missing is a way to embed text from another source.

I'd suggest moving the repeated text to its own section and linking to that section (using L<...>) whenever you want to reference that text.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Is there a way to make those links do anything in a terminal? I've never seen them have any utility for navigation in a pager. What pager do you use (if those links do anything for you)? – hepcat72 Jun 01 '20 at 22:25
3

While Pod markup is meant to be very basic, we don't have to literally type it all by hand.

Text for documentation can be processed like any other text in Perl, using its extensive set of tools, to create a string with pod-formatted text. That string can then be displayed using core Pod::Usage, via a file (that can be removed or kept), or directly by using core Pod::Simple.

Display the Pod string by writing a file

use warnings;
use strict;
use feature 'say';
use Path::Tiny;    # convenience, to "spew" a file    

my $man = shift;        
show_pod() if $man;
say "done $$";

sub show_pod {
    require Pod::Usage; Pod::Usage->import('pod2usage');

    my $pod_text = make_pod();
    my $pod_file = $0 =~ s/\.pl$/.pod/r;
    path($pod_file)->spew($pod_text);

    pod2usage( -verbose => 2, -input => $pod_file );  # exits by default
}

sub make_pod {
    my $repeated = q(lotsa text that gets repeated);
    my $doc_text = <<EOD;
=head1 NAME

$0 - demo perldoc

=head1 SYNOP...

Text that is also used elsewhere: $repeated...

=cut
EOD

    return $doc_text;
}

The .pod file can be removed: add -exitval => 'NOEXIT' to pod2usage arguments so that it doesn't exit and then remove the file. Or, rather, keep it available for other tools and uses.

I've split the job into two subs as a hint, since it can be useful to be able to only write a .pod file, which can then also be used and viewed in other ways and formats. This isn't needed to just show docs, and all Pod business can be done in one sub.

Dispaly the Pod string directly

If there is no desire to keep a .pod file around then we don't have to make it

sub show_pod {                  # The rest of the program same as above
    my $pod_text = make_pod();

    require Pod::Simple::Text;
    Pod::Simple::Text->filter( \$pod_text );  # doesn't exit so add that
}

The ->filter call is a shortcut for creating an object, setting a filehandle, and processing the contents. See docs for a whole lot more.

Either of these approaches provides you with far more flexibility.

Also, while one can indeed solve the repeated text by referencing a separate section with that text, that of course doesn't allow us to use variables or do any Perl processing -- what is all available when you write a Pod string, which is then passed over to perlpod to dispaly.

Note   Use of .pod files affects perldoc. Thanks to @briandfoy for a comment.

For smaller documentation, that has no particular benefit of using separate .pod files, I recommend the second approach, as hinted in the answer. It only differs in how the docs text is organized in the file while still allowing one to process it as any text is normally processed with Perl.

For use cases where .pod files are of good value I still find it an acceptable trade-off but that is my call. Be aware that perldoc is affected and assess how much that matters in your project.


I use a system like this in a large project, with .pod files in their directory. There is also a simple separate script for overall documentation management, that invokes individual programs with options to write/update their .pods, in HTML with CPAN's style-file, for the main web page. Any program can also simply display its docs in a desired format.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • Note that `perldoc` will preferentially chose a _.pod_ file over a _.pm_ file, which means `perldoc -m` doesn't get you to the code anymore. It's not wrong to have use _.pod_ files, but it certainly annoys me. – brian d foy Jun 01 '20 at 22:01
  • @briandfoy Thank you for the comment, that needed be mentioned. (I don't use `-m` on code with docs prepared this way so I didn't know.) I fear that if I can only use in-file docs (so to call them) then I get restricted quite a bit. The project I mention would then need a non-native tool for its intended documentation, while the use of separate `.pod` files made POD itself fully adequate. – zdim Jun 01 '20 at 23:48
  • 1
    @briandfoy Please note that the other option I offer doesn't have that problem (`perldoc` works with it) and yet allows full use of Perl on generating and processing documentation text. I find it silly to be reduced to quoting exact text in order to be able to use docs for more than formatting. While POD is basic it is not _that_ basic. I added a note on all this at the end, thank you. – zdim Jun 01 '20 at 23:56