1Pod::Parser(3pm) Perl Programmers Reference Guide Pod::Parser(3pm)
2
3
4
6 Pod::Parser - base class for creating POD filters and translators
7
9 use Pod::Parser;
10
11 package MyParser;
12 @ISA = qw(Pod::Parser);
13
14 sub command {
15 my ($parser, $command, $paragraph, $line_num) = @_;
16 ## Interpret the command and its text; sample actions might be:
17 if ($command eq 'head1') { ... }
18 elsif ($command eq 'head2') { ... }
19 ## ... other commands and their actions
20 my $out_fh = $parser->output_handle();
21 my $expansion = $parser->interpolate($paragraph, $line_num);
22 print $out_fh $expansion;
23 }
24
25 sub verbatim {
26 my ($parser, $paragraph, $line_num) = @_;
27 ## Format verbatim paragraph; sample actions might be:
28 my $out_fh = $parser->output_handle();
29 print $out_fh $paragraph;
30 }
31
32 sub textblock {
33 my ($parser, $paragraph, $line_num) = @_;
34 ## Translate/Format this block of text; sample actions might be:
35 my $out_fh = $parser->output_handle();
36 my $expansion = $parser->interpolate($paragraph, $line_num);
37 print $out_fh $expansion;
38 }
39
40 sub interior_sequence {
41 my ($parser, $seq_command, $seq_argument) = @_;
42 ## Expand an interior sequence; sample actions might be:
43 return "*$seq_argument*" if ($seq_command eq 'B');
44 return "`$seq_argument'" if ($seq_command eq 'C');
45 return "_${seq_argument}_'" if ($seq_command eq 'I');
46 ## ... other sequence commands and their resulting text
47 }
48
49 package main;
50
51 ## Create a parser object and have it parse file whose name was
52 ## given on the command-line (use STDIN if no files were given).
53 $parser = new MyParser();
54 $parser->parse_from_filehandle(\*STDIN) if (@ARGV == 0);
55 for (@ARGV) { $parser->parse_from_file($_); }
56
58 perl5.005, Pod::InputObjects, Exporter, Symbol, Carp
59
61 Nothing.
62
64 Pod::Parser is a base class for creating POD filters and translators.
65 It handles most of the effort involved with parsing the POD sections
66 from an input stream, leaving subclasses free to be concerned only with
67 performing the actual translation of text.
68
69 Pod::Parser parses PODs, and makes method calls to handle the various
70 components of the POD. Subclasses of Pod::Parser override these methods
71 to translate the POD into whatever output format they desire.
72
74 To create a POD filter for translating POD documentation into some
75 other format, you create a subclass of Pod::Parser which typically
76 overrides just the base class implementation for the following methods:
77
78 · command()
79
80 · verbatim()
81
82 · textblock()
83
84 · interior_sequence()
85
86 You may also want to override the begin_input() and end_input() methods
87 for your subclass (to perform any needed per-file and/or per-document
88 initialization or cleanup).
89
90 If you need to perform any preprocesssing of input before it is parsed
91 you may want to override one or more of preprocess_line() and/or pre‐
92 process_paragraph().
93
94 Sometimes it may be necessary to make more than one pass over the input
95 files. If this is the case you have several options. You can make the
96 first pass using Pod::Parser and override your methods to store the
97 intermediate results in memory somewhere for the end_pod() method to
98 process. You could use Pod::Parser for several passes with an appropri‐
99 ate state variable to control the operation for each pass. If your
100 input source can't be reset to start at the beginning, you can store it
101 in some other structure as a string or an array and have that structure
102 implement a getline() method (which is all that parse_from_filehandle()
103 uses to read input).
104
105 Feel free to add any member data fields you need to keep track of
106 things like current font, indentation, horizontal or vertical position,
107 or whatever else you like. Be sure to read "PRIVATE METHODS AND DATA"
108 to avoid name collisions.
109
110 For the most part, the Pod::Parser base class should be able to do most
111 of the input parsing for you and leave you free to worry about how to
112 intepret the commands and translate the result.
113
114 Note that all we have described here in this quick overview is the sim‐
115 plest most straightforward use of Pod::Parser to do stream-based pars‐
116 ing. It is also possible to use the Pod::Parser::parse_text function to
117 do more sophisticated tree-based parsing. See "TREE-BASED PARSING".
118
120 A parse-option is simply a named option of Pod::Parser with a value
121 that corresponds to a certain specified behavior. These various behav‐
122 iors of Pod::Parser may be enabled/disabled by setting or unsetting one
123 or more parse-options using the parseopts() method. The set of cur‐
124 rently accepted parse-options is as follows:
125
126 -want_nonPODs (default: unset)
127 Normally (by default) Pod::Parser will only provide access to the
128 POD sections of the input. Input paragraphs that are not part of the
129 POD-format documentation are not made available to the caller (not
130 even using preprocess_paragraph()). Setting this option to a
131 non-empty, non-zero value will allow preprocess_paragraph() to see
132 non-POD sections of the input as well as POD sections. The cutting()
133 method can be used to determine if the corresponding paragraph is a
134 POD paragraph, or some other input paragraph.
135
136 -process_cut_cmd (default: unset)
137 Normally (by default) Pod::Parser handles the "=cut" POD directive
138 by itself and does not pass it on to the caller for processing. Set‐
139 ting this option to a non-empty, non-zero value will cause
140 Pod::Parser to pass the "=cut" directive to the caller just like any
141 other POD command (and hence it may be processed by the command()
142 method).
143
144 Pod::Parser will still interpret the "=cut" directive to mean that
145 "cutting mode" has been (re)entered, but the caller will get a
146 chance to capture the actual "=cut" paragraph itself for whatever
147 purpose it desires.
148
149 -warnings (default: unset)
150 Normally (by default) Pod::Parser recognizes a bare minimum of pod
151 syntax errors and warnings and issues diagnostic messages for
152 errors, but not for warnings. (Use Pod::Checker to do more thorough
153 checking of POD syntax.) Setting this option to a non-empty, non-
154 zero value will cause Pod::Parser to issue diagnostics for the few
155 warnings it recognizes as well as the errors.
156
157 Please see "parseopts()" for a complete description of the interface
158 for the setting and unsetting of parse-options.
159
161 Pod::Parser provides several methods which most subclasses will proba‐
162 bly want to override. These methods are as follows:
163
165 $parser->command($cmd,$text,$line_num,$pod_para);
166
167 This method should be overridden by subclasses to take the appropriate
168 action when a POD command paragraph (denoted by a line beginning with
169 "=") is encountered. When such a POD directive is seen in the input,
170 this method is called and is passed:
171
172 $cmd
173 the name of the command for this POD paragraph
174
175 $text
176 the paragraph text for the given POD paragraph command.
177
178 $line_num
179 the line-number of the beginning of the paragraph
180
181 $pod_para
182 a reference to a "Pod::Paragraph" object which contains further
183 information about the paragraph command (see Pod::InputObjects for
184 details).
185
186 Note that this method is called for "=pod" paragraphs.
187
188 The base class implementation of this method simply treats the raw POD
189 command as normal block of paragraph text (invoking the textblock()
190 method with the command paragraph).
191
193 $parser->verbatim($text,$line_num,$pod_para);
194
195 This method may be overridden by subclasses to take the appropriate
196 action when a block of verbatim text is encountered. It is passed the
197 following parameters:
198
199 $text
200 the block of text for the verbatim paragraph
201
202 $line_num
203 the line-number of the beginning of the paragraph
204
205 $pod_para
206 a reference to a "Pod::Paragraph" object which contains further
207 information about the paragraph (see Pod::InputObjects for details).
208
209 The base class implementation of this method simply prints the
210 textblock (unmodified) to the output filehandle.
211
213 $parser->textblock($text,$line_num,$pod_para);
214
215 This method may be overridden by subclasses to take the appropriate
216 action when a normal block of POD text is encountered (although the
217 base class method will usually do what you want). It is passed the fol‐
218 lowing parameters:
219
220 $text
221 the block of text for the a POD paragraph
222
223 $line_num
224 the line-number of the beginning of the paragraph
225
226 $pod_para
227 a reference to a "Pod::Paragraph" object which contains further
228 information about the paragraph (see Pod::InputObjects for details).
229
230 In order to process interior sequences, subclasses implementations of
231 this method will probably want to invoke either interpolate() or
232 parse_text(), passing it the text block $text, and the corresponding
233 line number in $line_num, and then perform any desired processing upon
234 the returned result.
235
236 The base class implementation of this method simply prints the text
237 block as it occurred in the input stream).
238
240 $parser->interior_sequence($seq_cmd,$seq_arg,$pod_seq);
241
242 This method should be overridden by subclasses to take the appropriate
243 action when an interior sequence is encountered. An interior sequence
244 is an embedded command within a block of text which appears as a com‐
245 mand name (usually a single uppercase character) followed immediately
246 by a string of text which is enclosed in angle brackets. This method is
247 passed the sequence command $seq_cmd and the corresponding text
248 $seq_arg. It is invoked by the interpolate() method for each interior
249 sequence that occurs in the string that it is passed. It should return
250 the desired text string to be used in place of the interior sequence.
251 The $pod_seq argument is a reference to a "Pod::InteriorSequence"
252 object which contains further information about the interior sequence.
253 Please see Pod::InputObjects for details if you need to access this
254 additional information.
255
256 Subclass implementations of this method may wish to invoke the nested()
257 method of $pod_seq to see if it is nested inside some other interior-
258 sequence (and if so, which kind).
259
260 The base class implementation of the interior_sequence() method simply
261 returns the raw text of the interior sequence (as it occurred in the
262 input) to the caller.
263
265 Pod::Parser provides several methods which subclasses may want to over‐
266 ride to perform any special pre/post-processing. These methods do not
267 have to be overridden, but it may be useful for subclasses to take
268 advantage of them.
269
271 my $parser = Pod::Parser->new();
272
273 This is the constructor for Pod::Parser and its subclasses. You do not
274 need to override this method! It is capable of constructing subclass
275 objects as well as base class objects, provided you use any of the fol‐
276 lowing constructor invocation styles:
277
278 my $parser1 = MyParser->new();
279 my $parser2 = new MyParser();
280 my $parser3 = $parser2->new();
281
282 where "MyParser" is some subclass of Pod::Parser.
283
284 Using the syntax "MyParser::new()" to invoke the constructor is not
285 recommended, but if you insist on being able to do this, then the sub‐
286 class will need to override the new() constructor method. If you do
287 override the constructor, you must be sure to invoke the initialize()
288 method of the newly blessed object.
289
290 Using any of the above invocations, the first argument to the construc‐
291 tor is always the corresponding package name (or object reference). No
292 other arguments are required, but if desired, an associative array (or
293 hash-table) my be passed to the new() constructor, as in:
294
295 my $parser1 = MyParser->new( MYDATA => $value1, MOREDATA => $value2 );
296 my $parser2 = new MyParser( -myflag => 1 );
297
298 All arguments passed to the new() constructor will be treated as
299 key/value pairs in a hash-table. The newly constructed object will be
300 initialized by copying the contents of the given hash-table (which may
301 have been empty). The new() constructor for this class and all of its
302 subclasses returns a blessed reference to the initialized object
303 (hash-table).
304
306 $parser->initialize();
307
308 This method performs any necessary object initialization. It takes no
309 arguments (other than the object instance of course, which is typically
310 copied to a local variable named $self). If subclasses override this
311 method then they must be sure to invoke "$self->SUPER::initialize()".
312
314 $parser->begin_pod();
315
316 This method is invoked at the beginning of processing for each POD doc‐
317 ument that is encountered in the input. Subclasses should override this
318 method to perform any per-document initialization.
319
321 $parser->begin_input();
322
323 This method is invoked by parse_from_filehandle() immediately before
324 processing input from a filehandle. The base class implementation does
325 nothing, however, subclasses may override it to perform any per-file
326 initializations.
327
328 Note that if multiple files are parsed for a single POD document (per‐
329 haps the result of some future "=include" directive) this method is
330 invoked for every file that is parsed. If you wish to perform certain
331 initializations once per document, then you should use begin_pod().
332
334 $parser->end_input();
335
336 This method is invoked by parse_from_filehandle() immediately after
337 processing input from a filehandle. The base class implementation does
338 nothing, however, subclasses may override it to perform any per-file
339 cleanup actions.
340
341 Please note that if multiple files are parsed for a single POD document
342 (perhaps the result of some kind of "=include" directive) this method
343 is invoked for every file that is parsed. If you wish to perform cer‐
344 tain cleanup actions once per document, then you should use end_pod().
345
347 $parser->end_pod();
348
349 This method is invoked at the end of processing for each POD document
350 that is encountered in the input. Subclasses should override this
351 method to perform any per-document finalization.
352
354 $textline = $parser->preprocess_line($text, $line_num);
355
356 This method should be overridden by subclasses that wish to perform any
357 kind of preprocessing for each line of input (before it has been deter‐
358 mined whether or not it is part of a POD paragraph). The parameter
359 $text is the input line; and the parameter $line_num is the line number
360 of the corresponding text line.
361
362 The value returned should correspond to the new text to use in its
363 place. If the empty string or an undefined value is returned then no
364 further processing will be performed for this line.
365
366 Please note that the preprocess_line() method is invoked before the
367 preprocess_paragraph() method. After all (possibly preprocessed) lines
368 in a paragraph have been assembled together and it has been determined
369 that the paragraph is part of the POD documentation from one of the
370 selected sections, then preprocess_paragraph() is invoked.
371
372 The base class implementation of this method returns the given text.
373
375 $textblock = $parser->preprocess_paragraph($text, $line_num);
376
377 This method should be overridden by subclasses that wish to perform any
378 kind of preprocessing for each block (paragraph) of POD documentation
379 that appears in the input stream. The parameter $text is the POD para‐
380 graph from the input file; and the parameter $line_num is the line num‐
381 ber for the beginning of the corresponding paragraph.
382
383 The value returned should correspond to the new text to use in its
384 place If the empty string is returned or an undefined value is
385 returned, then the given $text is ignored (not processed).
386
387 This method is invoked after gathering up all the lines in a paragraph
388 and after determining the cutting state of the paragraph, but before
389 trying to further parse or interpret them. After preprocess_paragraph()
390 returns, the current cutting state (which is returned by "$self->cut‐
391 ting()") is examined. If it evaluates to true then input text (includ‐
392 ing the given $text) is cut (not processed) until the next POD direc‐
393 tive is encountered.
394
395 Please note that the preprocess_line() method is invoked before the
396 preprocess_paragraph() method. After all (possibly preprocessed) lines
397 in a paragraph have been assembled together and either it has been
398 determined that the paragraph is part of the POD documentation from one
399 of the selected sections or the "-want_nonPODs" option is true, then
400 preprocess_paragraph() is invoked.
401
402 The base class implementation of this method returns the given text.
403
405 Pod::Parser provides several methods to process input text. These meth‐
406 ods typically won't need to be overridden (and in some cases they can't
407 be overridden), but subclasses may want to invoke them to exploit their
408 functionality.
409
411 $ptree1 = $parser->parse_text($text, $line_num);
412 $ptree2 = $parser->parse_text({%opts}, $text, $line_num);
413 $ptree3 = $parser->parse_text(\%opts, $text, $line_num);
414
415 This method is useful if you need to perform your own interpolation of
416 interior sequences and can't rely upon interpolate to expand them in
417 simple bottom-up order.
418
419 The parameter $text is a string or block of text to be parsed for inte‐
420 rior sequences; and the parameter $line_num is the line number curre‐
421 sponding to the beginning of $text.
422
423 parse_text() will parse the given text into a parse-tree of "nodes."
424 and interior-sequences. Each "node" in the parse tree is either a
425 text-string, or a Pod::InteriorSequence. The result returned is a
426 parse-tree of type Pod::ParseTree. Please see Pod::InputObjects for
427 more information about Pod::InteriorSequence and Pod::ParseTree.
428
429 If desired, an optional hash-ref may be specified as the first argument
430 to customize certain aspects of the parse-tree that is created and
431 returned. The set of recognized option keywords are:
432
433 -expand_seq => code-ref⎪method-name
434 Normally, the parse-tree returned by parse_text() will contain an
435 unexpanded "Pod::InteriorSequence" object for each interior-sequence
436 encountered. Specifying -expand_seq tells parse_text() to "expand"
437 every interior-sequence it sees by invoking the referenced function
438 (or named method of the parser object) and using the return value as
439 the expanded result.
440
441 If a subroutine reference was given, it is invoked as:
442
443 &$code_ref( $parser, $sequence )
444
445 and if a method-name was given, it is invoked as:
446
447 $parser->method_name( $sequence )
448
449 where $parser is a reference to the parser object, and $sequence is
450 a reference to the interior-sequence object. [NOTE: If the inte‐
451 rior_sequence() method is specified, then it is invoked according to
452 the interface specified in "interior_sequence()"].
453
454 -expand_text => code-ref⎪method-name
455 Normally, the parse-tree returned by parse_text() will contain a
456 text-string for each contiguous sequence of characters outside of an
457 interior-sequence. Specifying -expand_text tells parse_text() to
458 "preprocess" every such text-string it sees by invoking the refer‐
459 enced function (or named method of the parser object) and using the
460 return value as the preprocessed (or "expanded") result. [Note that
461 if the result is an interior-sequence, then it will not be expanded
462 as specified by the -expand_seq option; Any such recursive expansion
463 needs to be handled by the specified callback routine.]
464
465 If a subroutine reference was given, it is invoked as:
466
467 &$code_ref( $parser, $text, $ptree_node )
468
469 and if a method-name was given, it is invoked as:
470
471 $parser->method_name( $text, $ptree_node )
472
473 where $parser is a reference to the parser object, $text is the
474 text-string encountered, and $ptree_node is a reference to the cur‐
475 rent node in the parse-tree (usually an interior-sequence object or
476 else the top-level node of the parse-tree).
477
478 -expand_ptree => code-ref⎪method-name
479 Rather than returning a "Pod::ParseTree", pass the parse-tree as an
480 argument to the referenced subroutine (or named method of the parser
481 object) and return the result instead of the parse-tree object.
482
483 If a subroutine reference was given, it is invoked as:
484
485 &$code_ref( $parser, $ptree )
486
487 and if a method-name was given, it is invoked as:
488
489 $parser->method_name( $ptree )
490
491 where $parser is a reference to the parser object, and $ptree is a
492 reference to the parse-tree object.
493
495 $textblock = $parser->interpolate($text, $line_num);
496
497 This method translates all text (including any embedded interior
498 sequences) in the given text string $text and returns the interpolated
499 result. The parameter $line_num is the line number corresponding to the
500 beginning of $text.
501
502 interpolate() merely invokes a private method to recursively expand
503 nested interior sequences in bottom-up order (innermost sequences are
504 expanded first). If there is a need to expand nested sequences in some
505 alternate order, use parse_text instead.
506
508 $parser->parse_from_filehandle($in_fh,$out_fh);
509
510 This method takes an input filehandle (which is assumed to already be
511 opened for reading) and reads the entire input stream looking for
512 blocks (paragraphs) of POD documentation to be processed. If no first
513 argument is given the default input filehandle "STDIN" is used.
514
515 The $in_fh parameter may be any object that provides a getline() method
516 to retrieve a single line of input text (hence, an appropriate wrapper
517 object could be used to parse PODs from a single string or an array of
518 strings).
519
520 Using "$in_fh->getline()", input is read line-by-line and assembled
521 into paragraphs or "blocks" (which are separated by lines containing
522 nothing but whitespace). For each block of POD documentation encoun‐
523 tered it will invoke a method to parse the given paragraph.
524
525 If a second argument is given then it should correspond to a filehandle
526 where output should be sent (otherwise the default output filehandle is
527 "STDOUT" if no output filehandle is currently in use).
528
529 NOTE: For performance reasons, this method caches the input stream at
530 the top of the stack in a local variable. Any attempts by clients to
531 change the stack contents during processing when in the midst executing
532 of this method will not affect the input stream used by the current
533 invocation of this method.
534
535 This method does not usually need to be overridden by subclasses.
536
538 $parser->parse_from_file($filename,$outfile);
539
540 This method takes a filename and does the following:
541
542 · opens the input and output files for reading (creating the appropri‐
543 ate filehandles)
544
545 · invokes the parse_from_filehandle() method passing it the correspond‐
546 ing input and output filehandles.
547
548 · closes the input and output files.
549
550 If the special input filename "-" or "<&STDIN" is given then the STDIN
551 filehandle is used for input (and no open or close is performed). If no
552 input filename is specified then "-" is implied.
553
554 If a second argument is given then it should be the name of the desired
555 output file. If the special output filename "-" or ">&STDOUT" is given
556 then the STDOUT filehandle is used for output (and no open or close is
557 performed). If the special output filename ">&STDERR" is given then the
558 STDERR filehandle is used for output (and no open or close is per‐
559 formed). If no output filehandle is currently in use and no output
560 filename is specified, then "-" is implied. Alternatively, an
561 IO::String object is also accepted as an output file handle.
562
563 This method does not usually need to be overridden by subclasses.
564
566 Clients of Pod::Parser should use the following methods to access
567 instance data fields:
568
570 $parser->errorsub("method_name");
571 $parser->errorsub(\&warn_user);
572 $parser->errorsub(sub { print STDERR, @_ });
573
574 Specifies the method or subroutine to use when printing error messages
575 about POD syntax. The supplied method/subroutine must return TRUE upon
576 successful printing of the message. If "undef" is given, then the warn
577 builtin is used to issue error messages (this is the default behavior).
578
579 my $errorsub = $parser->errorsub()
580 my $errmsg = "This is an error message!\n"
581 (ref $errorsub) and &{$errorsub}($errmsg)
582 or (defined $errorsub) and $parser->$errorsub($errmsg)
583 or warn($errmsg);
584
585 Returns a method name, or else a reference to the user-supplied subrou‐
586 tine used to print error messages. Returns "undef" if the warn builtin
587 is used to issue error messages (this is the default behavior).
588
590 $boolean = $parser->cutting();
591
592 Returns the current "cutting" state: a boolean-valued scalar which
593 evaluates to true if text from the input file is currently being "cut"
594 (meaning it is not considered part of the POD document).
595
596 $parser->cutting($boolean);
597
598 Sets the current "cutting" state to the given value and returns the
599 result.
600
602 When invoked with no additional arguments, parseopts returns a
603 hashtable of all the current parsing options.
604
605 ## See if we are parsing non-POD sections as well as POD ones
606 my %opts = $parser->parseopts();
607 $opts{'-want_nonPODs}' and print "-want_nonPODs\n";
608
609 When invoked using a single string, parseopts treats the string as the
610 name of a parse-option and returns its corresponding value if it exists
611 (returns "undef" if it doesn't).
612
613 ## Did we ask to see '=cut' paragraphs?
614 my $want_cut = $parser->parseopts('-process_cut_cmd');
615 $want_cut and print "-process_cut_cmd\n";
616
617 When invoked with multiple arguments, parseopts treats them as
618 key/value pairs and the specified parse-option names are set to the
619 given values. Any unspecified parse-options are unaffected.
620
621 ## Set them back to the default
622 $parser->parseopts(-warnings => 0);
623
624 When passed a single hash-ref, parseopts uses that hash to completely
625 reset the existing parse-options, all previous parse-option values are
626 lost.
627
628 ## Reset all options to default
629 $parser->parseopts( { } );
630
631 See "PARSING OPTIONS" for more information on the name and meaning of
632 each parse-option currently recognized.
633
635 $fname = $parser->output_file();
636
637 Returns the name of the output file being written.
638
640 $fhandle = $parser->output_handle();
641
642 Returns the output filehandle object.
643
645 $fname = $parser->input_file();
646
647 Returns the name of the input file being read.
648
650 $fhandle = $parser->input_handle();
651
652 Returns the current input filehandle object.
653
655 Pod::Parser makes use of several internal methods and data fields which
656 clients should not need to see or use. For the sake of avoiding name
657 collisions for client data and methods, these methods and fields are
658 briefly discussed here. Determined hackers may obtain further informa‐
659 tion about them by reading the Pod::Parser source code.
660
661 Private data fields are stored in the hash-object whose reference is
662 returned by the new() constructor for this class. The names of all pri‐
663 vate methods and data-fields used by Pod::Parser begin with a prefix of
664 "_" and match the regular expression "/^_\w+$/".
665
667 If straightforward stream-based parsing wont meet your needs (as is
668 likely the case for tasks such as translating PODs into structured
669 markup languages like HTML and XML) then you may need to take the tree-
670 based approach. Rather than doing everything in one pass and calling
671 the interpolate() method to expand sequences into text, it may be
672 desirable to instead create a parse-tree using the parse_text() method
673 to return a tree-like structure which may contain an ordered list of
674 children (each of which may be a text-string, or a similar tree-like
675 structure).
676
677 Pay special attention to "METHODS FOR PARSING AND PROCESSING" and to
678 the objects described in Pod::InputObjects. The former describes the
679 gory details and parameters for how to customize and extend the parsing
680 behavior of Pod::Parser. Pod::InputObjects provides several objects
681 that may all be used interchangeably as parse-trees. The most obvious
682 one is the Pod::ParseTree object. It defines the basic interface and
683 functionality that all things trying to be a POD parse-tree should do.
684 A Pod::ParseTree is defined such that each "node" may be a text-string,
685 or a reference to another parse-tree. Each Pod::Paragraph object and
686 each Pod::InteriorSequence object also supports the basic parse-tree
687 interface.
688
689 The parse_text() method takes a given paragraph of text, and returns a
690 parse-tree that contains one or more children, each of which may be a
691 text-string, or an InteriorSequence object. There are also callback-
692 options that may be passed to parse_text() to customize the way it
693 expands or transforms interior-sequences, as well as the returned
694 result. These callbacks can be used to create a parse-tree with custom-
695 made objects (which may or may not support the parse-tree interface,
696 depending on how you choose to do it).
697
698 If you wish to turn an entire POD document into a parse-tree, that
699 process is fairly straightforward. The parse_text() method is the key
700 to doing this successfully. Every paragraph-callback (i.e. the polymor‐
701 phic methods for command(), verbatim(), and textblock() paragraphs)
702 takes a Pod::Paragraph object as an argument. Each paragraph object has
703 a parse_tree() method that can be used to get or set a corresponding
704 parse-tree. So for each of those paragraph-callback methods, simply
705 call parse_text() with the options you desire, and then use the
706 returned parse-tree to assign to the given paragraph object.
707
708 That gives you a parse-tree for each paragraph - so now all you need is
709 an ordered list of paragraphs. You can maintain that yourself as a data
710 element in the object/hash. The most straightforward way would be sim‐
711 ply to use an array-ref, with the desired set of custom "options" for
712 each invocation of parse_text. Let's assume the desired option-set is
713 given by the hash %options. Then we might do something like the follow‐
714 ing:
715
716 package MyPodParserTree;
717
718 @ISA = qw( Pod::Parser );
719
720 ...
721
722 sub begin_pod {
723 my $self = shift;
724 $self->{'-paragraphs'} = []; ## initialize paragraph list
725 }
726
727 sub command {
728 my ($parser, $command, $paragraph, $line_num, $pod_para) = @_;
729 my $ptree = $parser->parse_text({%options}, $paragraph, ...);
730 $pod_para->parse_tree( $ptree );
731 push @{ $self->{'-paragraphs'} }, $pod_para;
732 }
733
734 sub verbatim {
735 my ($parser, $paragraph, $line_num, $pod_para) = @_;
736 push @{ $self->{'-paragraphs'} }, $pod_para;
737 }
738
739 sub textblock {
740 my ($parser, $paragraph, $line_num, $pod_para) = @_;
741 my $ptree = $parser->parse_text({%options}, $paragraph, ...);
742 $pod_para->parse_tree( $ptree );
743 push @{ $self->{'-paragraphs'} }, $pod_para;
744 }
745
746 ...
747
748 package main;
749 ...
750 my $parser = new MyPodParserTree(...);
751 $parser->parse_from_file(...);
752 my $paragraphs_ref = $parser->{'-paragraphs'};
753
754 Of course, in this module-author's humble opinion, I'd be more inclined
755 to use the existing Pod::ParseTree object than a simple array. That way
756 everything in it, paragraphs and sequences, all respond to the same
757 core interface for all parse-tree nodes. The result would look some‐
758 thing like:
759
760 package MyPodParserTree2;
761
762 ...
763
764 sub begin_pod {
765 my $self = shift;
766 $self->{'-ptree'} = new Pod::ParseTree; ## initialize parse-tree
767 }
768
769 sub parse_tree {
770 ## convenience method to get/set the parse-tree for the entire POD
771 (@_ > 1) and $_[0]->{'-ptree'} = $_[1];
772 return $_[0]->{'-ptree'};
773 }
774
775 sub command {
776 my ($parser, $command, $paragraph, $line_num, $pod_para) = @_;
777 my $ptree = $parser->parse_text({<<options>>}, $paragraph, ...);
778 $pod_para->parse_tree( $ptree );
779 $parser->parse_tree()->append( $pod_para );
780 }
781
782 sub verbatim {
783 my ($parser, $paragraph, $line_num, $pod_para) = @_;
784 $parser->parse_tree()->append( $pod_para );
785 }
786
787 sub textblock {
788 my ($parser, $paragraph, $line_num, $pod_para) = @_;
789 my $ptree = $parser->parse_text({<<options>>}, $paragraph, ...);
790 $pod_para->parse_tree( $ptree );
791 $parser->parse_tree()->append( $pod_para );
792 }
793
794 ...
795
796 package main;
797 ...
798 my $parser = new MyPodParserTree2(...);
799 $parser->parse_from_file(...);
800 my $ptree = $parser->parse_tree;
801 ...
802
803 Now you have the entire POD document as one great big parse-tree. You
804 can even use the -expand_seq option to parse_text to insert whole dif‐
805 ferent kinds of objects. Just don't expect Pod::Parser to know what to
806 do with them after that. That will need to be in your code. Or, alter‐
807 natively, you can insert any object you like so long as it conforms to
808 the Pod::ParseTree interface.
809
810 One could use this to create subclasses of Pod::Paragraphs and
811 Pod::InteriorSequences for specific commands (or to create your own
812 custom node-types in the parse-tree) and add some kind of emit() method
813 to each custom node/subclass object in the tree. Then all you'd need to
814 do is recursively walk the tree in the desired order, processing the
815 children (most likely from left to right) by formatting them if they
816 are text-strings, or by calling their emit() method if they are
817 objects/references.
818
820 Pod::InputObjects, Pod::Select
821
822 Pod::InputObjects defines POD input objects corresponding to command
823 paragraphs, parse-trees, and interior-sequences.
824
825 Pod::Select is a subclass of Pod::Parser which provides the ability to
826 selectively include and/or exclude sections of a POD document from
827 being translated based upon the current heading, subheading, subsub‐
828 heading, etc.
829
831 Please report bugs using <http://rt.cpan.org>.
832
833 Brad Appleton <bradapp@enteract.com>
834
835 Based on code for Pod::Text written by Tom Christiansen
836 <tchrist@mox.perl.com>
837
838
839
840perl v5.8.8 2001-09-21 Pod::Parser(3pm)