1Pod::Constants(3) User Contributed Perl Documentation Pod::Constants(3)
2
3
4
6 Pod::Constants - Include constants from POD
7
9 our ($myvar, $VERSION, @myarray, $html, %myhash);
10
11 use Pod::Constants -trim => 1,
12 'Pod Section Name' => \$myvar,
13 'Version' => sub { eval },
14 'Some list' => \@myarray,
15 html => \$html,
16 'Some hash' => \%myhash;
17
18 =head2 Pod Section Name
19
20 This string will be loaded into $myvar
21
22 =head2 Version
23
24 # This is an example of using a closure. $_ is set to the
25 # contents of the paragraph. In this example, "eval" is
26 # used to execute this code at run time.
27 $VERSION = 0.19;
28
29 =head2 Some list
30
31 Each line from this section of the file
32 will be placed into a separate array element.
33 For example, this is $myarray[2].
34
35 =head2 Some hash
36
37 This text will not go into the hash, because
38 it doesn't look like a definition list.
39 key1 => Some value (this will go into the hash)
40 var2 => Some Other value (so will this)
41 wtf = This won't make it in.
42
43 =head2 %myhash's value after the above:
44
45 ( key1 => "Some value (this will go into the hash)",
46 var2 => "Some Other value (so will this)" )
47
48 =begin html <p>This text will be in $html</p>
49
50 =cut
51
53 This module allows you to specify those constants that should be
54 documented in your POD, and pull them out a run time in a fairly
55 arbitrary fashion.
56
57 Pod::Constants uses Pod::Parser to do the parsing of the source file.
58 It has to open the source file it is called from, and does so directly
59 either by lookup in %INC or by assuming it is $0 if the caller is
60 "main" (or it can't find %INC{caller()})
61
62 ARBITARY DECISIONS
63 I have made this code only allow the "Pod Section Name" to match
64 `headN', `item', `for' and `begin' POD sections. If you have a good
65 reason why you think it should match other POD sections, drop me a line
66 and if I'm convinced I'll put it in the standard version.
67
68 For `for' and `begin' sections, only the first word is counted as being
69 a part of the specifier, as opposed to `headN' and `item', where the
70 entire rest of the line counts.
71
73 import(@args)
74 This function is called when we are "use"'d. It determines the source
75 file by inspecting the value of caller() or $0.
76
77 The form of @args is HOOK => $where.
78
79 $where may be a scalar reference, in which case the contents of the POD
80 section called "HOOK" will be loaded into $where.
81
82 $where may be an array reference, in which case the contents of the
83 array will be the contents of the POD section called "HOOK", split into
84 lines.
85
86 $where may be a hash reference, in which case any lines with a "=>"
87 symbol present will have everything on the left have side of the =>
88 operator as keys and everything on the right as values. You do not
89 need to quote either, nor have trailing commas at the end of the lines.
90
91 $where may be a code reference (sub { }), in which case the sub is
92 called when the hook is encountered. $_ is set to the value of the POD
93 paragraph.
94
95 You may also specify the behaviour of whitespace trimming; by default,
96 no trimming is done except on the HOOK names. Setting "-trim => 1"
97 turns on a package "global" (until the next time import is called) that
98 will trim the $_ sent for processing by the hook processing function
99 (be it a given function, or the built-in array/hash splitters) for
100 leading and trailing whitespace.
101
102 The name of HOOK is matched against any "=head1", "=head2", "=item",
103 "=for", "=begin" value. If you specify the special hooknames "*item",
104 "*head1", etc, then you will get a function that is run for every
105
106 Note that the supplied functions for array and hash splitting are
107 exactly equivalent to fairly simple Perl blocks:
108
109 Array:
110
111 HOOK => sub { @array = split /\n/, $_ }
112
113 Hash:
114
115 HOOK => sub {
116 %hash =
117 (map { map { s/^\s+|\s+$//g; $_ } split /=>/, $_ }
118 (grep m/^
119 ( (?:[^=]|=[^>])+ ) # scan up to "=>"
120 =>
121 ( (?:[^=]|=[^>])+ =? )# don't allow more "=>"'s
122 $/x, split /\n/, $_));
123 }
124
125 Well, they're simple if you can grok map, a regular expression like
126 that and a functional programming style. If you can't I'm sure it is
127 probably voodoo to you.
128
129 Here's the procedural equivalent:
130
131 HOOK => sub {
132 for my $line (split /\n/, $_) {
133 my ($key, $value, $junk) = split /=>/, $line;
134 next if $junk;
135 $key =~ s/^\s+|\s+$//g
136 $value =~ s/^\s+|\s+$//g
137 $hash{$key} = $value;
138 }
139 },
140
141 import_from_file($filename, @args)
142 Very similar to straight "import", but you specify the source filename
143 explicitly.
144
145 add_hook(NAME => value)
146 This function adds another hook, it is useful for dynamic updating of
147 parsing through the document.
148
149 For an example, please see t/01-constants.t in the source distribution.
150 More detailed examples will be added in a later release.
151
152 delete_hook(@list)
153 Deletes the named hooks. Companion function to add_hook
154
155 CLOSURES AS DESTINATIONS
156 If the given value is a ref CODE, then that function is called, with $_
157 set to the value of the paragraph. This can be very useful for
158 applying your own custom mutations to the POD to change it from human
159 readable text into something your program can use.
160
161 After I added this function, I just kept on thinking of cool uses for
162 it. The nice, succinct code you can make with it is one of
163 Pod::Constant's strongest features.
164
165 Below are some examples.
166
168 Module Makefile.PL maintenance
169 Tired of keeping those module Makefile.PL's up to date? Note: This
170 method seems to break dh-make-perl.
171
172 Example Makefile.PL
173 eval "use Pod::Constants";
174 ($Pod::Constants::VERSION >= 0.11)
175 or die <<EOF
176 ####
177 #### ERROR: This module requires Pod::Constants 0.11 or
178 #### higher to be installed.
179 ####
180 EOF
181
182 my ($VERSION, $NAME, $PREREQ_PM, $ABSTRACT, $AUTHOR);
183 Pod::Constants::import_from_file
184 (
185 'MyTestModule.pm',
186 'MODULE RELEASE' => sub { ($VERSION) = m/(\d+\.\d+)/ },
187 'DEPENDENCIES' => ($PREREQ_PM = { }),
188 -trim => 1,
189 'NAME' => sub { $ABSTRACT=$_; ($NAME) = m/(\S+)/ },
190 'AUTHOR' => \$AUTHOR,
191 );
192
193 WriteMakefile
194 (
195 'NAME' => $NAME,
196 'PREREQ_PM' => $PREREQ_PM,
197 'VERSION' => $VERSION,
198 ($] >= 5.005 ? ## Add these new keywords supported since 5.005
199 (ABSTRACT => $ABSTRACT,
200 AUTHOR => $AUTHOR) : ()),
201 );
202
203 Corresponding Module
204 =head1 NAME
205
206 MyTestModule - Demonstrate Pod::Constant's Makefile.PL usefulness
207
208 =head2 MODULE RELEASE
209
210 This is release 1.05 of this module.
211
212 =head2 DEPENDENCIES
213
214 The following modules are required to make this module:
215
216 Some::Module => 0.02
217
218 =head2 AUTHOR
219
220 Ima Twat <ima@twat.name>
221
222 =cut
223
224 our $VERSION;
225 use Pod::Constants -trim => 1,
226 'MODULE RELEASE' => sub { ($VERSION) = m/(\d+\.\d+) or die };
227
229 Sam Vilain, <samv@cpan.org>
230
231 Maintained by Marius Gavrilescu, <marius@ieval.ro> since July 2015
232
234 Copyright (C) 2001, 2002, 2007 Sam Vilain. All Rights Reserved.
235
236 Copyright (C) 2015-2016 by Marius Gavrilescu <marius@ieval.ro>.
237
238 This module is free software. It may be used, redistributed and/or
239 modified under the terms of the Perl Artistic License, version 2.
240
241 See the LICENSE file in the root of this distribution for a copy of the
242 Perl Artistic License, version 2.
243
245 I keep thinking it would be nice to be able to import an =item list
246 into an array or something, eg for a program argument list. But I'm
247 not too sure how it would be all that useful in practice; you'd end up
248 putting the function names for callbacks in the pod or something
249 (perhaps not all that bad).
250
251 Would this be useful?
252
253 Pod::Constants::import(Foo::SECTION => \$myvar);
254
255 Debug output is not very readable
256
257
258
259perl v5.32.0 2020-07-28 Pod::Constants(3)