Pod::Constants(3pm)
π Pod::Constants π
okay bestie, gather round, because this is literally Pod::Constants β the manual page for it, and she's a whole vibe. β¨
πΈ NAME πΈ
Pod::Constants β include constants straight out of your POD. yes fr, your docs become your data. π
π SYNOPSIS π
like, here's the whole mood board:
our ($myvar, $VERSION, @myarray, $html, %myhash);
use Pod::Constants -trim => 1,
'Pod Section Name' => \$myvar,
'Version' => sub { eval },
'Some list' => \@myarray,
html => \$html,
'Some hash' => \%myhash;
=head2 Pod Section Name
This string will be loaded into $myvar
=head2 Version
# This is an example of using a closure. $_ is set to the
# contents of the paragraph. In this example, "eval" is
# used to execute this code at run time.
$VERSION = 0.19;
=head2 Some list
Each line from this section of the file
will be placed into a separate array element.
For example, this is $myarray[2].
=head2 Some hash
This text will not go into the hash, because
it doesn't look like a definition list.
key1 => Some value (this will go into the hash)
var2 => Some Other value (so will this)
wtf = This won't make it in.
=head2 %myhash's value after the above:
( key1 => "Some value (this will go into the hash)",
var2 => "Some Other value (so will this)" )
=begin html <p>This text will be in $html</p>
=cut
see how the POD is doing double duty? iconic behaviour, no notes. π«Ά
π¦ DESCRIPTION π¦
so the whole point of this module is: you get to decide which constants should be documented in your POD, and then pull them out at run time in a fairly arbitrary fashion. it's giving "my docs ARE my config."
Pod::Constants uses Pod::Parser to do the parsing of the source file. it has to open the source file it is called from, and it does so directly either by lookup in %INC or by assuming it is $0 if the caller is main (or if it can't find %INC{caller()}).
π ARBITARY DECISIONS π
(spelling courtesy of the original author, we don't touch that π )
I have made this code only allow the "Pod Section Name" to match headN, item, for and begin POD sections. If you have a good reason why you think it should match other POD sections, drop me a line and if I'm convinced I'll put it in the standard version.
For for and begin sections, only the first word is counted as being a part of the specifier, as opposed to headN and item, where the entire rest of the line counts.
β¨ FUNCTIONS β¨
import(@args)
This function is called when we are use'd. It determines the source file by inspecting the value of caller() or $0.
The form of @args is HOOK => $where.
$wheremay be a scalar reference, in which case the contents of the POD section called"HOOK"will be loaded into$where. π$wheremay be an array reference, in which case the contents of the array will be the contents of the POD section called"HOOK", split into lines.$wheremay be a hash reference, in which case any lines with a=>symbol present will have everything on the left have side of the=>operator as keys and everything on the right as values. You do not need to quote either, nor have trailing commas at the end of the lines.$wheremay be a code reference (sub { }), in which case the sub is called when the hook is encountered.$_is set to the value of the POD paragraph.
You may also specify the behaviour of whitespace trimming; by default, no trimming is done except on the HOOK names. Setting -trim => 1 turns on a package "global" (until the next time import is called) that will trim the $_ sent for processing by the hook processing function (be it a given function, or the built-in array/hash splitters) for leading and trailing whitespace.
The name of HOOK is matched against any "=head1", "=head2", "=item", "=for", "=begin" value. If you specify the special hooknames "*item", "*head1", etc, then you will get a function that is run for every
(yes bestie, that sentence just... stops. that's how the original ends. we reproduce, we don't fix. π)
Note that the supplied functions for array and hash splitting are exactly equivalent to fairly simple Perl blocks:
Array:
HOOK => sub { @array = split /\n/, $_ }
Hash:
HOOK => sub {
%hash =
(map { map { s/^\s+|\s+$//g; $_ } split /=>/, $_ }
(grep m/^
( (?:[^=]|=[^>])+ ) # scan up to "=>"
=>
( (?:[^=]|=[^>])+ =? )# don't allow more "=>"'s
$/x, split /\n/, $_));
}
Well, they're simple if you can grok map, a regular expression like that and a functional programming style. If you can't I'm sure it is probably voodoo to you.
Here's the procedural equivalent:
HOOK => sub {
for my $line (split /\n/, $_) {
my ($key, $value, $junk) = split /=>/, $line;
next if $junk;
$key =~ s/^\s+|\s+$//g
$value =~ s/^\s+|\s+$//g
$hash{$key} = $value;
}
},
import_from_file($filename, @args)
Very similar to straight "import", but you specify the source filename explicitly. π
add_hook(NAME => value)
This function adds another hook, it is useful for dynamic updating of parsing through the document.
For an example, please see t/01-constants.t in the source distribution. More detailed examples will be added in a later release.
delete_hook(@list)
Deletes the named hooks. Companion function to add_hook.
π CLOSURES AS DESTINATIONS π
If the given value is a ref CODE, then that function is called, with $_ set to the value of the paragraph. This can be very useful for applying your own custom mutations to the POD to change it from human readable text into something your program can use.
After I added this function, I just kept on thinking of cool uses for it. The nice, succinct code you can make with it is one of Pod::Constant's strongest features.
Below are some examples.
π EXAMPLES π
π οΈ Module Makefile.PL maintenance
Tired of keeping those module Makefile.PL's up to date? Note: This method seems to break dh-make-perl.
π Example Makefile.PL
eval "use Pod::Constants";
($Pod::Constants::VERSION >= 0.11)
or die <<EOF
####
#### ERROR: This module requires Pod::Constants 0.11 or
#### higher to be installed.
####
EOF
my ($VERSION, $NAME, $PREREQ_PM, $ABSTRACT, $AUTHOR);
Pod::Constants::import_from_file
(
'MyTestModule.pm',
'MODULE RELEASE' => sub { ($VERSION) = m/(\d+\.\d+)/ },
'DEPENDENCIES' => ($PREREQ_PM = { }),
-trim => 1,
'NAME' => sub { $ABSTRACT=$_; ($NAME) = m/(\S+)/ },
'AUTHOR' => \$AUTHOR,
);
WriteMakefile
(
'NAME' => $NAME,
'PREREQ_PM' => $PREREQ_PM,
'VERSION' => $VERSION,
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT => $ABSTRACT,
AUTHOR => $AUTHOR) : ()),
);
π§ Corresponding Module
=head1 NAME
MyTestModule - Demonstrate Pod::Constant's Makefile.PL usefulness
=head2 MODULE RELEASE
This is release 1.05 of this module.
=head2 DEPENDENCIES
The following modules are required to make this module:
Some::Module => 0.02
=head2 AUTHOR
Ima Twat <ima@twat.name>
=cut
our $VERSION;
use Pod::Constants -trim => 1,
'MODULE RELEASE' => sub { ($VERSION) = m/(\d+\.\d+) or die };
π AUTHOR π
Sam Vilain, <samv@cpan.org>
Maintained by Marius Gavrilescu, <marius@ieval.ro> since July 2015
π COPYRIGHT AND LICENSE π
Copyright (C) 2001, 2002, 2007 Sam Vilain. All Rights Reserved.
Copyright (C) 2015-2016 by Marius Gavrilescu <marius@ieval.ro>.
This module is free software. It may be used, redistributed and/or modified under the terms of the Perl Artistic License, version 2.
See the LICENSE file in the root of this distribution for a copy of the Perl Artistic License, version 2.
π BUGS/TODO π
I keep thinking it would be nice to be able to import an =item list into an array or something, eg for a program argument list. But I'm not too sure how it would be all that useful in practice; you'd end up putting the function names for callbacks in the pod or something (perhaps not all that bad).
Would this be useful?
Pod::Constants::import(Foo::SECTION => \$myvar);
Debug output is not very readable
fr fr, that's the whole page. go forth and put your constants in your POD, bestie. π β¨
and that's a wrap β this page came to you courtesy of perl v5.30.0, dated 2020-05-17. mwah. π«Άπ
π¦ where this page comes from
- debian bookworm: libpod-constants-perl 0.19-2 Β· Artistic-2.0
- debian trixie: libpod-constants-perl 0.19-2 Β· Artistic-2.0
- ubuntu jammy: libpod-constants-perl 0.19-2 Β· Artistic-2.0
- ubuntu noble: libpod-constants-perl 0.19-2 Β· Artistic-2.0
- ubuntu resolute: libpod-constants-perl 0.19-2 Β· Artistic-2.0
- ubuntu stonking: libpod-constants-perl 0.19-2 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms