📄 parser.pm
字号:
my ($self, $text, $line_num, $pod_para) = @_; my $out_fh = $self->{_OUTPUT}; print $out_fh $self->interpolate($text, $line_num);}##---------------------------------------------------------------------------=head1 B<interior_sequence()> $parser->interior_sequence($seq_cmd,$seq_arg,$pod_seq);This method should be overridden by subclasses to take the appropriateaction when an interior sequence is encountered. An interior sequence isan embedded command within a block of text which appears as a commandname (usually a single uppercase character) followed immediately by astring of text which is enclosed in angle brackets. This method ispassed the sequence command C<$seq_cmd> and the corresponding textC<$seq_arg>. It is invoked by the B<interpolate()> method for each interiorsequence that occurs in the string that it is passed. It should returnthe desired text string to be used in place of the interior sequence.The C<$pod_seq> argument is a reference to a C<Pod::InteriorSequence>object which contains further information about the interior sequence.Please see L<Pod::InputObjects> for details if you need to access thisadditional information.Subclass implementations of this method may wish to invoke the B<nested()> method of C<$pod_seq> to see if it is nested insidesome other interior-sequence (and if so, which kind).The base class implementation of the B<interior_sequence()> methodsimply returns the raw text of the interior sequence (as it occurredin the input) to the caller.=cutsub interior_sequence { my ($self, $seq_cmd, $seq_arg, $pod_seq) = @_; ## Just return the raw text of the interior sequence return $pod_seq->raw_text();}#############################################################################=head1 OPTIONAL SUBROUTINE/METHOD OVERRIDESB<Pod::Parser> provides several methods which subclasses may want to overrideto perform any special pre/post-processing. These methods do I<not> have tobe overridden, but it may be useful for subclasses to take advantage of them.=cut##---------------------------------------------------------------------------=head1 B<new()> my $parser = Pod::Parser->new();This is the constructor for B<Pod::Parser> and its subclasses. YouI<do not> need to override this method! It is capable of constructingsubclass objects as well as base class objects, provided you useany of the following constructor invocation styles: my $parser1 = MyParser->new(); my $parser2 = new MyParser(); my $parser3 = $parser2->new();where C<MyParser> is some subclass of B<Pod::Parser>.Using the syntax C<MyParser::new()> to invoke the constructor is I<not>recommended, but if you insist on being able to do this, then thesubclass I<will> need to override the B<new()> constructor method. Ifyou do override the constructor, you I<must> be sure to invoke theB<initialize()> method of the newly blessed object.Using any of the above invocations, the first argument to theconstructor is always the corresponding package name (or objectreference). No other arguments are required, but if desired, anassociative array (or hash-table) my be passed to the B<new()>constructor, as in: my $parser1 = MyParser->new( MYDATA => $value1, MOREDATA => $value2 ); my $parser2 = new MyParser( -myflag => 1 );All arguments passed to the B<new()> constructor will be treated askey/value pairs in a hash-table. The newly constructed object will beinitialized by copying the contents of the given hash-table (which mayhave been empty). The B<new()> constructor for this class and all of itssubclasses returns a blessed reference to the initialized object (hash-table).=cutsub new { ## Determine if we were called via an object-ref or a classname my $this = shift; my $class = ref($this) || $this; ## Any remaining arguments are treated as initial values for the ## hash that is used to represent this object. my %params = @_; my $self = { %params }; ## Bless ourselves into the desired class and perform any initialization bless $self, $class; $self->initialize(); return $self;}##---------------------------------------------------------------------------=head1 B<initialize()> $parser->initialize();This method performs any necessary object initialization. It takes noarguments (other than the object instance of course, which is typicallycopied to a local variable named C<$self>). If subclasses override thismethod then they I<must> be sure to invoke C<$self-E<gt>SUPER::initialize()>.=cutsub initialize { #my $self = shift; #return;}##---------------------------------------------------------------------------=head1 B<begin_pod()> $parser->begin_pod();This method is invoked at the beginning of processing for each PODdocument that is encountered in the input. Subclasses should overridethis method to perform any per-document initialization.=cutsub begin_pod { #my $self = shift; #return;}##---------------------------------------------------------------------------=head1 B<begin_input()> $parser->begin_input();This method is invoked by B<parse_from_filehandle()> immediately I<before>processing input from a filehandle. The base class implementation doesnothing, however, subclasses may override it to perform any per-fileinitializations.Note that if multiple files are parsed for a single POD document(perhaps the result of some future C<=include> directive) this methodis invoked for every file that is parsed. If you wish to perform certaininitializations once per document, then you should use B<begin_pod()>.=cutsub begin_input { #my $self = shift; #return;}##---------------------------------------------------------------------------=head1 B<end_input()> $parser->end_input();This method is invoked by B<parse_from_filehandle()> immediately I<after>processing input from a filehandle. The base class implementation doesnothing, however, subclasses may override it to perform any per-filecleanup actions.Please note that if multiple files are parsed for a single POD document(perhaps the result of some kind of C<=include> directive) this methodis invoked for every file that is parsed. If you wish to perform certaincleanup actions once per document, then you should use B<end_pod()>.=cutsub end_input { #my $self = shift; #return;}##---------------------------------------------------------------------------=head1 B<end_pod()> $parser->end_pod();This method is invoked at the end of processing for each POD documentthat is encountered in the input. Subclasses should override this methodto perform any per-document finalization.=cutsub end_pod { #my $self = shift; #return;}##---------------------------------------------------------------------------=head1 B<preprocess_line()> $textline = $parser->preprocess_line($text, $line_num);This method should be overridden by subclasses that wish to performany kind of preprocessing for each I<line> of input (I<before> it hasbeen determined whether or not it is part of a POD paragraph). Theparameter C<$text> is the input line; and the parameter C<$line_num> isthe line number of the corresponding text line.The value returned should correspond to the new text to use in itsplace. If the empty string or an undefined value is returned then nofurther processing will be performed for this line.Please note that the B<preprocess_line()> method is invoked I<before>the B<preprocess_paragraph()> method. After all (possibly preprocessed)lines in a paragraph have been assembled together and it has beendetermined that the paragraph is part of the POD documentation from oneof the selected sections, then B<preprocess_paragraph()> is invoked.The base class implementation of this method returns the given text.=cutsub preprocess_line { my ($self, $text, $line_num) = @_; return $text;}##---------------------------------------------------------------------------=head1 B<preprocess_paragraph()> $textblock = $parser->preprocess_paragraph($text, $line_num);This method should be overridden by subclasses that wish to perform anykind of preprocessing for each block (paragraph) of POD documentationthat appears in the input stream. The parameter C<$text> is the PODparagraph from the input file; and the parameter C<$line_num> is theline number for the beginning of the corresponding paragraph.The value returned should correspond to the new text to use in itsplace If the empty string is returned or an undefined value isreturned, then the given C<$text> is ignored (not processed).This method is invoked after gathering up all the lines in a paragraphand after determining the cutting state of the paragraph,but before trying to further parse or interpret them. AfterB<preprocess_paragraph()> returns, the current cutting state (whichis returned by C<$self-E<gt>cutting()>) is examined. If it evaluatesto true then input text (including the given C<$text>) is cut (notprocessed) until the next POD directive is encountered.Please note that the B<preprocess_line()> method is invoked I<before>the B<preprocess_paragraph()> method. After all (possibly preprocessed)lines in a paragraph have been assembled together and either it has beendetermined that the paragraph is part of the POD documentation from oneof the selected sections or the C<-want_nonPODs> option is true,then B<preprocess_paragraph()> is invoked.The base class implementation of this method returns the given text.=cutsub preprocess_paragraph { my ($self, $text, $line_num) = @_; return $text;}#############################################################################=head1 METHODS FOR PARSING AND PROCESSINGB<Pod::Parser> provides several methods to process input text. Thesemethods typically won't need to be overridden (and in some cases theycan't be overridden), but subclasses may want to invoke them to exploittheir functionality.=cut##---------------------------------------------------------------------------=head1 B<parse_text()> $ptree1 = $parser->parse_text($text, $line_num); $ptree2 = $parser->parse_text({%opts}, $text, $line_num); $ptree3 = $parser->parse_text(\%opts, $text, $line_num);This method is useful if you need to perform your own interpolation of interior sequences and can't rely upon B<interpolate> to expandthem in simple bottom-up order order.The parameter C<$text> is a string or block of text to be parsedfor interior sequences; and the parameter C<$line_num> is theline number curresponding to the beginning of C<$text>.B<parse_text()> will parse the given text into a parse-tree of "nodes."and interior-sequences. Each "node" in the parse tree is either atext-string, or a B<Pod::InteriorSequence>. The result returned is aparse-tree of type B<Pod::ParseTree>. Please see L<Pod::InputObjects>for more information about B<Pod::InteriorSequence> and B<Pod::ParseTree>.If desired, an optional hash-ref may be specified as the first argumentto customize certain aspects of the parse-tree that is created andreturned. The set of recognized option keywords are:=over 3=item B<-expand_seq> =E<gt> I<code-ref>|I<method-name>Normally, the parse-tree returned by B<parse_text()> will contain anunexpanded C<Pod::InteriorSequence> object for each interior-sequenceencountered. Specifying B<-expand_seq> tells B<parse_text()> 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 theexpanded result.If a subroutine reference was given, it is invoked as: &$code_ref( $parser, $sequence )and if a method-name was given, it is invoked as: $parser->method_name( $sequence )where C<$parser> is a reference to the parser object, and C<$sequence>is a reference to the interior-sequence object.[I<NOTE>: If the B<interior_sequence()> method is specified, then it isinvoked according to the interface specified in L<"interior_sequence()">].=item B<-expand_text> =E<gt> I<code-ref>|I<method-name>Normally, the parse-tree returned by B<parse_text()> will contain atext-string for each contiguous sequence of characters outside of aninterior-sequence. Specifying B<-expand_text> tells B<parse_text()> to"preprocess" every such text-string it sees by invoking the referencedfunction (or named method of the parser object) and using the return valueas the preprocessed (or "expanded") result. [Note that if the result isan interior-sequence, then it will I<not> be expanded as specified by theB<-expand_seq> option; Any such recursive expansion needs to be handled bythe specified callback routine.]If a subroutine reference was given, it is invoked as: &$code_ref( $parser, $text, $ptree_node )and if a method-name was given, it is invoked as: $parser->method_name( $text, $ptree_node )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -