NAME
Pod::Literate - Literate programming through POD, for general applications or specifications managed by AI/Copilot/Claude/ChatGPT
VERSION
Version 0.0.6
SYNOPSIS
podweave.pl Module.pod | pod2text
podtangle.pl Module.pod
perl -e 'use Module; ...'
# Within POD
---------------------------------------
=for literate:command target=filename
---------------------------------------
=begin literate filename
code
=end literate
---------------------------------------
=begin literate Name
code
=end literate
---------------------------------------
=for literate Name
code
=for literate .
---------------------------------------
Inline C<D<name>=some code> declaration
---------------------------------------
#< Reference a name >#
---------------------------------------
=for literate:command section names
---------------------------------------
=for literate:command section usage
---------------------------------------
DESCRIPTION
Literate programming naturally structures code based on its written specification. Traditionally this permitted organization of very large applications in languages that initially focused on single-file development, which slowly extended to multiple files used as basic imports. With the advance of projects spanning many directories and isolation into objects, and the propensity to author code with minimal comments and documentation, literate programming became an historical curiosity.
With AI/LLM tools focused on software development assistance, it is possible to leverage literate programming to build specifications for applications and interfaces. Given the natural-language focus of this approach, LLMs benefit from and can better maintain consistency with specifications. Code again becomes focused on small, isolated, well-tested goals that can be merged together to form larger systems.
These specifications can be processed in two ways. A tangle command permits coalescing all named sections into the final application. A weave operation generates the specification with all relevant visible elements. In Pod::Literate, the weave application is perlpod itself and its associated formatters (pod2man etc.).
Backwards Incompatibility
IMPORTANT: This is an early version. Monitor the version change logs for additional information about forthcoming functional changes.
- Section declaration versus addition
-
Expect the behavior of
literate Nameandliterate +Nameto change in the future. Specifically, it is likely that the additive form will be required for all concatenated sections in the future, as a mechanism to clearly state "This section may be extended". - Unused sections
-
Unused sections may become failures, warnings, etc.
- Weave behavior
-
Weave is currently a lightweight mechanism to fix up the source POD with a small number of tricks. There may be circumstances where a full POD rescan becomes necessary. Currently indexing, for example, is handled through the Unix trick of creating index files, but for obvious reasons this may not be ideal in the long term.
FORMAT FEATURES
The module, Pod::Literate, is implemented using the formatting features of perlpod. At this time, either =begin or =for may be used to declare named sections of code. They have different end-of-line behaviors. Both declarations are visible when using the weave command, but perlpod will hide the =begin sections if weave is not used first.
begin and end
The begin and end command paragraphs are used to create named sections of code, and must appear in pairs:
=begin literate Descriptive name
...
=end literate
When processing the start of these blocks, a title attribute is set with the "Descriptive name" which is used to identify the target section into which the text contents will be stored. The begin/end is best suited for code blocks, such as complete subroutines or functions, because the final blank line before the =end will be included in the code. These sections will be hidden with plain perlpod, but are visible when using weave.
Notice, at this time, that the classic additive syntax for a named section is supported, in particular
=begin literate +Descriptive name
...
=end literate
but this is equivalent to the non-additive name. In particular, if sections appear more than once, the contents are always concatenated. (This is subject to change, additional warnings, etcetera).
for
The =for command paragraph may also be used to create named sections of code, and must appear paired with and ending for statement:
=for literate Descriptive name
...
=for literate .
The stand-alone period is mandatory to signal the end of content collection. The target section is still identified by the "Descriptive name". The for/for is best suited for referenced lines of code, one or multiple lines, because the final blank line before the ending =for will not be included in the code. These sections will be visible with plain perlpod, and are visible when using weave.
The additive syntax also works for sections created with =for.
Inline declarations
An inline definition is formatted code within a paragraph containing a declared name, indicated with the 'D' formatting code. It has the following form:
C<D<name>=some code>
The equal sign is not required, but any sequence of leading spaces and a single equal sign will be removed from the defined code. That is:
C<D<name> code> becomes "code"
C<D<name> = 5> becomes " 5"
C<D<name>===5> becomes "==5"
Section names
Sections named in =begin and =for tags are limited only by perlpod functionality. Each name is referenced in code by placing it within commented identifiers as:
#< Descriptive name >#
Leading and trailing spaces are ignored, but otherwise only the sequence ># would be illegal in a name.
Vertical Spacing
The behavior of single and concatenated begin and for blocks differs with regards to blank lines. Blank lines appear
when after each reference between concatenations
using begin/end yes yes
using for/for no no
Consider this example:
=begin literate subs
sub AAA {
#< for lines >#
}
=end literate
=begin literate subs
sub BBB {
#< for lines >#
}
=end literate
=for literate for lines
# comment 1
=for literate .
=for literate for lines
# comment 2
=for literate .
The result of tangle will be:
sub AAA {
# comment 1
# comment 2
}
(blank line here)
sub BBB {
# comment 1
# comment 2
}
(blank line here)
For greater control, defined #< blank ># and place it as needed.
Horizontal Spacing
References are included verbatim where they appear, so begin/end blocks are typically not appropriate in the middle of lines. Consider
=begin literate example
if(#< begin inline >#) { #< for inline ># }
=end literate
=begin literate begin inline
begin line
=end literate
=for literate for inline
for line
=for literate .
The result of tangle will be:
if(begin line
) { for line }
Note that #< for inline >##< for inline ># will yield two references without an inside newline, but multiple, concatenated declarations of =for literate will still have a blank line between each.
This permits declarations such as
=for literate header
=head
=for literate .
to be used in cases such as #<header>#1 Title.
COMMANDS
Output
An output filename should be specified via
=for literate:command target=Module.pm
Multiple targets may be specified by repeating the command, once for each output file. Each target filename must have a defined section =begin literate filename or =for literate filename. Relative and full path specifications are permitted.
Note that the weave command still produces single-file output and does not split documentation into multiple files.
The old syntax will be deprecated: =for literate target:Module.pm
Indexes
By default, podtangle generates an index file matching the source filename suffixed with the extension ".idx". The index contains all declared code section names, the sub/sections in the document where the declarations were made, and the calling section.
The index of section name declarations may be obtained by including the following in the POD:
=for literate:command section names
A cross reference of section names and callers may be obtained with
=for literate:command section usage
The linking limitations of POD apply. It's recommended that headings/items be unique to avoid linking ambiguity.
TODO
The following are being considered. No particular priority order here.
- todo Support
=for literate:command import=filename -
Documents may appear in sections, and one large POD can become difficult to manage. With a specialized weave command, it would be sensible to support imports.
- done Handle multiple output filenames
-
Multiple
target=filenamecalls can be made. - todo Detect/reject invalid sequences like begin/for, begin/begin, for title/for title
- todo Blank code lines
-
Blank lines after subroutines have improved with updates to
=begin, but there are still cases where it may be worthwhile to support additional filtering instructions during tangling, or when sections are loaded.This may be entirely the responsibility of a later style tool, but it should be possible to get actual blank lines without having to put those blank lines around the reference. Maybe some
#nlor equivalent, which would still look nicer than some lengthy# blank line #type of comment.Another approach would be, since this is Perl POD, to permit enabling spaces around balanced blocks that are denoted, such as
sub. - done Cross reference
-
Lexicographic index of all section name declarations. Cross reference of callers. Not well tested.
- todo Tests
-
Generally tests will appear in different sections, but it's possible to write a specification where the tests are associated with each section. Unfortunately, for concatenated sections the tests might be more difficult to arrange prior to tangling.
- todo Reference wildcards
-
Support
#< Name.* >#to select all matching names, alphabetical order - todo Tangle and Weave options
-
Weave should automatically insert headers, eg "Generated by..." or "Edit the original at...".
Tangle warnings versus error conditions.
Tangle/Weave automatic insertion of "(this code has not yet been defined)".
SEE ALSO
* perlpod