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