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