⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.pm

📁 mrtg 监控,请认真阅读您的文件包然后写出其具体功能
💻 PM
📖 第 1 页 / 共 5 页
字号:
This method might be invoked when printing diagnostic messages, for example,to obtain the name and line number of the all input files that are currentlybeing processed.=end __PRIVATE__=cutsub input_streams {   return $_[0]->{_INPUT_STREAMS};}##---------------------------------------------------------------------------=begin __PRIVATE__=head1 B<top_stream()>            $hashref = $parser->top_stream();Returns a reference to the hash-table that represents the elementthat is currently at the top (end) of the input stream stack(see L<"input_streams()">). The return value will be the C<undef>if the input stack is empty.This method might be used when printing diagnostic messages, for example,to obtain the name and line number of the current input file.=end __PRIVATE__=cutsub top_stream {   return $_[0]->{_TOP_STREAM} || undef;}#############################################################################=head1 PRIVATE METHODS AND DATAB<Pod::Parser> 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 B<Pod::Parser> source code.Private data fields are stored in the hash-object whose reference isreturned by the B<new()> constructor for this class. The names of allprivate methods and data-fields used by B<Pod::Parser> begin with aprefix of "_" and match the regular expression C</^_\w+$/>.=cut##---------------------------------------------------------------------------=begin _PRIVATE_=head1 B<_push_input_stream()>            $hashref = $parser->_push_input_stream($in_fh,$out_fh);This method will push the given input stream on the input stack andperform any necessary beginning-of-document or beginning-of-fileprocessing. The argument C<$in_fh> is the input stream filehandle topush, and C<$out_fh> is the corresponding output filehandle to use (ifit is not given or is undefined, then the current output stream is used,which defaults to standard output if it doesnt exist yet).The value returned will be reference to the hash-table that representsthe new top of the input stream stack. I<Please Note> that it ispossible for this method to use default values for the input and outputfile handles. If this happens, you will need to look at the C<INPUT>and C<OUTPUT> instance data members to determine their new values.=end _PRIVATE_=cutsub _push_input_stream {    my ($self, $in_fh, $out_fh) = @_;    local *myData = $self;    ## Initialize stuff for the entire document if this is *not*    ## an included file.    ##    ## NOTE: we need to be *very* careful when "defaulting" the output    ## filehandle. We only want to use a default value if this is the    ## beginning of the entire document (but *not* if this is an included    ## file).    unless (defined  $myData{_TOP_STREAM}) {        $out_fh  = \*STDOUT  unless (defined $out_fh);        $myData{_CUTTING}       = 1;   ## current "cutting" state        $myData{_INPUT_STREAMS} = [];  ## stack of all input streams    }    ## Initialize input indicators    $myData{_OUTFILE} = '(unknown)'  unless (defined  $myData{_OUTFILE});    $myData{_OUTPUT}  = $out_fh      if (defined  $out_fh);    $in_fh            = \*STDIN      unless (defined  $in_fh);    $myData{_INFILE}  = '(unknown)'  unless (defined  $myData{_INFILE});    $myData{_INPUT}   = $in_fh;    my $input_top     = $myData{_TOP_STREAM}                      = new Pod::InputSource(                            -name        => $myData{_INFILE},                            -handle      => $in_fh,                            -was_cutting => $myData{_CUTTING}                        );    local *input_stack = $myData{_INPUT_STREAMS};    push(@input_stack, $input_top);    ## Perform beginning-of-document and/or beginning-of-input processing    $self->begin_pod()  if (@input_stack == 1);    $self->begin_input();    return  $input_top;}##---------------------------------------------------------------------------=begin _PRIVATE_=head1 B<_pop_input_stream()>            $hashref = $parser->_pop_input_stream();This takes no arguments. It will perform any necessary end-of-file orend-of-document processing and then pop the current input stream fromthe top of the input stack.The value returned will be reference to the hash-table that representsthe new top of the input stream stack.=end _PRIVATE_=cutsub _pop_input_stream {    my ($self) = @_;    local *myData = $self;    local *input_stack = $myData{_INPUT_STREAMS};    ## Perform end-of-input and/or end-of-document processing    $self->end_input()  if (@input_stack > 0);    $self->end_pod()    if (@input_stack == 1);    ## Restore cutting state to whatever it was before we started    ## parsing this file.    my $old_top = pop(@input_stack);    $myData{_CUTTING} = $old_top->was_cutting();    ## Dont forget to reset the input indicators    my $input_top = undef;    if (@input_stack > 0) {       $input_top = $myData{_TOP_STREAM} = $input_stack[-1];       $myData{_INFILE}  = $input_top->name();       $myData{_INPUT}   = $input_top->handle();    } else {       delete $myData{_TOP_STREAM};       delete $myData{_INPUT_STREAMS};    }    return  $input_top;}#############################################################################=head1 TREE-BASED PARSINGIf straightforward stream-based parsing wont meet your needs (as islikely the case for tasks such as translating PODs into structuredmarkup languages like HTML and XML) then you may need to take thetree-based approach. Rather than doing everything in one pass andcalling the B<interpolate()> method to expand sequences into text, itmay be desirable to instead create a parse-tree using the B<parse_text()>method to return a tree-like structure which may contain an ordered listlist of children (each of which may be a text-string, or a similartree-like structure).Pay special attention to L<"METHODS FOR PARSING AND PROCESSING"> andto the objects described in L<Pod::InputObjects>. The former describesthe gory details and parameters for how to customize and extend theparsing behavior of B<Pod::Parser>. B<Pod::InputObjects> providesseveral objects that may all be used interchangeably as parse-trees. Themost obvious one is the B<Pod::ParseTree> object. It defines the basicinterface and functionality that all things trying to be a POD parse-treeshould do. A B<Pod::ParseTree> is defined such that each "node" may be atext-string, or a reference to another parse-tree.  Each B<Pod::Paragraph>object and each B<Pod::InteriorSequence> object also supports the basicparse-tree interface.The B<parse_text()> 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 B<parse_text()> 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).If you wish to turn an entire POD document into a parse-tree, that processis fairly straightforward. The B<parse_text()> method is the key to doingthis successfully. Every paragraph-callback (i.e. the polymorphic methodsfor B<command()>, B<verbatim()>, and B<textblock()> paragraphs) takesa B<Pod::Paragraph> object as an argument. Each paragraph object has aB<parse_tree()> method that can be used to get or set a correspondingparse-tree. So for each of those paragraph-callback methods, simply callB<parse_text()> with the options you desire, and then use the returnedparse-tree to assign to the given paragraph object.That 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 "options" for eachinvocation of B<parse_text>. Let's assume the desired option-set isgiven by the hash C<%options>. Then we might do something like thefollowing:    package MyPodParserTree;    @ISA = qw( Pod::Parser );    ...    sub begin_pod {        my $self = shift;        $self->{'-paragraphs'} = [];  ## 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->{'-paragraphs'} }, $pod_para;    }    sub verbatim {         my ($parser, $paragraph, $line_num, $pod_para) = @_;        push @{ $self->{'-paragraphs'} }, $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->{'-paragraphs'} }, $pod_para;    }    ...    package main;    ...    my $parser = new MyPodParserTree(...);    $parser->parse_from_file(...);    my $paragraphs_ref = $parser->{'-paragraphs'};Of course, in this module-author's humble opinion, I'd be more inclined touse the existing B<Pod::ParseTree> 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:    package MyPodParserTree2;    ...    sub begin_pod {        my $self = shift;        $self->{'-ptree'} = new Pod::ParseTree;  ## initialize parse-tree    }    sub parse_tree {        ## 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, $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;    ...Now you have the entire POD document as one great big parse-tree. Youcan even use the B<-expand_seq> option to B<parse_text> to insertwhole different kinds of objects. Just don't expect B<Pod::Parser>to 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 B<Pod::ParseTree> interface.One could use this to create subclasses of B<Pod::Paragraphs> andB<Pod::InteriorSequences> for specific commands (or to create your owncustom node-types in the parse-tree) and add some kind of B<emit()>method 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 B<emit()> method if theyare objects/references.=head1 SEE ALSOL<Pod::InputObjects>, L<Pod::Select>B<Pod::InputObjects> defines POD input objects corresponding tocommand paragraphs, parse-trees, and interior-sequences.B<Pod::Select> is a subclass of B<Pod::Parser> which provides the abilityto selectively include and/or exclude sections of a POD document from beingtranslated based upon the current heading, subheading, subsubheading, etc.=for __PRIVATE__B<Pod::Callbacks> is a subclass of B<Pod::Parser> which gives its usersthe ability the employ I<callback functions> instead of, or in additionto, overriding methods of the base class.=for __PRIVATE__B<Pod::Select> and B<Pod::Callbacks> do not override anymethods nor do they define any new methods with the same name. Becauseof this, they may I<both> be used (in combination) as a base class ofthe same subclass in order to combine their functionality withoutcausing any namespace clashes due to multiple inheritance.=head1 AUTHORBrad Appleton E<lt>bradapp@enteract.comE<gt>Based on code for B<Pod::Text> written byTom Christiansen E<lt>tchrist@mox.perl.comE<gt>=cut1;

⌨️ 快捷键说明

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