Manual Pages for UNIX Darwin command on man Pod::Parser
MyWebUniversity

Manual Pages for UNIX Darwin command on man Pod::Parser

Pod::Parser(3pm) Perl Programmers Reference Guide Pod::Parser(3pm)

NAME

Pod::Parser - base class for creating POD filters and translators

SYNOPSIS

use Pod::Parser;

package MyParser;

@ISA = qw(Pod::Parser);

sub command {

my ($parser, $command, $paragraph, $linenum) = @;

## Interpret the command and its text; sample actions might be:

if ($command eq 'head1') { ... }

elsif ($command eq 'head2') { ... }

## ... other commands and their actions

my $outfh = $parser->outputhandle();

my $expansion = $parser->interpolate($paragraph, $linenum);

print $outfh $expansion;

} sub verbatim {

my ($parser, $paragraph, $linenum) = @;

## Format verbatim paragraph; sample actions might be:

my $outfh = $parser->outputhandle();

print $outfh $paragraph;

} sub textblock {

my ($parser, $paragraph, $linenum) = @;

## Translate/Format this block of text; sample actions might be:

my $outfh = $parser->outputhandle();

my $expansion = $parser->interpolate($paragraph, $linenum);

print $outfh $expansion;

} sub interiorsequence {

my ($parser, $seqcommand, $seqargument) = @;

## Expand an interior sequence; sample actions might be:

return "*$seqargument*" if ($seqcommand eq 'B');

return "`$seqargument'" if ($seqcommand eq 'C');

return "${seqargument}'" if ($seqcommand eq 'I');

## ... other sequence commands and their resulting text

} package main;

## Create a parser object and have it parse file whose name was

## given on the command-line (use STDIN if no files were given).

$parser = new MyParser();

$parser->parsefromfilehandle(\*STDIN) if (@ARGV == 0);

for (@ARGV) { $parser->parsefromfile($); }

RREEQQUUIIRREESS perl5.005, Pod::InputObjects, Exporter, Symbol, Carp EEXXPPOORRTTSS Nothing.

DESCRIPTION

PPoodd::::PPaarrsseerr is a base class for creating POD filters and translators. It handles most of the effort involved with parsing the POD sections from an input stream, leaving subclasses free to be concerned only with performing the actual translation of text. PPoodd::::PPaarrsseerr parses PODs, and makes method calls to handle the various components of the POD. Subclasses of PPoodd::::PPaarrsseerr override these methods to translate the POD into whatever output format they desire. QQUUIICCKK OOVVEERRVVIIEEWW To create a POD filter for translating POD documentation into some other format, you create a subclass of PPoodd::::PPaarrsseerr which typically overrides just the base class implementation for the following methods: +o ccoommmmaanndd(()) +o vveerrbbaattiimm(()) +o tteexxttbblloocckk(()) +o iinntteerriioorrsseeqquueennccee(()) You may also want to override the bbeeggiinniinnppuutt(()) and eennddiinnppuutt(()) methods

for your subclass (to perform any needed per-file and/or per-document

initialization or cleanup). If you need to perform any preprocesssing of input before it is parsed

you may want to override one or more of pprreepprroocceesssslliinnee(()) and/or pprree-

pprroocceessssppaarraaggrraapphh(()). Sometimes it may be necessary to make more than one pass over the input files. If this is the case you have several options. You can make the first pass using PPoodd::::PPaarrsseerr and override your methods to store the intermediate results in memory somewhere for the eennddppoodd(()) method to

process. You could use PPoodd::::PPaarrsseerr for several passes with an appropri-

ate state variable to control the operation for each pass. If your input source can't be reset to start at the beginning, you can store it in some other structure as a string or an array and have that structure implement a ggeettlliinnee(()) method (which is all that ppaarrsseeffrroommffiilleehhaannddllee(()) uses to read input). Feel free to add any member data fields you need to keep track of things like current font, indentation, horizontal or vertical position, or whatever else you like. Be sure to read "PRIVATE METHODS AND DATA" to avoid name collisions. For the most part, the PPoodd::::PPaarrsseerr base class should be able to do most of the input parsing for you and leave you free to worry about how to intepret the commands and translate the result.

Note that all we have described here in this quick overview is the sim-

plest most straightforward use of PPoodd::::PPaarrsseerr to do stream-based pars-

ing. It is also possible to use the PPoodd::::PPaarrsseerr::::ppaarrsseetteexxtt function to

do more sophisticated tree-based parsing. See "TREE-BASED PARSING".

PPAARRSSIINNGG OOPPTTIIOONNSS

A parse-option is simply a named option of PPoodd::::PPaarrsseerr with a value

that corresponds to a certain specified behavior. These various behav-

iors of PPoodd::::PPaarrsseerr may be enabled/disabled by setting or unsetting one

or more parse-options using the ppaarrsseeooppttss(()) method. The set of cur-

rently accepted parse-options is as follows:

-wwaannttnnoonnPPOODDss (default: unset)

Normally (by default) PPoodd::::PPaarrsseerr will only provide access to the POD sections of the input. Input paragraphs that are not part of the

POD-format documentation are not made available to the caller (not

even using pprreepprroocceessssppaarraaggrraapphh(())). Setting this option to a

non-empty, non-zero value will allow pprreepprroocceessssppaarraaggrraapphh(()) to see

non-POD sections of the input as well as POD sections. The ccuuttttiinngg(())

method can be used to determine if the corresponding paragraph is a POD paragraph, or some other input paragraph.

-pprroocceessssccuuttccmmdd (default: unset)

Normally (by default) PPoodd::::PPaarrsseerr handles the "=cut" POD directive

by itself and does not pass it on to the caller for processing. Set-

ting this option to a non-empty, non-zero value will cause

PPoodd::::PPaarrsseerr to pass the "=cut" directive to the caller just like any other POD command (and hence it may be processed by the ccoommmmaanndd(()) method). PPoodd::::PPaarrsseerr will still interpret the "=cut" directive to mean that "cutting mode" has been (re)entered, but the caller will get a chance to capture the actual "=cut" paragraph itself for whatever purpose it desires.

-wwaarrnniinnggss (default: unset)

Normally (by default) PPoodd::::PPaarrsseerr recognizes a bare minimum of pod syntax errors and warnings and issues diagnostic messages for errors, but not for warnings. (Use PPoodd::::CChheecckkeerr to do more thorough

checking of POD syntax.) Setting this option to a non-empty, non-

zero value will cause PPoodd::::PPaarrsseerr to issue diagnostics for the few warnings it recognizes as well as the errors. Please see "parseopts()" for a complete description of the interface

for the setting and unsetting of parse-options.

RREECCOOMMMMEENNDDEEDD SSUUBBRROOUUTTIINNEE//MMEETTHHOODD OOVVEERRRRIIDDEESS

PPoodd::::PPaarrsseerr provides several methods which most subclasses will proba-

bly want to override. These methods are as follows: ccoommmmaanndd(())

$parser->command($cmd,$text,$linenum,$podpara);

This method should be overridden by subclasses to take the appropriate action when a POD command paragraph (denoted by a line beginning with "=") is encountered. When such a POD directive is seen in the input, this method is called and is passed:

$cmd

the name of the command for this POD paragraph

$text

the paragraph text for the given POD paragraph command.

$linenum

the line-number of the beginning of the paragraph

$podpara

a reference to a "Pod::Paragraph" object which contains further information about the paragraph command (see Pod::InputObjects for details). NNoottee that this method is called for "=pod" paragraphs. The base class implementation of this method simply treats the raw POD command as normal block of paragraph text (invoking the tteexxttbblloocckk(()) method with the command paragraph). vveerrbbaattiimm(())

$parser->verbatim($text,$linenum,$podpara);

This method may be overridden by subclasses to take the appropriate action when a block of verbatim text is encountered. It is passed the following parameters:

$text

the block of text for the verbatim paragraph

$linenum

the line-number of the beginning of the paragraph

$podpara

a reference to a "Pod::Paragraph" object which contains further information about the paragraph (see Pod::InputObjects for details). The base class implementation of this method simply prints the textblock (unmodified) to the output filehandle. tteexxttbblloocckk(())

$parser->textblock($text,$linenum,$podpara);

This method may be overridden by subclasses to take the appropriate action when a normal block of POD text is encountered (although the

base class method will usually do what you want). It is passed the fol-

lowing parameters:

$text

the block of text for the a POD paragraph

$linenum

the line-number of the beginning of the paragraph

$podpara

a reference to a "Pod::Paragraph" object which contains further information about the paragraph (see Pod::InputObjects for details). In order to process interior sequences, subclasses implementations of this method will probably want to invoke either iinntteerrppoollaattee(()) or

ppaarrsseetteexxtt(()), passing it the text block $text, and the corresponding

line number in $linenum, and then perform any desired processing upon

the returned result. The base class implementation of this method simply prints the text block as it occurred in the input stream). iinntteerriioorrsseeqquueennccee(())

$parser->interiorsequence($seqcmd,$seqarg,$podseq);

This method should be overridden by subclasses to take the appropriate action when an interior sequence is encountered. An interior sequence

is an embedded command within a block of text which appears as a com-

mand name (usually a single uppercase character) followed immediately by a string of text which is enclosed in angle brackets. This method is

passed the sequence command $seqcmd and the corresponding text

$seqarg. It is invoked by the iinntteerrppoollaattee(()) method for each interior

sequence that occurs in the string that it is passed. It should return the desired text string to be used in place of the interior sequence.

The $podseq argument is a reference to a "Pod::InteriorSequence"

object which contains further information about the interior sequence. Please see Pod::InputObjects for details if you need to access this additional information. Subclass implementations of this method may wish to invoke the nneesstteedd(())

method of $podseq to see if it is nested inside some other interior-

sequence (and if so, which kind). The base class implementation of the iinntteerriioorrsseeqquueennccee(()) method simply returns the raw text of the interior sequence (as it occurred in the input) to the caller. OOPPTTIIOONNAALL SSUUBBRROOUUTTIINNEE//MMEETTHHOODD OOVVEERRRRIIDDEESS

PPoodd::::PPaarrsseerr provides several methods which subclasses may want to over-

ride to perform any special pre/post-processing. These methods do not

have to be overridden, but it may be useful for subclasses to take advantage of them. nneeww(())

my $parser = Pod::Parser->new();

This is the constructor for PPoodd::::PPaarrsseerr and its subclasses. You do not need to override this method! It is capable of constructing subclass

objects as well as base class objects, provided you use any of the fol-

lowing constructor invocation styles:

my $parser1 = MyParser->new();

my $parser2 = new MyParser();

my $parser3 = $parser2->new();

where "MyParser" is some subclass of PPoodd::::PPaarrsseerr. Using the syntax "MyParser::new()" to invoke the constructor is not

recommended, but if you insist on being able to do this, then the sub-

class will need to override the nneeww(()) constructor method. If you do override the constructor, you must be sure to invoke the iinniittiiaalliizzee(()) method of the newly blessed object.

Using any of the above invocations, the first argument to the construc-

tor is always the corresponding package name (or object reference). No other arguments are required, but if desired, an associative array (or

hash-table) my be passed to the nneeww(()) constructor, as in:

my $parser1 = MyParser->new( MYDATA => $value1, MOREDATA => $value2 );

my $parser2 = new MyParser( -myflag => 1 );

All arguments passed to the nneeww(()) constructor will be treated as

key/value pairs in a hash-table. The newly constructed object will be

initialized by copying the contents of the given hash-table (which may

have been empty). The nneeww(()) constructor for this class and all of its subclasses returns a blessed reference to the initialized object

(hash-table).

iinniittiiaalliizzee(())

$parser->initialize();

This method performs any necessary object initialization. It takes no arguments (other than the object instance of course, which is typically

copied to a local variable named $self). If subclasses override this

method then they must be sure to invoke "$self->SUPER::initialize()".

bbeeggiinnppoodd(())

$parser->beginpod();

This method is invoked at the beginning of processing for each POD doc-

ument that is encountered in the input. Subclasses should override this

method to perform any per-document initialization.

bbeeggiinniinnppuutt(())

$parser->begininput();

This method is invoked by ppaarrsseeffrroommffiilleehhaannddllee(()) immediately before processing input from a filehandle. The base class implementation does

nothing, however, subclasses may override it to perform any per-file

initializations.

Note that if multiple files are parsed for a single POD document (per-

haps the result of some future "=include" directive) this method is invoked for every file that is parsed. If you wish to perform certain initializations once per document, then you should use bbeeggiinnppoodd(()). eennddiinnppuutt(())

$parser->endinput();

This method is invoked by ppaarrsseeffrroommffiilleehhaannddllee(()) immediately after processing input from a filehandle. The base class implementation does

nothing, however, subclasses may override it to perform any per-file

cleanup actions. Please note that if multiple files are parsed for a single POD document (perhaps the result of some kind of "=include" directive) this method

is invoked for every file that is parsed. If you wish to perform cer-

tain cleanup actions once per document, then you should use eennddppoodd(()). eennddppoodd(())

$parser->endpod();

This method is invoked at the end of processing for each POD document that is encountered in the input. Subclasses should override this

method to perform any per-document finalization.

pprreepprroocceesssslliinnee(())

$textline = $parser->preprocessline($text, $linenum);

This method should be overridden by subclasses that wish to perform any

kind of preprocessing for each line of input (before it has been deter-

mined whether or not it is part of a POD paragraph). The parameter

$text is the input line; and the parameter $linenum is the line number

of the corresponding text line. The value returned should correspond to the new text to use in its place. If the empty string or an undefined value is returned then no further processing will be performed for this line. Please note that the pprreepprroocceesssslliinnee(()) method is invoked before the pprreepprroocceessssppaarraaggrraapphh(()) method. After all (possibly preprocessed) lines in a paragraph have been assembled together and it has been determined that the paragraph is part of the POD documentation from one of the selected sections, then pprreepprroocceessssppaarraaggrraapphh(()) is invoked. The base class implementation of this method returns the given text. pprreepprroocceessssppaarraaggrraapphh(())

$textblock = $parser->preprocessparagraph($text, $linenum);

This method should be overridden by subclasses that wish to perform any kind of preprocessing for each block (paragraph) of POD documentation

that appears in the input stream. The parameter $text is the POD para-

graph from the input file; and the parameter $linenum is the line num-

ber for the beginning of the corresponding paragraph. The value returned should correspond to the new text to use in its place If the empty string is returned or an undefined value is

returned, then the given $text is ignored (not processed).

This method is invoked after gathering up all the lines in a paragraph and after determining the cutting state of the paragraph, but before trying to further parse or interpret them. After pprreepprroocceessssppaarraaggrraapphh(())

returns, the current cutting state (which is returned by "$self->cut-

ting()") is examined. If it evaluates to true then input text (includ-

ing the given $text) is cut (not processed) until the next POD direc-

tive is encountered. Please note that the pprreepprroocceesssslliinnee(()) method is invoked before the pprreepprroocceessssppaarraaggrraapphh(()) method. After all (possibly preprocessed) lines in a paragraph have been assembled together and either it has been determined that the paragraph is part of the POD documentation from one

of the selected sections or the "-wantnonPODs" option is true, then

pprreepprroocceessssppaarraaggrraapphh(()) is invoked. The base class implementation of this method returns the given text. MMEETTHHOODDSS FFOORR PPAARRSSIINNGG AANNDD PPRROOCCEESSSSIINNGG

PPoodd::::PPaarrsseerr provides several methods to process input text. These meth-

ods typically won't need to be overridden (and in some cases they can't be overridden), but subclasses may want to invoke them to exploit their functionality. ppaarrsseetteexxtt(())

$ptree1 = $parser->parsetext($text, $linenum);

$ptree2 = $parser->parsetext({%opts}, $text, $linenum);

$ptree3 = $parser->parsetext(\%opts, $text, $linenum);

This method is useful if you need to perform your own interpolation of interior sequences and can't rely upon iinntteerrppoollaattee to expand them in

simple bottom-up order.

The parameter $text is a string or block of text to be parsed for inte-

rior sequences; and the parameter $linenum is the line number curre-

sponding to the beginning of $text.

ppaarrsseetteexxtt(()) will parse the given text into a parse-tree of "nodes."

and interior-sequences. Each "node" in the parse tree is either a

text-string, or a PPoodd::::IInntteerriioorrSSeeqquueennccee. The result returned is a

parse-tree of type PPoodd::::PPaarrsseeTTrreeee. Please see Pod::InputObjects for

more information about PPoodd::::IInntteerriioorrSSeeqquueennccee and PPoodd::::PPaarrsseeTTrreeee.

If desired, an optional hash-ref may be specified as the first argument

to customize certain aspects of the parse-tree that is created and

returned. The set of recognized option keywords are:

-eexxppaannddsseeqq => code-ref|method-name

Normally, the parse-tree returned by ppaarrsseetteexxtt(()) will contain an

unexpanded "Pod::InteriorSequence" object for each interior-sequence

encountered. Specifying -eexxppaannddsseeqq tells ppaarrsseetteexxtt(()) to "expand"

every interior-sequence it sees by invoking the referenced function

(or named method of the parser object) and using the return value as the expanded result. If a subroutine reference was given, it is invoked as:

&$coderef( $parser, $sequence )

and if a method-name was given, it is invoked as:

$parser->methodname( $sequence )

where $parser is a reference to the parser object, and $sequence is

a reference to the interior-sequence object. [NOTE: If the iinnttee-

rriioorrsseeqquueennccee(()) method is specified, then it is invoked according to the interface specified in "interiorsequence()"].

-eexxppaannddtteexxtt => code-ref|method-name

Normally, the parse-tree returned by ppaarrsseetteexxtt(()) will contain a

text-string for each contiguous sequence of characters outside of an

interior-sequence. Specifying -eexxppaannddtteexxtt tells ppaarrsseetteexxtt(()) to

"preprocess" every such text-string it sees by invoking the refer-

enced function (or named method of the parser object) and using the return value as the preprocessed (or "expanded") result. [Note that

if the result is an interior-sequence, then it will not be expanded

as specified by the -eexxppaannddsseeqq option; Any such recursive expansion

needs to be handled by the specified callback routine.] If a subroutine reference was given, it is invoked as:

&$coderef( $parser, $text, $ptreenode )

and if a method-name was given, it is invoked as:

$parser->methodname( $text, $ptreenode )

where $parser is a reference to the parser object, $text is the

text-string encountered, and $ptreenode is a reference to the cur-

rent node in the parse-tree (usually an interior-sequence object or

else the top-level node of the parse-tree).

-eexxppaannddppttrreeee => code-ref|method-name

Rather than returning a "Pod::ParseTree", pass the parse-tree as an

argument to the referenced subroutine (or named method of the parser

object) and return the result instead of the parse-tree object.

If a subroutine reference was given, it is invoked as:

&$coderef( $parser, $ptree )

and if a method-name was given, it is invoked as:

$parser->methodname( $ptree )

where $parser is a reference to the parser object, and $ptree is a

reference to the parse-tree object.

iinntteerrppoollaattee(())

$textblock = $parser->interpolate($text, $linenum);

This method translates all text (including any embedded interior

sequences) in the given text string $text and returns the interpolated

result. The parameter $linenum is the line number corresponding to the

beginning of $text.

iinntteerrppoollaattee(()) merely invokes a private method to recursively expand

nested interior sequences in bottom-up order (innermost sequences are

expanded first). If there is a need to expand nested sequences in some alternate order, use ppaarrsseetteexxtt instead. ppaarrsseeffrroommffiilleehhaannddllee(())

$parser->parsefromfilehandle($infh,$outfh);

This method takes an input filehandle (which is assumed to already be opened for reading) and reads the entire input stream looking for blocks (paragraphs) of POD documentation to be processed. If no first argument is given the default input filehandle "STDIN" is used.

The $infh parameter may be any object that provides a ggeettlliinnee(()) method

to retrieve a single line of input text (hence, an appropriate wrapper object could be used to parse PODs from a single string or an array of strings).

Using "$infh->getline()", input is read line-by-line and assembled

into paragraphs or "blocks" (which are separated by lines containing

nothing but whitespace). For each block of POD documentation encoun-

tered it will invoke a method to parse the given paragraph. If a second argument is given then it should correspond to a filehandle where output should be sent (otherwise the default output filehandle is "STDOUT" if no output filehandle is currently in use). NNOOTTEE:: For performance reasons, this method caches the input stream at the top of the stack in a local variable. Any attempts by clients to change the stack contents during processing when in the midst executing of this method will not affect the input stream used by the current invocation of this method. This method does not usually need to be overridden by subclasses. ppaarrsseeffrroommffiillee(())

$parser->parsefromfile($filename,$outfile);

This method takes a filename and does the following:

+o opens the input and output files for reading (creating the appropri-

ate filehandles)

+o invokes the ppaarrsseeffrroommffiilleehhaannddllee(()) method passing it the correspond-

ing input and output filehandles. +o closes the input and output files.

If the special input filename "-" or "<&STDIN" is given then the STDIN

filehandle is used for input (and no open or close is performed). If no

input filename is specified then "-" is implied.

If a second argument is given then it should be the name of the desired

output file. If the special output filename "-" or ">&STDOUT" is given

then the STDOUT filehandle is used for output (and no open or close is performed). If the special output filename ">&STDERR" is given then the

STDERR filehandle is used for output (and no open or close is per-

formed). If no output filehandle is currently in use and no output

filename is specified, then "-" is implied. Alternatively, an

IO::String object is also accepted as an output file handle. This method does not usually need to be overridden by subclasses. AACCCCEESSSSOORR MMEETTHHOODDSS Clients of PPoodd::::PPaarrsseerr should use the following methods to access instance data fields: eerrrroorrssuubb(())

$parser->errorsub("methodname");

$parser->errorsub(\&warnuser);

$parser->errorsub(sub { print STDERR, @ });

Specifies the method or subroutine to use when printing error messages about POD syntax. The supplied method/subroutine must return TRUE upon successful printing of the message. If "undef" is given, then the wwaarrnn builtin is used to issue error messages (this is the default behavior).

my $errorsub = $parser->errorsub()

my $errmsg = "This is an error message!\n"

(ref $errorsub) and &{$errorsub}($errmsg)

or (defined $errorsub) and $parser->$errorsub($errmsg)

or warn($errmsg);

Returns a method name, or else a reference to the user-supplied subrou-

tine used to print error messages. Returns "undef" if the wwaarrnn builtin is used to issue error messages (this is the default behavior). ccuuttttiinngg(())

$boolean = $parser->cutting();

Returns the current "cutting" state: a boolean-valued scalar which

evaluates to true if text from the input file is currently being "cut" (meaning it is not considered part of the POD document).

$parser->cutting($boolean);

Sets the current "cutting" state to the given value and returns the result. ppaarrsseeooppttss(()) When invoked with no additional arguments, ppaarrsseeooppttss returns a hashtable of all the current parsing options.

## See if we are parsing non-POD sections as well as POD ones

my %opts = $parser->parseopts();

$opts{'-wantnonPODs}' and print "-wantnonPODs\n";

When invoked using a single string, ppaarrsseeooppttss treats the string as the

name of a parse-option and returns its corresponding value if it exists

(returns "undef" if it doesn't).

## Did we ask to see '=cut' paragraphs?

my $wantcut = $parser->parseopts('-processcutcmd');

$wantcut and print "-processcutcmd\n";

When invoked with multiple arguments, ppaarrsseeooppttss treats them as

key/value pairs and the specified parse-option names are set to the

given values. Any unspecified parse-options are unaffected.

## Set them back to the default

$parser->parseopts(-warnings => 0);

When passed a single hash-ref, ppaarrsseeooppttss uses that hash to completely

reset the existing parse-options, all previous parse-option values are

lost.

## Reset all options to default

$parser->parseopts( { } );

See "PARSING OPTIONS" for more information on the name and meaning of

each parse-option currently recognized.

oouuttppuuttffiillee(())

$fname = $parser->outputfile();

Returns the name of the output file being written. oouuttppuutthhaannddllee(())

$fhandle = $parser->outputhandle();

Returns the output filehandle object. iinnppuuttffiillee(())

$fname = $parser->inputfile();

Returns the name of the input file being read. iinnppuutthhaannddllee(())

$fhandle = $parser->inputhandle();

Returns the current input filehandle object. PPRRIIVVAATTEE MMEETTHHOODDSS AANNDD DDAATTAA PPoodd::::PPaarrsseerr makes use of several internal methods and data fields which clients should not need to see or use. For the sake of avoiding name collisions for client data and methods, these methods and fields are

briefly discussed here. Determined hackers may obtain further informa-

tion about them by reading the PPoodd::::PPaarrsseerr source code.

Private data fields are stored in the hash-object whose reference is

returned by the nneeww(()) constructor for this class. The names of all pri-

vate methods and data-fields used by PPoodd::::PPaarrsseerr begin with a prefix of

"" and match the regular expression "/^\w+$/".

TTRREEEE-BBAASSEEDD PPAARRSSIINNGG

If straightforward stream-based parsing wont meet your needs (as is

likely the case for tasks such as translating PODs into structured

markup languages like HTML and XML) then you may need to take the tree-

based approach. Rather than doing everything in one pass and calling the iinntteerrppoollaattee(()) method to expand sequences into text, it may be

desirable to instead create a parse-tree using the ppaarrsseetteexxtt(()) method

to return a tree-like structure which may contain an ordered list of

children (each of which may be a text-string, or a similar tree-like

structure). Pay special attention to "METHODS FOR PARSING AND PROCESSING" and to the objects described in Pod::InputObjects. The former describes the gory details and parameters for how to customize and extend the parsing behavior of PPoodd::::PPaarrsseerr. PPoodd::::IInnppuuttOObbjjeeccttss provides several objects

that may all be used interchangeably as parse-trees. The most obvious

one is the PPoodd::::PPaarrsseeTTrreeee object. It defines the basic interface and

functionality that all things trying to be a POD parse-tree should do.

A PPoodd::::PPaarrsseeTTrreeee is defined such that each "node" may be a text-string,

or a reference to another parse-tree. Each PPoodd::::PPaarraaggrraapphh object and

each PPoodd::::IInntteerriioorrSSeeqquueennccee object also supports the basic parse-tree

interface. The ppaarrsseetteexxtt(()) method takes a given paragraph of text, and returns a

parse-tree that contains one or more children, each of which may be a

text-string, or an InteriorSequence object. There are also callback-

options that may be passed to ppaarrsseetteexxtt(()) to customize the way it

expands or transforms interior-sequences, as well as the returned

result. These callbacks can be used to create a parse-tree with custom-

made objects (which may or may not support the parse-tree interface,

depending on how you choose to do it).

If you wish to turn an entire POD document into a parse-tree, that

process is fairly straightforward. The ppaarrsseetteexxtt(()) method is the key

to doing this successfully. Every paragraph-callback (i.e. the polymor-

phic methods for ccoommmmaanndd(()), vveerrbbaattiimm(()), and tteexxttbblloocckk(()) paragraphs) takes a PPoodd::::PPaarraaggrraapphh object as an argument. Each paragraph object has a ppaarrsseettrreeee(()) method that can be used to get or set a corresponding

parse-tree. So for each of those paragraph-callback methods, simply

call ppaarrsseetteexxtt(()) with the options you desire, and then use the

returned parse-tree to assign to the given paragraph object.

That gives you a parse-tree for each paragraph - so now all you need is

an ordered list of paragraphs. You can maintain that yourself as a data

element in the object/hash. The most straightforward way would be sim-

ply to use an array-ref, with the desired set of custom "options" for

each invocation of ppaarrsseetteexxtt. Let's assume the desired option-set is

given by the hash %options. Then we might do something like the follow-

ing: package MyPodParserTree;

@ISA = qw( Pod::Parser );

... sub beginpod {

my $self = shift;

$self->{'-paragraphs'} = []; ## initialize paragraph list

} sub command {

my ($parser, $command, $paragraph, $linenum, $podpara) = @;

my $ptree = $parser->parsetext({%options}, $paragraph, ...);

$podpara->parsetree( $ptree );

push @{ $self->{'-paragraphs'} }, $podpara;

} sub verbatim {

my ($parser, $paragraph, $linenum, $podpara) = @;

push @{ $self->{'-paragraphs'} }, $podpara;

} sub textblock {

my ($parser, $paragraph, $linenum, $podpara) = @;

my $ptree = $parser->parsetext({%options}, $paragraph, ...);

$podpara->parsetree( $ptree );

push @{ $self->{'-paragraphs'} }, $podpara;

} ... package main; ...

my $parser = new MyPodParserTree(...);

$parser->parsefromfile(...);

my $paragraphsref = $parser->{'-paragraphs'};

Of course, in this module-author's humble opinion, I'd be more inclined

to use the existing PPoodd::::PPaarrsseeTTrreeee object than a simple array. That way everything in it, paragraphs and sequences, all respond to the same

core interface for all parse-tree nodes. The result would look some-

thing like: package MyPodParserTree2; ... sub beginpod {

my $self = shift;

$self->{'-ptree'} = new Pod::ParseTree; ## initialize parse-tree

} sub parsetree {

## convenience method to get/set the parse-tree for the entire POD

(@ > 1) and $[0]->{'-ptree'} = $[1];

return $[0]->{'-ptree'};

} sub command {

my ($parser, $command, $paragraph, $linenum, $podpara) = @;

my $ptree = $parser->parsetext({<>}, $paragraph, ...);

$podpara->parsetree( $ptree );

$parser->parsetree()->append( $podpara );

} sub verbatim {

my ($parser, $paragraph, $linenum, $podpara) = @;

$parser->parsetree()->append( $podpara );

} sub textblock {

my ($parser, $paragraph, $linenum, $podpara) = @;

my $ptree = $parser->parsetext({<>}, $paragraph, ...);

$podpara->parsetree( $ptree );

$parser->parsetree()->append( $podpara );

} ... package main; ...

my $parser = new MyPodParserTree2(...);

$parser->parsefromfile(...);

my $ptree = $parser->parsetree;

...

Now you have the entire POD document as one great big parse-tree. You

can even use the -eexxppaannddsseeqq option to ppaarrsseetteexxtt to insert whole dif-

ferent kinds of objects. Just don't expect PPoodd::::PPaarrsseerr to know what to

do with them after that. That will need to be in your code. Or, alter-

natively, you can insert any object you like so long as it conforms to the PPoodd::::PPaarrsseeTTrreeee interface. One could use this to create subclasses of PPoodd::::PPaarraaggrraapphhss and PPoodd::::IInntteerriioorrSSeeqquueenncceess for specific commands (or to create your own

custom node-types in the parse-tree) and add some kind of eemmiitt(()) method

to each custom node/subclass object in the tree. Then all you'd need to do is recursively walk the tree in the desired order, processing the children (most likely from left to right) by formatting them if they

are text-strings, or by calling their eemmiitt(()) method if they are

objects/references.

SEE ALSO

Pod::InputObjects, Pod::Select PPoodd::::IInnppuuttOObbjjeeccttss defines POD input objects corresponding to command

paragraphs, parse-trees, and interior-sequences.

PPoodd::::SSeelleecctt is a subclass of PPoodd::::PPaarrsseerr which provides the ability to selectively include and/or exclude sections of a POD document from

being translated based upon the current heading, subheading, subsub-

heading, etc. AUTHOR Please report bugs using . Brad Appleton Based on code for PPoodd::::TTeexxtt written by Tom Christiansen

perl v5.8.8 2001-09-21 Pod::Parser(3pm)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™