pod::parser.3

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· 3 代码 · 共 1,066 行 · 第 1/3 页

3
1,066
字号
Using \f(CW\*(C`$in_fh\->getline()\*(C'\fR, input is read line-by-line and assembledinto paragraphs or \*(L"blocks\*(R" (which are separated by lines containingnothing but whitespace). For each block of \s-1POD\s0 documentationencountered it will invoke a method to parse the given paragraph..PPIf a second argument is given then it should correspond to a filehandle whereoutput should be sent (otherwise the default output filehandle is\&\f(CW\*(C`STDOUT\*(C'\fR if no output filehandle is currently in use)..PP\&\fB\s-1NOTE:\s0\fR For performance reasons, this method caches the input stream atthe top of the stack in a local variable. Any attempts by clients tochange the stack contents during processing when in the midst executingof this method \fIwill not affect\fR the input stream used by the currentinvocation of this method..PPThis method does \fInot\fR usually need to be overridden by subclasses..SH "\fB\fP\f(BIparse_from_file()\fP\fB\fP".IX Header "parse_from_file()".Vb 1\&            $parser\->parse_from_file($filename,$outfile);.Ve.PPThis method takes a filename and does the following:.IP "\(bu" 2opens the input and output files for reading(creating the appropriate filehandles).IP "\(bu" 2invokes the \fB\f(BIparse_from_filehandle()\fB\fR method passing it thecorresponding input and output filehandles..IP "\(bu" 2closes the input and output files..PPIf the special input filename \*(L"\-\*(R" or \*(L"<&STDIN\*(R" is given then the \s-1STDIN\s0filehandle is used for input (and no open or close is performed). If noinput filename is specified then \*(L"\-\*(R" is implied..PPIf a second argument is given then it should be the name of the desiredoutput file. If the special output filename \*(L"\-\*(R" or \*(L">&STDOUT\*(R" is giventhen the \s-1STDOUT\s0 filehandle is used for output (and no open or close isperformed). If the special output filename \*(L">&STDERR\*(R" is given then the\&\s-1STDERR\s0 filehandle is used for output (and no open or close isperformed). If no output filehandle is currently in use and no outputfilename is specified, then \*(L"\-\*(R" is implied.Alternatively, an IO::String object is also accepted as an outputfile handle..PPThis method does \fInot\fR usually need to be overridden by subclasses..SH "ACCESSOR METHODS".IX Header "ACCESSOR METHODS"Clients of \fBPod::Parser\fR should use the following methods to accessinstance data fields:.SH "\fB\fP\f(BIerrorsub()\fP\fB\fP".IX Header "errorsub()".Vb 3\&            $parser\->errorsub("method_name");\&            $parser\->errorsub(\e&warn_user);\&            $parser\->errorsub(sub { print STDERR, @_ });.Ve.PPSpecifies the method or subroutine to use when printing error messagesabout \s-1POD\s0 syntax. The supplied method/subroutine \fImust\fR return \s-1TRUE\s0 uponsuccessful printing of the message. If \f(CW\*(C`undef\*(C'\fR is given, then the \fBwarn\fRbuiltin is used to issue error messages (this is the default behavior)..PP.Vb 5\&            my $errorsub = $parser\->errorsub()\&            my $errmsg = "This is an error message!\en"\&            (ref $errorsub) and &{$errorsub}($errmsg)\&                or (defined $errorsub) and $parser\->$errorsub($errmsg)\&                    or  warn($errmsg);.Ve.PPReturns a method name, or else a reference to the user-supplied subroutineused to print error messages. Returns \f(CW\*(C`undef\*(C'\fR if the \fBwarn\fR builtinis used to issue error messages (this is the default behavior)..SH "\fB\fP\f(BIcutting()\fP\fB\fP".IX Header "cutting()".Vb 1\&            $boolean = $parser\->cutting();.Ve.PPReturns the current \f(CW\*(C`cutting\*(C'\fR state: a boolean-valued scalar whichevaluates to true if text from the input file is currently being \*(L"cut\*(R"(meaning it is \fInot\fR considered part of the \s-1POD\s0 document)..PP.Vb 1\&            $parser\->cutting($boolean);.Ve.PPSets the current \f(CW\*(C`cutting\*(C'\fR state to the given value and returns theresult..SH "\fB\fP\f(BIparseopts()\fP\fB\fP".IX Header "parseopts()"When invoked with no additional arguments, \fBparseopts\fR returns a hashtableof all the current parsing options..PP.Vb 3\&            ## See if we are parsing non\-POD sections as well as POD ones\&            my %opts = $parser\->parseopts();\&            $opts{\*(Aq\-want_nonPODs}\*(Aq and print "\-want_nonPODs\en";.Ve.PPWhen invoked using a single string, \fBparseopts\fR treats the string as thename of a parse-option and returns its corresponding value if it exists(returns \f(CW\*(C`undef\*(C'\fR if it doesn't)..PP.Vb 3\&            ## Did we ask to see \*(Aq=cut\*(Aq paragraphs?\&            my $want_cut = $parser\->parseopts(\*(Aq\-process_cut_cmd\*(Aq);\&            $want_cut and print "\-process_cut_cmd\en";.Ve.PPWhen invoked with multiple arguments, \fBparseopts\fR treats them askey/value pairs and the specified parse-option names are set to thegiven values. Any unspecified parse-options are unaffected..PP.Vb 2\&            ## Set them back to the default\&            $parser\->parseopts(\-warnings => 0);.Ve.PPWhen passed a single hash-ref, \fBparseopts\fR uses that hash to completelyreset the existing parse-options, all previous parse-option valuesare lost..PP.Vb 2\&            ## Reset all options to default \&            $parser\->parseopts( { } );.Ve.PPSee \*(L"\s-1PARSING\s0 \s-1OPTIONS\s0\*(R" for more information on the name and meaning of eachparse-option currently recognized..SH "\fB\fP\f(BIoutput_file()\fP\fB\fP".IX Header "output_file()".Vb 1\&            $fname = $parser\->output_file();.Ve.PPReturns the name of the output file being written..SH "\fB\fP\f(BIoutput_handle()\fP\fB\fP".IX Header "output_handle()".Vb 1\&            $fhandle = $parser\->output_handle();.Ve.PPReturns the output filehandle object..SH "\fB\fP\f(BIinput_file()\fP\fB\fP".IX Header "input_file()".Vb 1\&            $fname = $parser\->input_file();.Ve.PPReturns the name of the input file being read..SH "\fB\fP\f(BIinput_handle()\fP\fB\fP".IX Header "input_handle()".Vb 1\&            $fhandle = $parser\->input_handle();.Ve.PPReturns the current input filehandle object..SH "PRIVATE METHODS AND DATA".IX Header "PRIVATE METHODS AND DATA"\&\fBPod::Parser\fR makes use of several internal methods and data fieldswhich clients should not need to see or use. For the sake of avoidingname collisions for client data and methods, these methods and fieldsare briefly discussed here. Determined hackers may obtain furtherinformation about them by reading the \fBPod::Parser\fR source code..PPPrivate data fields are stored in the hash-object whose reference isreturned by the \fB\f(BInew()\fB\fR constructor for this class. The names of allprivate methods and data-fields used by \fBPod::Parser\fR begin with aprefix of \*(L"_\*(R" and match the regular expression \f(CW\*(C`/^_\ew+$/\*(C'\fR..SH "TREE-BASED PARSING".IX Header "TREE-BASED PARSING"If straightforward stream-based parsing wont meet your needs (as islikely the case for tasks such as translating PODs into structuredmarkup languages like \s-1HTML\s0 and \s-1XML\s0) then you may need to take thetree-based approach. Rather than doing everything in one pass andcalling the \fB\f(BIinterpolate()\fB\fR method to expand sequences into text, itmay be desirable to instead create a parse-tree using the \fB\f(BIparse_text()\fB\fRmethod to return a tree-like structure which may contain an orderedlist of children (each of which may be a text-string, or a similartree-like structure)..PPPay special attention to \*(L"\s-1METHODS\s0 \s-1FOR\s0 \s-1PARSING\s0 \s-1AND\s0 \s-1PROCESSING\s0\*(R" andto the objects described in Pod::InputObjects. The former describesthe gory details and parameters for how to customize and extend theparsing behavior of \fBPod::Parser\fR. \fBPod::InputObjects\fR providesseveral objects that may all be used interchangeably as parse-trees. Themost obvious one is the \fBPod::ParseTree\fR object. It defines the basicinterface and functionality that all things trying to be a \s-1POD\s0 parse-treeshould do. A \fBPod::ParseTree\fR is defined such that each \*(L"node\*(R" may be atext-string, or a reference to another parse-tree.  Each \fBPod::Paragraph\fRobject and each \fBPod::InteriorSequence\fR object also supports the basicparse-tree interface..PPThe \fB\f(BIparse_text()\fB\fR method takes a given paragraph of text, andreturns a parse-tree that contains one or more children, each of whichmay be a text-string, or an InteriorSequence object. There are alsocallback-options that may be passed to \fB\f(BIparse_text()\fB\fR to customizethe way it expands or transforms interior-sequences, as well as thereturned result. These callbacks can be used to create a parse-treewith custom-made objects (which may or may not support the parse-treeinterface, depending on how you choose to do it)..PPIf you wish to turn an entire \s-1POD\s0 document into a parse-tree, that processis fairly straightforward. The \fB\f(BIparse_text()\fB\fR method is the key to doingthis successfully. Every paragraph-callback (i.e. the polymorphic methodsfor \fB\f(BIcommand()\fB\fR, \fB\f(BIverbatim()\fB\fR, and \fB\f(BItextblock()\fB\fR paragraphs) takesa \fBPod::Paragraph\fR object as an argument. Each paragraph object has a\&\fB\f(BIparse_tree()\fB\fR method that can be used to get or set a correspondingparse-tree. So for each of those paragraph-callback methods, simply call\&\fB\f(BIparse_text()\fB\fR with the options you desire, and then use the returnedparse-tree to assign to the given paragraph object..PPThat gives you a parse-tree for each paragraph \- so now all you need isan ordered list of paragraphs. You can maintain that yourself as a dataelement in the object/hash. The most straightforward way would be simplyto use an array-ref, with the desired set of custom \*(L"options\*(R" for eachinvocation of \fBparse_text\fR. Let's assume the desired option-set isgiven by the hash \f(CW%options\fR. Then we might do something like thefollowing:.PP.Vb 1\&    package MyPodParserTree;\&\&    @ISA = qw( Pod::Parser );\&\&    ...\&\&    sub begin_pod {\&        my $self = shift;\&        $self\->{\*(Aq\-paragraphs\*(Aq} = [];  ## initialize paragraph list\&    }\&\&    sub command { \&        my ($parser, $command, $paragraph, $line_num, $pod_para) = @_;\&        my $ptree = $parser\->parse_text({%options}, $paragraph, ...);\&        $pod_para\->parse_tree( $ptree );\&        push @{ $self\->{\*(Aq\-paragraphs\*(Aq} }, $pod_para;\&    }\&\&    sub verbatim { \&        my ($parser, $paragraph, $line_num, $pod_para) = @_;\&        push @{ $self\->{\*(Aq\-paragraphs\*(Aq} }, $pod_para;\&    }\&\&    sub textblock { \&        my ($parser, $paragraph, $line_num, $pod_para) = @_;\&        my $ptree = $parser\->parse_text({%options}, $paragraph, ...);\&        $pod_para\->parse_tree( $ptree );\&        push @{ $self\->{\*(Aq\-paragraphs\*(Aq} }, $pod_para;\&    }\&\&    ...\&\&    package main;\&    ...\&    my $parser = new MyPodParserTree(...);\&    $parser\->parse_from_file(...);\&    my $paragraphs_ref = $parser\->{\*(Aq\-paragraphs\*(Aq};.Ve.PPOf course, in this module-author's humble opinion, I'd be more inclined touse the existing \fBPod::ParseTree\fR object than a simple array. That wayeverything in it, paragraphs and sequences, all respond to the same coreinterface for all parse-tree nodes. The result would look something like:.PP.Vb 1\&    package MyPodParserTree2;\&\&    ...\&\&    sub begin_pod {\&        my $self = shift;\&        $self\->{\*(Aq\-ptree\*(Aq} = new Pod::ParseTree;  ## initialize parse\-tree\&    }\&\&    sub parse_tree {\&        ## convenience method to get/set the parse\-tree for the entire POD\&        (@_ > 1)  and  $_[0]\->{\*(Aq\-ptree\*(Aq} = $_[1];\&        return $_[0]\->{\*(Aq\-ptree\*(Aq};\&    }\&\&    sub command { \&        my ($parser, $command, $paragraph, $line_num, $pod_para) = @_;\&        my $ptree = $parser\->parse_text({<<options>>}, $paragraph, ...);\&        $pod_para\->parse_tree( $ptree );\&        $parser\->parse_tree()\->append( $pod_para );\&    }\&\&    sub verbatim { \&        my ($parser, $paragraph, $line_num, $pod_para) = @_;\&        $parser\->parse_tree()\->append( $pod_para );\&    }\&\&    sub textblock { \&        my ($parser, $paragraph, $line_num, $pod_para) = @_;\&        my $ptree = $parser\->parse_text({<<options>>}, $paragraph, ...);\&        $pod_para\->parse_tree( $ptree );\&        $parser\->parse_tree()\->append( $pod_para );\&    }\&\&    ...\&\&    package main;\&    ...\&    my $parser = new MyPodParserTree2(...);\&    $parser\->parse_from_file(...);\&    my $ptree = $parser\->parse_tree;\&    ....Ve.PPNow you have the entire \s-1POD\s0 document as one great big parse-tree. Youcan even use the \fB\-expand_seq\fR option to \fBparse_text\fR to insertwhole different kinds of objects. Just don't expect \fBPod::Parser\fRto know what to do with them after that. That will need to be in yourcode. Or, alternatively, you can insert any object you like so long asit conforms to the \fBPod::ParseTree\fR interface..PPOne could use this to create subclasses of \fBPod::Paragraphs\fR and\&\fBPod::InteriorSequences\fR for specific commands (or to create your owncustom node-types in the parse-tree) and add some kind of \fB\f(BIemit()\fB\fRmethod to each custom node/subclass object in the tree. Then all you'dneed to do is recursively walk the tree in the desired order, processingthe children (most likely from left to right) by formatting them ifthey are text-strings, or by calling their \fB\f(BIemit()\fB\fR method if theyare objects/references..SH "CAVEATS".IX Header "CAVEATS"Please note that \s-1POD\s0 has the notion of \*(L"paragraphs\*(R": this is somethingstarting \fIafter\fR a blank (read: empty) line, with the single exceptionof the file start, which is also starting a paragraph. That means thatespecially a command (e.g. \f(CW\*(C`=head1\*(C'\fR) \fImust\fR be preceded with a blankline; \f(CW\*(C`_\|_END_\|_\*(C'\fR is \fInot\fR a blank line..SH "SEE ALSO".IX Header "SEE ALSO"Pod::InputObjects, Pod::Select.PP\&\fBPod::InputObjects\fR defines \s-1POD\s0 input objects corresponding tocommand paragraphs, parse-trees, and interior-sequences..PP\&\fBPod::Select\fR is a subclass of \fBPod::Parser\fR which provides the abilityto selectively include and/or exclude sections of a \s-1POD\s0 document from beingtranslated based upon the current heading, subheading, subsubheading, etc..SH "AUTHOR".IX Header "AUTHOR"Please report bugs using <http://rt.cpan.org>..PPBrad Appleton <bradapp@enteract.com>.PPBased on code for \fBPod::Text\fR written byTom Christiansen <tchrist@mox.perl.com>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?