source: trunk/essentials/dev-lang/perl/pod/perlpodspec.pod@ 3184

Last change on this file since 3184 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 65.5 KB
Line 
1
2=head1 NAME
3
4perlpodspec - Plain Old Documentation: format specification and notes
5
6=head1 DESCRIPTION
7
8This document is detailed notes on the Pod markup language. Most
9people will only have to read L<perlpod|perlpod> to know how to write
10in Pod, but this document may answer some incidental questions to do
11with parsing and rendering Pod.
12
13In this document, "must" / "must not", "should" /
14"should not", and "may" have their conventional (cf. RFC 2119)
15meanings: "X must do Y" means that if X doesn't do Y, it's against
16this specification, and should really be fixed. "X should do Y"
17means that it's recommended, but X may fail to do Y, if there's a
18good reason. "X may do Y" is merely a note that X can do Y at
19will (although it is up to the reader to detect any connotation of
20"and I think it would be I<nice> if X did Y" versus "it wouldn't
21really I<bother> me if X did Y").
22
23Notably, when I say "the parser should do Y", the
24parser may fail to do Y, if the calling application explicitly
25requests that the parser I<not> do Y. I often phrase this as
26"the parser should, by default, do Y." This doesn't I<require>
27the parser to provide an option for turning off whatever
28feature Y is (like expanding tabs in verbatim paragraphs), although
29it implicates that such an option I<may> be provided.
30
31=head1 Pod Definitions
32
33Pod is embedded in files, typically Perl source files -- although you
34can write a file that's nothing but Pod.
35
36A B<line> in a file consists of zero or more non-newline characters,
37terminated by either a newline or the end of the file.
38
39A B<newline sequence> is usually a platform-dependent concept, but
40Pod parsers should understand it to mean any of CR (ASCII 13), LF
41(ASCII 10), or a CRLF (ASCII 13 followed immediately by ASCII 10), in
42addition to any other system-specific meaning. The first CR/CRLF/LF
43sequence in the file may be used as the basis for identifying the
44newline sequence for parsing the rest of the file.
45
46A B<blank line> is a line consisting entirely of zero or more spaces
47(ASCII 32) or tabs (ASCII 9), and terminated by a newline or end-of-file.
48A B<non-blank line> is a line containing one or more characters other
49than space or tab (and terminated by a newline or end-of-file).
50
51(I<Note:> Many older Pod parsers did not accept a line consisting of
52spaces/tabs and then a newline as a blank line -- the only lines they
53considered blank were lines consisting of I<no characters at all>,
54terminated by a newline.)
55
56B<Whitespace> is used in this document as a blanket term for spaces,
57tabs, and newline sequences. (By itself, this term usually refers
58to literal whitespace. That is, sequences of whitespace characters
59in Pod source, as opposed to "EE<lt>32>", which is a formatting
60code that I<denotes> a whitespace character.)
61
62A B<Pod parser> is a module meant for parsing Pod (regardless of
63whether this involves calling callbacks or building a parse tree or
64directly formatting it). A B<Pod formatter> (or B<Pod translator>)
65is a module or program that converts Pod to some other format (HTML,
66plaintext, TeX, PostScript, RTF). A B<Pod processor> might be a
67formatter or translator, or might be a program that does something
68else with the Pod (like wordcounting it, scanning for index points,
69etc.).
70
71Pod content is contained in B<Pod blocks>. A Pod block starts with a
72line that matches <m/\A=[a-zA-Z]/>, and continues up to the next line
73that matches C<m/\A=cut/> -- or up to the end of the file, if there is
74no C<m/\A=cut/> line.
75
76=for comment
77 The current perlsyn says:
78 [beginquote]
79 Note that pod translators should look at only paragraphs beginning
80 with a pod directive (it makes parsing easier), whereas the compiler
81 actually knows to look for pod escapes even in the middle of a
82 paragraph. This means that the following secret stuff will be ignored
83 by both the compiler and the translators.
84 $a=3;
85 =secret stuff
86 warn "Neither POD nor CODE!?"
87 =cut back
88 print "got $a\n";
89 You probably shouldn't rely upon the warn() being podded out forever.
90 Not all pod translators are well-behaved in this regard, and perhaps
91 the compiler will become pickier.
92 [endquote]
93 I think that those paragraphs should just be removed; paragraph-based
94 parsing seems to have been largely abandoned, because of the hassle
95 with non-empty blank lines messing up what people meant by "paragraph".
96 Even if the "it makes parsing easier" bit were especially true,
97 it wouldn't be worth the confusion of having perl and pod2whatever
98 actually disagree on what can constitute a Pod block.
99
100Within a Pod block, there are B<Pod paragraphs>. A Pod paragraph
101consists of non-blank lines of text, separated by one or more blank
102lines.
103
104For purposes of Pod processing, there are four types of paragraphs in
105a Pod block:
106
107=over
108
109=item *
110
111A command paragraph (also called a "directive"). The first line of
112this paragraph must match C<m/\A=[a-zA-Z]/>. Command paragraphs are
113typically one line, as in:
114
115 =head1 NOTES
116
117 =item *
118
119But they may span several (non-blank) lines:
120
121 =for comment
122 Hm, I wonder what it would look like if
123 you tried to write a BNF for Pod from this.
124
125 =head3 Dr. Strangelove, or: How I Learned to
126 Stop Worrying and Love the Bomb
127
128I<Some> command paragraphs allow formatting codes in their content
129(i.e., after the part that matches C<m/\A=[a-zA-Z]\S*\s*/>), as in:
130
131 =head1 Did You Remember to C<use strict;>?
132
133In other words, the Pod processing handler for "head1" will apply the
134same processing to "Did You Remember to CE<lt>use strict;>?" that it
135would to an ordinary paragraph -- i.e., formatting codes (like
136"CE<lt>...>") are parsed and presumably formatted appropriately, and
137whitespace in the form of literal spaces and/or tabs is not
138significant.
139
140=item *
141
142A B<verbatim paragraph>. The first line of this paragraph must be a
143literal space or tab, and this paragraph must not be inside a "=begin
144I<identifier>", ... "=end I<identifier>" sequence unless
145"I<identifier>" begins with a colon (":"). That is, if a paragraph
146starts with a literal space or tab, but I<is> inside a
147"=begin I<identifier>", ... "=end I<identifier>" region, then it's
148a data paragraph, unless "I<identifier>" begins with a colon.
149
150Whitespace I<is> significant in verbatim paragraphs (although, in
151processing, tabs are probably expanded).
152
153=item *
154
155An B<ordinary paragraph>. A paragraph is an ordinary paragraph
156if its first line matches neither C<m/\A=[a-zA-Z]/> nor
157C<m/\A[ \t]/>, I<and> if it's not inside a "=begin I<identifier>",
158... "=end I<identifier>" sequence unless "I<identifier>" begins with
159a colon (":").
160
161=item *
162
163A B<data paragraph>. This is a paragraph that I<is> inside a "=begin
164I<identifier>" ... "=end I<identifier>" sequence where
165"I<identifier>" does I<not> begin with a literal colon (":"). In
166some sense, a data paragraph is not part of Pod at all (i.e.,
167effectively it's "out-of-band"), since it's not subject to most kinds
168of Pod parsing; but it is specified here, since Pod
169parsers need to be able to call an event for it, or store it in some
170form in a parse tree, or at least just parse I<around> it.
171
172=back
173
174For example: consider the following paragraphs:
175
176 # <- that's the 0th column
177
178 =head1 Foo
179
180 Stuff
181
182 $foo->bar
183
184 =cut
185
186Here, "=head1 Foo" and "=cut" are command paragraphs because the first
187line of each matches C<m/\A=[a-zA-Z]/>. "I<[space][space]>$foo->bar"
188is a verbatim paragraph, because its first line starts with a literal
189whitespace character (and there's no "=begin"..."=end" region around).
190
191The "=begin I<identifier>" ... "=end I<identifier>" commands stop
192paragraphs that they surround from being parsed as data or verbatim
193paragraphs, if I<identifier> doesn't begin with a colon. This
194is discussed in detail in the section
195L</About Data Paragraphs and "=beginE<sol>=end" Regions>.
196
197=head1 Pod Commands
198
199This section is intended to supplement and clarify the discussion in
200L<perlpod/"Command Paragraph">. These are the currently recognized
201Pod commands:
202
203=over
204
205=item "=head1", "=head2", "=head3", "=head4"
206
207This command indicates that the text in the remainder of the paragraph
208is a heading. That text may contain formatting codes. Examples:
209
210 =head1 Object Attributes
211
212 =head3 What B<Not> to Do!
213
214=item "=pod"
215
216This command indicates that this paragraph begins a Pod block. (If we
217are already in the middle of a Pod block, this command has no effect at
218all.) If there is any text in this command paragraph after "=pod",
219it must be ignored. Examples:
220
221 =pod
222
223 This is a plain Pod paragraph.
224
225 =pod This text is ignored.
226
227=item "=cut"
228
229This command indicates that this line is the end of this previously
230started Pod block. If there is any text after "=cut" on the line, it must be
231ignored. Examples:
232
233 =cut
234
235 =cut The documentation ends here.
236
237 =cut
238 # This is the first line of program text.
239 sub foo { # This is the second.
240
241It is an error to try to I<start> a Pod block with a "=cut" command. In
242that case, the Pod processor must halt parsing of the input file, and
243must by default emit a warning.
244
245=item "=over"
246
247This command indicates that this is the start of a list/indent
248region. If there is any text following the "=over", it must consist
249of only a nonzero positive numeral. The semantics of this numeral is
250explained in the L</"About =over...=back Regions"> section, further
251below. Formatting codes are not expanded. Examples:
252
253 =over 3
254
255 =over 3.5
256
257 =over
258
259=item "=item"
260
261This command indicates that an item in a list begins here. Formatting
262codes are processed. The semantics of the (optional) text in the
263remainder of this paragraph are
264explained in the L</"About =over...=back Regions"> section, further
265below. Examples:
266
267 =item
268
269 =item *
270
271 =item *
272
273 =item 14
274
275 =item 3.
276
277 =item C<< $thing->stuff(I<dodad>) >>
278
279 =item For transporting us beyond seas to be tried for pretended
280 offenses
281
282 =item He is at this time transporting large armies of foreign
283 mercenaries to complete the works of death, desolation and
284 tyranny, already begun with circumstances of cruelty and perfidy
285 scarcely paralleled in the most barbarous ages, and totally
286 unworthy the head of a civilized nation.
287
288=item "=back"
289
290This command indicates that this is the end of the region begun
291by the most recent "=over" command. It permits no text after the
292"=back" command.
293
294=item "=begin formatname"
295
296This marks the following paragraphs (until the matching "=end
297formatname") as being for some special kind of processing. Unless
298"formatname" begins with a colon, the contained non-command
299paragraphs are data paragraphs. But if "formatname" I<does> begin
300with a colon, then non-command paragraphs are ordinary paragraphs
301or data paragraphs. This is discussed in detail in the section
302L</About Data Paragraphs and "=beginE<sol>=end" Regions>.
303
304It is advised that formatnames match the regexp
305C<m/\A:?[-a-zA-Z0-9_]+\z/>. Implementors should anticipate future
306expansion in the semantics and syntax of the first parameter
307to "=begin"/"=end"/"=for".
308
309=item "=end formatname"
310
311This marks the end of the region opened by the matching
312"=begin formatname" region. If "formatname" is not the formatname
313of the most recent open "=begin formatname" region, then this
314is an error, and must generate an error message. This
315is discussed in detail in the section
316L</About Data Paragraphs and "=beginE<sol>=end" Regions>.
317
318=item "=for formatname text..."
319
320This is synonymous with:
321
322 =begin formatname
323
324 text...
325
326 =end formatname
327
328That is, it creates a region consisting of a single paragraph; that
329paragraph is to be treated as a normal paragraph if "formatname"
330begins with a ":"; if "formatname" I<doesn't> begin with a colon,
331then "text..." will constitute a data paragraph. There is no way
332to use "=for formatname text..." to express "text..." as a verbatim
333paragraph.
334
335=item "=encoding encodingname"
336
337This command, which should occur early in the document (at least
338before any non-US-ASCII data!), declares that this document is
339encoded in the encoding I<encodingname>, which must be
340an encoding name that L<Encoding> recognizes. (Encoding's list
341of supported encodings, in L<Encoding::Supported>, is useful here.)
342If the Pod parser cannot decode the declared encoding, it
343should emit a warning and may abort parsing the document
344altogether.
345
346A document having more than one "=encoding" line should be
347considered an error. Pod processors may silently tolerate this if
348the not-first "=encoding" lines are just duplicates of the
349first one (e.g., if there's a "=use utf8" line, and later on
350another "=use utf8" line). But Pod processors should complain if
351there are contradictory "=encoding" lines in the same document
352(e.g., if there is a "=encoding utf8" early in the document and
353"=encoding big5" later). Pod processors that recognize BOMs
354may also complain if they see an "=encoding" line
355that contradicts the BOM (e.g., if a document with a UTF-16LE
356BOM has an "=encoding shiftjis" line).
357
358=back
359
360If a Pod processor sees any command other than the ones listed
361above (like "=head", or "=haed1", or "=stuff", or "=cuttlefish",
362or "=w123"), that processor must by default treat this as an
363error. It must not process the paragraph beginning with that
364command, must by default warn of this as an error, and may
365abort the parse. A Pod parser may allow a way for particular
366applications to add to the above list of known commands, and to
367stipulate, for each additional command, whether formatting
368codes should be processed.
369
370Future versions of this specification may add additional
371commands.
372
373
374
375=head1 Pod Formatting Codes
376
377(Note that in previous drafts of this document and of perlpod,
378formatting codes were referred to as "interior sequences", and
379this term may still be found in the documentation for Pod parsers,
380and in error messages from Pod processors.)
381
382There are two syntaxes for formatting codes:
383
384=over
385
386=item *
387
388A formatting code starts with a capital letter (just US-ASCII [A-Z])
389followed by a "<", any number of characters, and ending with the first
390matching ">". Examples:
391
392 That's what I<you> think!
393
394 What's C<dump()> for?
395
396 X<C<chmod> and C<unlink()> Under Different Operating Systems>
397
398=item *
399
400A formatting code starts with a capital letter (just US-ASCII [A-Z])
401followed by two or more "<"'s, one or more whitespace characters,
402any number of characters, one or more whitespace characters,
403and ending with the first matching sequence of two or more ">"'s, where
404the number of ">"'s equals the number of "<"'s in the opening of this
405formatting code. Examples:
406
407 That's what I<< you >> think!
408
409 C<<< open(X, ">>thing.dat") || die $! >>>
410
411 B<< $foo->bar(); >>
412
413With this syntax, the whitespace character(s) after the "CE<lt><<"
414and before the ">>" (or whatever letter) are I<not> renderable -- they
415do not signify whitespace, are merely part of the formatting codes
416themselves. That is, these are all synonymous:
417
418 C<thing>
419 C<< thing >>
420 C<< thing >>
421 C<<< thing >>>
422 C<<<<
423 thing
424 >>>>
425
426and so on.
427
428=back
429
430In parsing Pod, a notably tricky part is the correct parsing of
431(potentially nested!) formatting codes. Implementors should
432consult the code in the C<parse_text> routine in Pod::Parser as an
433example of a correct implementation.
434
435=over
436
437=item C<IE<lt>textE<gt>> -- italic text
438
439See the brief discussion in L<perlpod/"Formatting Codes">.
440
441=item C<BE<lt>textE<gt>> -- bold text
442
443See the brief discussion in L<perlpod/"Formatting Codes">.
444
445=item C<CE<lt>codeE<gt>> -- code text
446
447See the brief discussion in L<perlpod/"Formatting Codes">.
448
449=item C<FE<lt>filenameE<gt>> -- style for filenames
450
451See the brief discussion in L<perlpod/"Formatting Codes">.
452
453=item C<XE<lt>topic nameE<gt>> -- an index entry
454
455See the brief discussion in L<perlpod/"Formatting Codes">.
456
457This code is unusual in that most formatters completely discard
458this code and its content. Other formatters will render it with
459invisible codes that can be used in building an index of
460the current document.
461
462=item C<ZE<lt>E<gt>> -- a null (zero-effect) formatting code
463
464Discussed briefly in L<perlpod/"Formatting Codes">.
465
466This code is unusual is that it should have no content. That is,
467a processor may complain if it sees C<ZE<lt>potatoesE<gt>>. Whether
468or not it complains, the I<potatoes> text should ignored.
469
470=item C<LE<lt>nameE<gt>> -- a hyperlink
471
472The complicated syntaxes of this code are discussed at length in
473L<perlpod/"Formatting Codes">, and implementation details are
474discussed below, in L</"About LE<lt>...E<gt> Codes">. Parsing the
475contents of LE<lt>content> is tricky. Notably, the content has to be
476checked for whether it looks like a URL, or whether it has to be split
477on literal "|" and/or "/" (in the right order!), and so on,
478I<before> EE<lt>...> codes are resolved.
479
480=item C<EE<lt>escapeE<gt>> -- a character escape
481
482See L<perlpod/"Formatting Codes">, and several points in
483L</Notes on Implementing Pod Processors>.
484
485=item C<SE<lt>textE<gt>> -- text contains non-breaking spaces
486
487This formatting code is syntactically simple, but semantically
488complex. What it means is that each space in the printable
489content of this code signifies a non-breaking space.
490
491Consider:
492
493 C<$x ? $y : $z>
494
495 S<C<$x ? $y : $z>>
496
497Both signify the monospace (c[ode] style) text consisting of
498"$x", one space, "?", one space, ":", one space, "$z". The
499difference is that in the latter, with the S code, those spaces
500are not "normal" spaces, but instead are non-breaking spaces.
501
502=back
503
504
505If a Pod processor sees any formatting code other than the ones
506listed above (as in "NE<lt>...>", or "QE<lt>...>", etc.), that
507processor must by default treat this as an error.
508A Pod parser may allow a way for particular
509applications to add to the above list of known formatting codes;
510a Pod parser might even allow a way to stipulate, for each additional
511command, whether it requires some form of special processing, as
512LE<lt>...> does.
513
514Future versions of this specification may add additional
515formatting codes.
516
517Historical note: A few older Pod processors would not see a ">" as
518closing a "CE<lt>" code, if the ">" was immediately preceded by
519a "-". This was so that this:
520
521 C<$foo->bar>
522
523would parse as equivalent to this:
524
525 C<$foo-E<gt>bar>
526
527instead of as equivalent to a "C" formatting code containing
528only "$foo-", and then a "bar>" outside the "C" formatting code. This
529problem has since been solved by the addition of syntaxes like this:
530
531 C<< $foo->bar >>
532
533Compliant parsers must not treat "->" as special.
534
535Formatting codes absolutely cannot span paragraphs. If a code is
536opened in one paragraph, and no closing code is found by the end of
537that paragraph, the Pod parser must close that formatting code,
538and should complain (as in "Unterminated I code in the paragraph
539starting at line 123: 'Time objects are not...'"). So these
540two paragraphs:
541
542 I<I told you not to do this!
543
544 Don't make me say it again!>
545
546...must I<not> be parsed as two paragraphs in italics (with the I
547code starting in one paragraph and starting in another.) Instead,