text::balanced.3
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· 3 代码 · 共 1,363 行 · 第 1/4 页
3
1,363 行
.IX Subsection "extract_multiple"The \f(CW\*(C`extract_multiple\*(C'\fR subroutine takes a string to be processed and a list of extractors (subroutines or regular expressions) to apply to that string..PPIn an array context \f(CW\*(C`extract_multiple\*(C'\fR returns an array of substringsof the original string, as extracted by the specified extractors.In a scalar context, \f(CW\*(C`extract_multiple\*(C'\fR returns the firstsubstring successfully extracted from the original string. In bothscalar and void contexts the original string has the first successfullyextracted substring removed from it. In all contexts\&\f(CW\*(C`extract_multiple\*(C'\fR starts at the current \f(CW\*(C`pos\*(C'\fR of the string, andsets that \f(CW\*(C`pos\*(C'\fR appropriately after it matches..PPHence, the aim of of a call to \f(CW\*(C`extract_multiple\*(C'\fR in a list contextis to split the processed string into as many non-overlapping fields aspossible, by repeatedly applying each of the specified extractorsto the remainder of the string. Thus \f(CW\*(C`extract_multiple\*(C'\fR isa generalized form of Perl's \f(CW\*(C`split\*(C'\fR subroutine..PPThe subroutine takes up to four optional arguments:.IP "1." 4A string to be processed (\f(CW$_\fR if the string is omitted or \f(CW\*(C`undef\*(C'\fR).IP "2." 4A reference to a list of subroutine references and/or qr// objects and/orliteral strings and/or hash references, specifying the extractorsto be used to split the string. If this argument is omitted (or\&\f(CW\*(C`undef\*(C'\fR) the list:.Sp.Vb 5\& [\& sub { extract_variable($_[0], \*(Aq\*(Aq) },\& sub { extract_quotelike($_[0],\*(Aq\*(Aq) },\& sub { extract_codeblock($_[0],\*(Aq{}\*(Aq,\*(Aq\*(Aq) },\& ].Ve.Spis used..IP "3." 4An number specifying the maximum number of fields to return. If thisargument is omitted (or \f(CW\*(C`undef\*(C'\fR), split continues as long as possible..SpIf the third argument is \fIN\fR, then extraction continues until \fIN\fR fieldshave been successfully extracted, or until the string has been completely processed..SpNote that in scalar and void contexts the value of this argument is automatically reset to 1 (under \f(CW\*(C`\-w\*(C'\fR, a warning is issued if the argument has to be reset)..IP "4." 4A value indicating whether unmatched substrings (see below) within thetext should be skipped or returned as fields. If the value is true,such substrings are skipped. Otherwise, they are returned..PPThe extraction process works by applying each extractor insequence to the text string..PPIf the extractor is a subroutine it is called in a list context and isexpected to return a list of a single element, namely the extractedtext. It may optionally also return two further arguments: a stringrepresenting the text left after extraction (like $' for a patternmatch), and a string representing any prefix skipped before theextraction (like $` in a pattern match). Note that this is designedto facilitate the use of other Text::Balanced subroutines with\&\f(CW\*(C`extract_multiple\*(C'\fR. Note too that the value returned by an extractorsubroutine need not bear any relationship to the corresponding substringof the original text (see examples below)..PPIf the extractor is a precompiled regular expression or a string,it is matched against the text in a scalar context with a leading\&'\eG' and the gc modifiers enabled. The extracted value is either\&\f(CW$1\fR if that variable is defined after the match, or else thecomplete match (i.e. $&)..PPIf the extractor is a hash reference, it must contain exactly one element.The value of that element is one of theabove extractor types (subroutine reference, regular expression, or string).The key of that element is the name of a class into which the successfulreturn value of the extractor will be blessed..PPIf an extractor returns a defined value, that value is immediatelytreated as the next extracted field and pushed onto the list of fields.If the extractor was specified in a hash reference, the field is alsoblessed into the appropriate class,.PPIf the extractor fails to match (in the case of a regex extractor), or returns an empty list or an undefined value (in the case of a subroutine extractor), it isassumed to have failed to extract.If none of the extractor subroutines succeeds, then onecharacter is extracted from the start of the text and the extractionsubroutines reapplied. Characters which are thus removed are accumulated andeventually become the next field (unless the fourth argument is true, in whichcase they are discarded)..PPFor example, the following extracts substrings that are valid Perl variables:.PP.Vb 3\& @fields = extract_multiple($text,\& [ sub { extract_variable($_[0]) } ],\& undef, 1);.Ve.PPThis example separates a text into fields which are quote delimited,curly bracketed, and anything else. The delimited and bracketedparts are also blessed to identify them (the \*(L"anything else\*(R" is unblessed):.PP.Vb 5\& @fields = extract_multiple($text,\& [\& { Delim => sub { extract_delimited($_[0],q{\*(Aq"}) } },\& { Brack => sub { extract_bracketed($_[0],\*(Aq{}\*(Aq) } },\& ]);.Ve.PPThis call extracts the next single substring that is a valid Perl quotelikeoperator (and removes it from \f(CW$text\fR):.PP.Vb 4\& $quotelike = extract_multiple($text,\& [\& sub { extract_quotelike($_[0]) },\& ], undef, 1);.Ve.PPFinally, here is yet another way to do comma-separated value parsing:.PP.Vb 6\& @fields = extract_multiple($csv_text,\& [\& sub { extract_delimited($_[0],q{\*(Aq"}) },\& qr/([^,]+)(.*)/,\& ],\& undef,1);.Ve.PPThe list in the second argument means:\&\fI\*(L"Try and extract a ' or \*(R" delimited string, otherwise extract anything up to a comma..."\fR.The undef third argument means:\&\fI\*(L"...as many times as possible...\*(R"\fR,and the true value in the fourth argument means\&\fI\*(L"...discarding anything else that appears (i.e. the commas)\*(R"\fR..PPIf you wanted the commas preserved as separate fields (i.e. like splitdoes if your split pattern has capturing parentheses), you wouldjust make the last parameter undefined (or remove it)..ie n .Sh """gen_delimited_pat""".el .Sh "\f(CWgen_delimited_pat\fP".IX Subsection "gen_delimited_pat"The \f(CW\*(C`gen_delimited_pat\*(C'\fR subroutine takes a single (string) argument and.PP.Vb 1\& gen_delimited_pat(q{\*(Aq"}).Ve.PPreturns the regex:.PP.Vb 1\& (?:\e"(?:\e\e\e"|(?!\e").)*\e"|\e\*(Aq(?:\e\e\e\*(Aq|(?!\e\*(Aq).)*\e\*(Aq).Ve.PPNote that the specified delimiters are automatically quotemeta'd..PPA typical use of \f(CW\*(C`gen_delimited_pat\*(C'\fR would be to build special purpose tagsfor \f(CW\*(C`extract_tagged\*(C'\fR. For example, to properly ignore \*(L"empty\*(R" \s-1XML\s0 elements(which might contain quoted strings):.PP.Vb 1\& my $empty_tag = \*(Aq<(\*(Aq . gen_delimited_pat(q{\*(Aq"}) . \*(Aq|.)+/>\*(Aq;\&\& extract_tagged($text, undef, undef, undef, {ignore => [$empty_tag]} );.Ve.PP\&\f(CW\*(C`gen_delimited_pat\*(C'\fR may also be called with an optional second argument,which specifies the \*(L"escape\*(R" character(s) to be used for each delimiter.For example to match a Pascal-style string (where ' is the delimiterand '' is a literal ' within the string):.PP.Vb 1\& gen_delimited_pat(q{\*(Aq},q{\*(Aq});.Ve.PPDifferent escape characters can be specified for different delimiters.For example, to specify that '/' is the escape for single quotesand '%' is the escape for double quotes:.PP.Vb 1\& gen_delimited_pat(q{\*(Aq"},q{/%});.Ve.PPIf more delimiters than escape chars are specified, the last escape charis used for the remaining delimiters.If no escape char is specified for a given specified delimiter, '\e' is used..ie n .Sh """delimited_pat""".el .Sh "\f(CWdelimited_pat\fP".IX Subsection "delimited_pat"Note that \f(CW\*(C`gen_delimited_pat\*(C'\fR was previously called \f(CW\*(C`delimited_pat\*(C'\fR.That name may still be used, but is now deprecated..SH "DIAGNOSTICS".IX Header "DIAGNOSTICS"In a list context, all the functions return \f(CW\*(C`(undef,$original_text)\*(C'\fRon failure. In a scalar context, failure is indicated by returning \f(CW\*(C`undef\*(C'\fR(in this case the input text is not modified in any way)..PPIn addition, on failure in \fIany\fR context, the \f(CW$@\fR variable is set.Accessing \f(CW\*(C`$@\->{error}\*(C'\fR returns one of the error diagnostics listedbelow.Accessing \f(CW\*(C`$@\->{pos}\*(C'\fR returns the offset into the original string atwhich the error was detected (although not necessarily where it occurred!)Printing \f(CW$@\fR directly produces the error message, with the offset appended.On success, the \f(CW$@\fR variable is guaranteed to be \f(CW\*(C`undef\*(C'\fR..PPThe available diagnostics are:.ie n .IP """Did not find a suitable bracket: ""%s""""" 4.el .IP "\f(CWDid not find a suitable bracket: ``%s''\fR" 4.IX Item "Did not find a suitable bracket: ""%s"""The delimiter provided to \f(CW\*(C`extract_bracketed\*(C'\fR was not one of\&\f(CW\*(Aq()[]<>{}\*(Aq\fR..ie n .IP """Did not find prefix: /%s/""" 4.el .IP "\f(CWDid not find prefix: /%s/\fR" 4.IX Item "Did not find prefix: /%s/"A non-optional prefix was specified but wasn't found at the start of the text..ie n .IP """Did not find opening bracket after prefix: ""%s""""" 4.el .IP "\f(CWDid not find opening bracket after prefix: ``%s''\fR" 4.IX Item "Did not find opening bracket after prefix: ""%s"""\&\f(CW\*(C`extract_bracketed\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR was expecting aparticular kind of bracket at the start of the text, and didn't find it..ie n .IP """No quotelike operator found after prefix: ""%s""""" 4.el .IP "\f(CWNo quotelike operator found after prefix: ``%s''\fR" 4.IX Item "No quotelike operator found after prefix: ""%s"""\&\f(CW\*(C`extract_quotelike\*(C'\fR didn't find one of the quotelike operators \f(CW\*(C`q\*(C'\fR,\&\f(CW\*(C`qq\*(C'\fR, \f(CW\*(C`qw\*(C'\fR, \f(CW\*(C`qx\*(C'\fR, \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`tr\*(C'\fR or \f(CW\*(C`y\*(C'\fR at the start of the substringit was extracting..ie n .IP """Unmatched closing bracket: ""%c""""" 4.el .IP "\f(CWUnmatched closing bracket: ``%c''\fR" 4.IX Item "Unmatched closing bracket: ""%c"""\&\f(CW\*(C`extract_bracketed\*(C'\fR, \f(CW\*(C`extract_quotelike\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR encountereda closing bracket where none was expected..ie n .IP """Unmatched opening bracket(s): ""%s""""" 4.el .IP "\f(CWUnmatched opening bracket(s): ``%s''\fR" 4.IX Item "Unmatched opening bracket(s): ""%s"""\&\f(CW\*(C`extract_bracketed\*(C'\fR, \f(CW\*(C`extract_quotelike\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR ran out of characters in the text before closing one or more levels of nestedbrackets..ie n .IP """Unmatched embedded quote (%s)""" 4.el .IP "\f(CWUnmatched embedded quote (%s)\fR" 4.IX Item "Unmatched embedded quote (%s)"\&\f(CW\*(C`extract_bracketed\*(C'\fR attempted to match an embedded quoted substring, butfailed to find a closing quote to match it..ie n .IP """Did not find closing delimiter to match \*(Aq%s\*(Aq""" 4.el .IP "\f(CWDid not find closing delimiter to match \*(Aq%s\*(Aq\fR" 4.IX Item "Did not find closing delimiter to match %s"\&\f(CW\*(C`extract_quotelike\*(C'\fR was unable to find a closing delimiter to match theone that opened the quote-like operation..ie n .IP """Mismatched closing bracket: expected ""%c"" but found ""%s""""" 4.el .IP "\f(CWMismatched closing bracket: expected ``%c'' but found ``%s''\fR" 4.IX Item "Mismatched closing bracket: expected ""%c"" but found ""%s"""\&\f(CW\*(C`extract_bracketed\*(C'\fR, \f(CW\*(C`extract_quotelike\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR founda valid bracket delimiter, but it was the wrong species. This usuallyindicates a nesting error, but may indicate incorrect quoting or escaping..ie n .IP """No block delimiter found after quotelike ""%s""""" 4.el .IP "\f(CWNo block delimiter found after quotelike ``%s''\fR" 4.IX Item "No block delimiter found after quotelike ""%s"""\&\f(CW\*(C`extract_quotelike\*(C'\fR or \f(CW\*(C`extract_codeblock\*(C'\fR found one of thequotelike operators \f(CW\*(C`q\*(C'\fR, \f(CW\*(C`qq\*(C'\fR, \f(CW\*(C`qw\*(C'\fR, \f(CW\*(C`qx\*(C'\fR, \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`tr\*(C'\fR or \f(CW\*(C`y\*(C'\fRwithout a suitable block after it..ie n .IP """Did not find leading dereferencer""" 4.el .IP "\f(CWDid not find leading dereferencer\fR" 4.IX Item "Did not find leading dereferencer"\&\f(CW\*(C`extract_variable\*(C'\fR was expecting one of '$', '@', or '%' at the start ofa variable, but didn't find any of them..ie n .IP """Bad identifier after dereferencer""" 4.el .IP "\f(CWBad identifier after dereferencer\fR" 4.IX Item "Bad identifier after dereferencer"\&\f(CW\*(C`extract_variable\*(C'\fR found a '$', '@', or '%' indicating a variable, but thatcharacter was not followed by a legal Perl identifier..ie n .IP """Did not find expected opening bracket at %s""" 4.el .IP "\f(CWDid not find expected opening bracket at %s\fR" 4.IX Item "Did not find expected opening bracket at %s"\&\f(CW\*(C`extract_codeblock\*(C'\fR failed to find any of the outermost opening bracketsthat were specified..ie n .IP """Improperly nested codeblock at %s""" 4.el .IP "\f(CWImproperly nested codeblock at %s\fR" 4.IX Item "Improperly nested codeblock at %s"A nested code block was found that started with a delimiter that was specifiedas being only to be used as an outermost bracket..ie n .IP """Missing second block for quotelike ""%s""""" 4.el .IP "\f(CWMissing second block for quotelike ``%s''\fR" 4.IX Item "Missing second block for quotelike ""%s"""\&\f(CW\*(C`extract_codeblock\*(C'\fR or \f(CW\*(C`extract_quotelike\*(C'\fR found one of thequotelike operators \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`tr\*(C'\fR or \f(CW\*(C`y\*(C'\fR followed by only one block..ie n .IP """No match found for opening bracket""" 4.el .IP "\f(CWNo match found for opening bracket\fR" 4.IX Item "No match found for opening bracket"\&\f(CW\*(C`extract_codeblock\*(C'\fR failed to find a closing bracket to match the outermostopening bracket..ie n .IP """Did not find opening tag: /%s/""" 4.el .IP "\f(CWDid not find opening tag: /%s/\fR" 4.IX Item "Did not find opening tag: /%s/"\&\f(CW\*(C`extract_tagged\*(C'\fR did not find a suitable opening tag (after any specifiedprefix was removed)..ie n .IP """Unable to construct closing tag to match: /%s/""" 4.el .IP "\f(CWUnable to construct closing tag to match: /%s/\fR" 4.IX Item "Unable to construct closing tag to match: /%s/"\&\f(CW\*(C`extract_tagged\*(C'\fR matched the specified opening tag and tried tomodify the matched text to produce a matching closing tag (becausenone was specified). It failed to generate the closing tag, almostcertainly because the opening tag did not start with abracket of some kind..ie n .IP """Found invalid nested tag: %s""" 4.el .IP "\f(CWFound invalid nested tag: %s\fR" 4.IX Item "Found invalid nested tag: %s"\&\f(CW\*(C`extract_tagged\*(C'\fR found a nested tag that appeared in the \*(L"reject\*(R" list(and the failure mode was not \*(L"\s-1MAX\s0\*(R" or \*(L"\s-1PARA\s0\*(R")..ie n .IP """Found unbalanced nested tag: %s""" 4.el .IP "\f(CWFound unbalanced nested tag: %s\fR" 4.IX Item "Found unbalanced nested tag: %s"\&\f(CW\*(C`extract_tagged\*(C'\fR found a nested opening tag that was not matched by acorresponding nested closing tag (and the failure mode was not \*(L"\s-1MAX\s0\*(R" or \*(L"\s-1PARA\s0\*(R")..ie n .IP """Did not find closing tag""" 4.el .IP "\f(CWDid not find closing tag\fR" 4.IX Item "Did not find closing tag"\&\f(CW\*(C`extract_tagged\*(C'\fR reached the end of the text without finding a closing tagto match the original opening tag (and the failure mode was not\&\*(L"\s-1MAX\s0\*(R" or \*(L"\s-1PARA\s0\*(R")..SH "AUTHOR".IX Header "AUTHOR"Damian Conway (damian@conway.org).SH "BUGS AND IRRITATIONS".IX Header "BUGS AND IRRITATIONS"There are undoubtedly serious bugs lurking somewhere in this code, ifonly because parts of it give the impression of understanding a great dealmore about Perl than they really do..PPBug reports and other feedback are most welcome..SH "COPYRIGHT".IX Header "COPYRIGHT".Vb 3\& Copyright (c) 1997\-2001, Damian Conway. All Rights Reserved.\& This module is free software. It may be used, redistributed\& and/or modified under the same terms as Perl itself..Ve
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?