' ); would correctly match something like this: $text = ''; See also: C<"extract_quotelike"> and C<"extract_codeblock">. =head2 C C extracts any valid Perl variable or variable-involved expression, including scalars, arrays, hashes, array accesses, hash look-ups, method calls through objects, subroutine calls through subroutine references, etc. The subroutine takes up to two optional arguments: =over 4 =item 1. A string to be processed (C<$_> if the string is omitted or C) =item 2. A string specifying a pattern to be matched as a prefix (which is to be skipped). If omitted, optional whitespace is skipped. =back On success in a list context, an array of 3 elements is returned. The elements are: =over 4 =item [0] the extracted variable, or variablish expression =item [1] the remainder of the input text, =item [2] the prefix substring (if any), =back On failure, all of these values (except the remaining text) are C. In a scalar context, C returns just the complete substring that matched a variablish expression. C is returned on failure. In addition, the original input text has the returned substring (and any prefix) removed from it. In a void context, the input text just has the matched substring (and any specified prefix) removed. =head2 C C extracts and segments text between (balanced) specified tags. The subroutine takes up to five optional arguments: =over 4 =item 1. A string to be processed (C<$_> if the string is omitted or C) =item 2. A string specifying a pattern to be matched as the opening tag. If the pattern string is omitted (or C) then a pattern that matches any standard XML tag is used. =item 3. A string specifying a pattern to be matched at the closing tag. If the pattern string is omitted (or C) then the closing tag is constructed by inserting a C> after any leading bracket characters in the actual opening tag that was matched (I the pattern that matched the tag). For example, if the opening tag pattern is specified as C<'{{\w+}}'> and actually matched the opening tag C<"{{DATA}}">, then the constructed closing tag would be C<"{{/DATA}}">. =item 4. A string specifying a pattern to be matched as a prefix (which is to be skipped). If omitted, optional whitespace is skipped. =item 5. A hash reference containing various parsing options (see below) =back The various options that can be specified are: =over 4 =item C $listref> The list reference contains one or more strings specifying patterns that must I appear within the tagged text. For example, to extract an HTML link (which should not contain nested links) use: extract_tagged($text, '', '', undef, {reject => ['']} ); =item C $listref> The list reference contains one or more strings specifying patterns that are I be be treated as nested tags within the tagged text (even if they would match the start tag pattern). For example, to extract an arbitrary XML tag, but ignore "empty" elements: extract_tagged($text, undef, undef, undef, {ignore => ['<[^>]*/>']} ); (also see L<"gen_delimited_pat"> below). =item C $str> The C option indicates the action to be taken if a matching end tag is not encountered (i.e. before the end of the string or some C pattern matches). By default, a failure to match a closing tag causes C to immediately fail. However, if the string value associated with is "MAX", then C returns the complete text up to the point of failure. If the string is "PARA", C returns only the first paragraph after the tag (up to the first line that is either empty or contains only whitespace characters). If the string is "", the the default behaviour (i.e. failure) is reinstated. For example, suppose the start tag "/para" introduces a paragraph, which then continues until the next "/endpara" tag or until another "/para" tag is encountered: $text = "/para line 1\n\nline 3\n/para line 4"; extract_tagged($text, '/para', '/endpara', undef, {reject => '/para', fail => MAX ); # EXTRACTED: "/para line 1\n\nline 3\n" Suppose instead, that if no matching "/endpara" tag is found, the "/para" tag refers only to the immediately following paragraph: $text = "/para line 1\n\nline 3\n/para line 4"; extract_tagged($text, '/para', '/endpara', undef, {reject => '/para', fail => MAX ); # EXTRACTED: "/para line 1\n" Note that the specified C behaviour applies to nested tags as well. =back On success in a list context, an array of 6 elements is returned. The elements are: =over 4 =item [0] the extracted tagged substring (including the outermost tags), =item [1] the remainder of the input text, =item [2] the prefix substring (if any), =item [3] the opening tag =item [4] the text between the opening and closing tags =item [5] the closing tag (or "" if no closing tag was found) =back On failure, all of these values (except the remaining text) are C. In a scalar context, C returns just the complete substring that matched a tagged text (including the start and end tags). C is returned on failure. In addition, the original input text has the returned substring (and any prefix) removed from it. In a void context, the input text just has the matched substring (and any specified prefix) removed. =head2 C (Note: This subroutine is only available under Perl5.005) C generates a new anonymous subroutine which extracts text between (balanced) specified tags. In other words, it generates a function identical in function to C. The difference between C and the anonymous subroutines generated by C, is that those generated subroutines: =over 4 =item * do not have to reparse tag specification or parsing options every time they are called (whereas C has to effectively rebuild its tag parser on every call); =item * make use of the new qr// construct to pre-compile the regexes they use (whereas C uses standard string variable interpolation to create tag-matching patterns). =back The subroutine takes up to four optional arguments (the same set as C except for the string to be processed). It returns a reference to a subroutine which in turn takes a single argument (the text to be extracted from). In other words, the implementation of C is exactly equivalent to: sub extract_tagged { my $text = shift; $extractor = gen_extract_tagged(@_); return $extractor->($text); } (although C is not currently implemented that way, in order to preserve pre-5.005 compatibility). Using C to create extraction functions for specific tags is a good idea if those functions are going to be called more than once, since their performance is typically twice as good as the more general-purpose C. =head2 C C attempts to recognize, extract, and segment any one of the various Perl quotes and quotelike operators (see L) Nested backslashed delimiters, embedded balanced bracket delimiters (for the quotelike operators), and trailing modifiers are all caught. For example, in: extract_quotelike 'q # an octothorpe: \# (not the end of the q!) #' extract_quotelike ' "You said, \"Use sed\"." ' extract_quotelike ' s{([A-Z]{1,8}\.[A-Z]{3})} /\L$1\E/; ' extract_quotelike ' tr/\\\/\\\\/\\\//ds; ' the full Perl quotelike operations are all extracted correctly. Note too that, when using the /x modifier on a regex, any comment containing the current pattern delimiter will cause the regex to be immediately terminated. In other words: 'm / (?i) # CASE INSENSITIVE [a-z_] # LEADING ALPHABETIC/UNDERSCORE [a-z0-9]* # FOLLOWED BY ANY NUMBER OF ALPHANUMERICS /x' will be extracted as if it were: 'm / (?i) # CASE INSENSITIVE [a-z_] # LEADING ALPHABETIC/' This behaviour is identical to that of the actual compiler. C takes two arguments: the text to be processed and a prefix to be matched at the very beginning of the text. If no prefix is specified, optional whitespace is the default. If no text is given, C<$_> is used. In a list context, an array of 11 elements is returned. The elements are: =over 4 =item [0] the extracted quotelike substring (including trailing modifiers), =item [1] the remainder of the input text, =item [2] the prefix substring (if any), =item [3] the name of the quotelike operator (if any), =item [4] the left delimiter of the first block of the operation, =item [5] the text of the first block of the operation (that is, the contents of a quote, the regex of a match or substitution or the target list of a translation), =item [6] the right delimiter of the first block of the operation, =item [7] the left delimiter of the second block of the operation (that is, if it is a C, C, or C), =item [8] the text of the second block of the operation (that is, the replacement of a substitution or the translation list of a translation), =item [9] the right delimiter of the second block of the operation (if any), =item [10] the trailing modifiers on the operation (if any). =back For each of the fields marked "(if any)" the default value on success is an empty string. On failure, all of these values (except the remaining text) are C. In a scalar context, C returns just the complete substring that matched a quotelike operation (or C on failure). In a scalar or void context, the input text has the same substring (and any specified prefix) removed. Examples: # Remove the first quotelike literal that appears in text $quotelike = extract_quotelike($text,'.*?'); # Replace one or more leading whitespace-separated quotelike # literals in $_ with "" do { $_ = join '', (extract_quotelike)[2,1] } until $@; # Isolate the search pattern in a quotelike operation from $text ($op,$pat) = (extract_quotelike $text)[3,5]; if ($op =~ /[ms]/) { print "search pattern: $pat\n"; } else { print "$op is not a pattern matching operation\n"; } =head2 C and "here documents" C can successfully extract "here documents" from an input string, but with an important caveat in list contexts. Unlike other types of quote-like literals, a here document is rarely a contiguous substring. For example, a typical piece of code using here document might look like this: <<'EOMSG' || die; This is the message. EOMSG exit; Given this as an input string in a scalar context, C would correctly return the string "<<'EOMSG'\nThis is the message.\nEOMSG", leaving the string " || die;\nexit;" in the original variable. In other words, the two separate pieces of the here document are successfully extracted and concatenated. In a list context, C would return the list =over 4 =item [0] "<<'EOMSG'\nThis is the message.\nEOMSG\n" (i.e. the full extracted here document, including fore and aft delimiters), =item [1] " || die;\nexit;" (i.e. the remainder of the input text, concatenated), =item [2] "" (i.e. the prefix substring -- trivial in this case), =item [3] "<<" (i.e. the "name" of the quotelike operator) =item [4] "'EOMSG'" (i.e. the left delimiter of the here document, including any quotes), =item [5] "This is the message.\n" (i.e. the text of the here document), =item [6] "EOMSG" (i.e. the right delimiter of the here document), =item [7..10] "" (a here document has no second left delimiter, second text, second right delimiter, or trailing modifiers). =back However, the matching position of the input variable would be set to "exit;" (i.e. I the closing delimiter of the here document), which would cause the earlier " || die;\nexit;" to be skipped in any sequence of code fragment extractions. To avoid this problem, when it encounters a here document whilst extracting from a modifiable string, C silently rearranges the string to an equivalent piece of Perl: <<'EOMSG' This is the message. EOMSG || die; exit; in which the here document I contiguous. It still leaves the matching position after the here document, but now the rest of the line on which the here document starts is not skipped. To prevent from mucking about with the input in this way (this is the only case where a list-context C does so), you can pass the input variable as an interpolated literal: $quotelike = extract_quotelike("$var"); =head2 C C attempts to recognize and extract a balanced bracket delimited substring that may contain unbalanced brackets inside Perl quotes or quotelike operations. That is, C is like a combination of C<"extract_bracketed"> and C<"extract_quotelike">. C takes the same initial three parameters as C: a text to process, a set of delimiter brackets to look for, and a prefix to match first. It also takes an optional fourth parameter, which allows the outermost delimiter brackets to be specified separately (see below). Omitting the first argument (input text) means process C<$_> instead. Omitting the second argument (delimiter brackets) indicates that only C<'{'> is to be used. Omitting the third argument (prefix argument) implies optional whitespace at the start. Omitting the fourth argument (outermost delimiter brackets) indicates that the value of the second argument is to be used for the outermost delimiters. Once the prefix an dthe outermost opening delimiter bracket have been recognized, code blocks are extracted by stepping through the input text and trying the following alternatives in sequence: =over 4 =item 1. Try and match a closing delimiter bracket. If the bracket was the same species as the last opening bracket, return the substring to that point. If the bracket was mismatched, return an error. =item 2. Try to match a quote or quotelike operator. If found, call C to eat it. If C fails, return the error it returned. Otherwise go back to step 1. =item 3. Try to match an opening delimiter bracket. If found, call C recursively to eat the embedded block. If the recursive call fails, return an error. Otherwise, go back to step 1. =item 4. Unconditionally match a bareword or any other single character, and then go back to step 1. =back Examples: # Find a while loop in the text if ($text =~ s/.*?while\s*\{/{/) { $loop = "while " . extract_codeblock($text); } # Remove the first round-bracketed list (which may include # round- or curly-bracketed code blocks or quotelike operators) extract_codeblock $text, "(){}", '[^(]*'; The ability to specify a different outermost delimiter bracket is useful in some circumstances. For example, in the Parse::RecDescent module, parser actions which are to be performed only on a successful parse are specified using a Cdefer:...E> directive. For example: sentence: subject verb object Parse::RecDescent uses CE')> to extract the code within the Cdefer:...E> directive, but there's a problem. A deferred action like this: 10) {$count--}} > will be incorrectly parsed as: because the "less than" operator is interpreted as a closing delimiter. But, by extracting the directive using SE')>> the '>' character is only treated as a delimited at the outermost level of the code block, so the directive is parsed correctly. =head2 C The C subroutine takes a string to be processed and a list of extractors (subroutines or regular expressions) to apply to that string. In an array context C returns an array of substrings of the original string, as extracted by the specified extractors. In a scalar context, C returns the first substring successfully extracted from the original string. In both scalar and void contexts the original string has the first successfully extracted substring removed from it. In all contexts C starts at the current C of the string, and sets that C appropriately after it matches. Hence, the aim of of a call to C in a list context is to split the processed string into as many non-overlapping fields as possible, by repeatedly applying each of the specified extractors to the remainder of the string. Thus C is a generalized form of Perl's C subroutine. The subroutine takes up to four optional arguments: =over 4 =item 1. A string to be processed (C<$_> if the string is omitted or C) =item 2. A reference to a list of subroutine references and/or qr// objects and/or literal strings and/or hash references, specifying the extractors to be used to split the string. If this argument is omitted (or C) the list: [ sub { extract_variable($_[0], '') }, sub { extract_quotelike($_[0],'') }, sub { extract_codeblock($_[0],'{}','') }, ] is used. =item 3. An number specifying the maximum number of fields to return. If this argument is omitted (or C), split continues as long as possible. If the third argument is I, then extraction continues until I fields have been successfully extracted, or until the string has been completely processed. Note that in scalar and void contexts the value of this argument is automatically reset to 1 (under C<-w>, a warning is issued if the argument has to be reset). =item 4. A value indicating whether unmatched substrings (see below) within the text should be skipped or returned as fields. If the value is true, such substrings are skipped. Otherwise, they are returned. =back The extraction process works by applying each extractor in sequence to the text string. If the extractor is a subroutine it is called in a list context and is expected to return a list of a single element, namely the extracted text. It may optionally also return two further arguments: a string representing the text left after extraction (like $' for a pattern match), and a string representing any prefix skipped before the extraction (like $` in a pattern match). Note that this is designed to facilitate the use of other Text::Balanced subroutines with C. Note too that the value returned by an extractor subroutine need not bear any relationship to the corresponding substring of the original text (see examples below). If the extractor is a precompiled regular expression or a string, it is matched against the text in a scalar context with a leading '\G' and the gc modifiers enabled. The extracted value is either $1 if that variable is defined after the match, or else the complete match (i.e. $&). If the extractor is a hash reference, it must contain exactly one element. The value of that element is one of the above extractor types (subroutine reference, regular expression, or string). The key of that element is the name of a class into which the successful return value of the extractor will be blessed. If an extractor returns a defined value, that value is immediately treated as the next extracted field and pushed onto the list of fields. If the extractor was specified in a hash reference, the field is also blessed into the appropriate class, If 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 is assumed to have failed to extract. If none of the extractor subroutines succeeds, then one character is extracted from the start of the text and the extraction subroutines reapplied. Characters which are thus removed are accumulated and eventually become the next field (unless the fourth argument is true, in which case they are discarded). For example, the following extracts substrings that are valid Perl variables: @fields = extract_multiple($text, [ sub { extract_variable($_[0]) } ], undef, 1); This example separates a text into fields which are quote delimited, curly bracketed, and anything else. The delimited and bracketed parts are also blessed to identify them (the "anything else" is unblessed): @fields = extract_multiple($text, [ { Delim => sub { extract_delimited($_[0],q{'"}) } }, { Brack => sub { extract_bracketed($_[0],'{}') } }, ]); This call extracts the next single substring that is a valid Perl quotelike operator (and removes it from $text): $quotelike = extract_multiple($text, [ sub { extract_quotelike($_[0]) }, ], undef, 1); Finally, here is yet another way to do comma-separated value parsing: @fields = extract_multiple($csv_text, [ sub { extract_delimited($_[0],q{'"}) }, qr/([^,]+)(.*)/, ], undef,1); The list in the second argument means: I<"Try and extract a ' or " delimited string, otherwise extract anything up to a comma...">. The undef third argument means: I<"...as many times as possible...">, and the true value in the fourth argument means I<"...discarding anything else that appears (i.e. the commas)">. If you wanted the commas preserved as separate fields (i.e. like split does if your split pattern has capturing parentheses), you would just make the last parameter undefined (or remove it). =head2 C The C subroutine takes a single (string) argument and > builds a Friedl-style optimized regex that matches a string delimited by any one of the characters in the single argument. For example: gen_delimited_pat(q{'"}) returns the regex: (?:\"(?:\\\"|(?!\").)*\"|\'(?:\\\'|(?!\').)*\') Note that the specified delimiters are automatically quotemeta'd. A typical use of C would be to build special purpose tags for C. For example, to properly ignore "empty" XML elements (which might contain quoted strings): my $empty_tag = '<(' . gen_delimited_pat(q{'"}) . '|.)+/>'; extract_tagged($text, undef, undef, undef, {ignore => [$empty_tag]} ); C may also be called with an optional second argument, which specifies the "escape" character(s) to be used for each delimiter. For example to match a Pascal-style string (where ' is the delimiter and '' is a literal ' within the string): gen_delimited_pat(q{'},q{'}); Different escape characters can be specified for different delimiters. For example, to specify that '/' is the escape for single quotes and '%' is the escape for double quotes: gen_delimited_pat(q{'"},q{/%}); If more delimiters than escape chars are specified, the last escape char is used for the remaining delimiters. If no escape char is specified for a given specified delimiter, '\' is used. =head2 C Note that C was previously called C. That name may still be used, but is now deprecated. =head1 DIAGNOSTICS In a list context, all the functions return C<(undef,$original_text)> on failure. In a scalar context, failure is indicated by returning C (in this case the input text is not modified in any way). In addition, on failure in I context, the C<$@> variable is set. Accessing C<$@-E{error}> returns one of the error diagnostics listed below. Accessing C<$@-E{pos}> returns the offset into the original string at which the error was detected (although not necessarily where it occurred!) Printing C<$@> directly produces the error message, with the offset appended. On success, the C<$@> variable is guaranteed to be C. The available diagnostics are: =over 4 =item C The delimiter provided to C was not one of C<'()[]EE{}'>. =item C A non-optional prefix was specified but wasn't found at the start of the text. =item C C or C was expecting a particular kind of bracket at the start of the text, and didn't find it. =item C C didn't find one of the quotelike operators C, C, C, C, C, C or C at the start of the substring it was extracting. =item C C, C or C encountered a closing bracket where none was expected. =item C C, C or C ran out of characters in the text before closing one or more levels of nested brackets. =item C C attempted to match an embedded quoted substring, but failed to find a closing quote to match it. =item C C was unable to find a closing delimiter to match the one that opened the quote-like operation. =item C C, C or C found a valid bracket delimiter, but it was the wrong species. This usually indicates a nesting error, but may indicate incorrect quoting or escaping. =item C C or C found one of the quotelike operators C, C, C, C, C, C or C without a suitable block after it. =item C C was expecting one of '$', '@', or '%' at the start of a variable, but didn't find any of them. =item C C found a '$', '@', or '%' indicating a variable, but that character was not followed by a legal Perl identifier. =item C C failed to find any of the outermost opening brackets that were specified. =item C A nested code block was found that started with a delimiter that was specified as being only to be used as an outermost bracket. =item C C or C found one of the quotelike operators C, C or C followed by only one block. =item C C failed to find a closing bracket to match the outermost opening bracket. =item C C did not find a suitable opening tag (after any specified prefix was removed). =item C C matched the specified opening tag and tried to modify the matched text to produce a matching closing tag (because none was specified). It failed to generate the closing tag, almost certainly because the opening tag did not start with a bracket of some kind. =item C C found a nested tag that appeared in the "reject" list (and the failure mode was not "MAX" or "PARA"). =item C C found a nested opening tag that was not matched by a corresponding nested closing tag (and the failure mode was not "MAX" or "PARA"). =item C C reached the end of the text without finding a closing tag to match the original opening tag (and the failure mode was not "MAX" or "PARA"). =back =head1 AUTHOR Damian Conway (damian@conway.org) =head1 BUGS AND IRRITATIONS There are undoubtedly serious bugs lurking somewhere in this code, if only because parts of it give the impression of understanding a great deal more about Perl than they really do. Bug reports and other feedback are most welcome. =head1 COPYRIGHT Copyright 1997 - 2001 Damian Conway. All Rights Reserved. Some (minor) parts copyright 2009 Adam Kennedy. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. =cut Simpan
, C, C, C, C, C or C at the start of the substring it was extracting. =item C C, C or C encountered a closing bracket where none was expected. =item C C, C or C ran out of characters in the text before closing one or more levels of nested brackets. =item C C attempted to match an embedded quoted substring, but failed to find a closing quote to match it. =item C C was unable to find a closing delimiter to match the one that opened the quote-like operation. =item C C, C or C found a valid bracket delimiter, but it was the wrong species. This usually indicates a nesting error, but may indicate incorrect quoting or escaping. =item C C or C found one of the quotelike operators C, C, C, C, C, C or C without a suitable block after it. =item C C was expecting one of '$', '@', or '%' at the start of a variable, but didn't find any of them. =item C C found a '$', '@', or '%' indicating a variable, but that character was not followed by a legal Perl identifier. =item C C failed to find any of the outermost opening brackets that were specified. =item C A nested code block was found that started with a delimiter that was specified as being only to be used as an outermost bracket. =item C C or C found one of the quotelike operators C, C or C followed by only one block. =item C C failed to find a closing bracket to match the outermost opening bracket. =item C C did not find a suitable opening tag (after any specified prefix was removed). =item C C matched the specified opening tag and tried to modify the matched text to produce a matching closing tag (because none was specified). It failed to generate the closing tag, almost certainly because the opening tag did not start with a bracket of some kind. =item C C found a nested tag that appeared in the "reject" list (and the failure mode was not "MAX" or "PARA"). =item C C found a nested opening tag that was not matched by a corresponding nested closing tag (and the failure mode was not "MAX" or "PARA"). =item C C reached the end of the text without finding a closing tag to match the original opening tag (and the failure mode was not "MAX" or "PARA"). =back =head1 AUTHOR Damian Conway (damian@conway.org) =head1 BUGS AND IRRITATIONS There are undoubtedly serious bugs lurking somewhere in this code, if only because parts of it give the impression of understanding a great deal more about Perl than they really do. Bug reports and other feedback are most welcome. =head1 COPYRIGHT Copyright 1997 - 2001 Damian Conway. All Rights Reserved. Some (minor) parts copyright 2009 Adam Kennedy. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. =cut Simpan
, C, C, C, C, C or C without a suitable block after it. =item C C was expecting one of '$', '@', or '%' at the start of a variable, but didn't find any of them. =item C C found a '$', '@', or '%' indicating a variable, but that character was not followed by a legal Perl identifier. =item C C failed to find any of the outermost opening brackets that were specified. =item C A nested code block was found that started with a delimiter that was specified as being only to be used as an outermost bracket. =item C C or C found one of the quotelike operators C, C or C followed by only one block. =item C C failed to find a closing bracket to match the outermost opening bracket. =item C C did not find a suitable opening tag (after any specified prefix was removed). =item C C matched the specified opening tag and tried to modify the matched text to produce a matching closing tag (because none was specified). It failed to generate the closing tag, almost certainly because the opening tag did not start with a bracket of some kind. =item C C found a nested tag that appeared in the "reject" list (and the failure mode was not "MAX" or "PARA"). =item C C found a nested opening tag that was not matched by a corresponding nested closing tag (and the failure mode was not "MAX" or "PARA"). =item C C reached the end of the text without finding a closing tag to match the original opening tag (and the failure mode was not "MAX" or "PARA"). =back =head1 AUTHOR Damian Conway (damian@conway.org) =head1 BUGS AND IRRITATIONS There are undoubtedly serious bugs lurking somewhere in this code, if only because parts of it give the impression of understanding a great deal more about Perl than they really do. Bug reports and other feedback are most welcome. =head1 COPYRIGHT Copyright 1997 - 2001 Damian Conway. All Rights Reserved. Some (minor) parts copyright 2009 Adam Kennedy. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. =cut Simpan