MAN page from Fedora 30 perl-Text-Balanced-2.03-415.module_2561+625a90a5.noarch.rpm


Section: Perl Programmers Reference Guide (3pm)
Updated: 2018-03-01


Text::Balanced - Extract delimited text sequences from strings. 


 use Text::Balanced qw (                        extract_delimited                        extract_bracketed                        extract_quotelike                        extract_codeblock                        extract_variable                        extract_tagged                        extract_multiple                        gen_delimited_pat                        gen_extract_tagged                       ); # Extract the initial substring of $text that is delimited by # two (unescaped) instances of the first character in $delim.        ($extracted, $remainder) = extract_delimited($text,$delim); # Extract the initial substring of $text that is bracketed # with a delimiter(s) specified by $delim (where the string # in $delim contains one or more of '(){}[]<>').        ($extracted, $remainder) = extract_bracketed($text,$delim); # Extract the initial substring of $text that is bounded by # an XML tag.        ($extracted, $remainder) = extract_tagged($text); # Extract the initial substring of $text that is bounded by # a C<BEGIN>...C<END> pair. Don't allow nested C<BEGIN> tags        ($extracted, $remainder) =                extract_tagged($text,"BEGIN","END",undef,{bad=>["BEGIN"]}); # Extract the initial substring of $text that represents a # Perl "quote or quote-like operation"        ($extracted, $remainder) = extract_quotelike($text); # Extract the initial substring of $text that represents a block # of Perl code, bracketed by any of character(s) specified by $delim # (where the string $delim contains one or more of '(){}[]<>').        ($extracted, $remainder) = extract_codeblock($text,$delim); # Extract the initial substrings of $text that would be extracted by # one or more sequential applications of the specified functions # or regular expressions        @extracted = extract_multiple($text,                                      [ \&extract_bracketed,                                        \&extract_quotelike,                                        \&some_other_extractor_sub,                                        qr/[xyz]*/,                                        'literal',                                      ]);

# Create a string representing an optimized pattern (a la Friedl)# that matches a substring delimited by any of the specified characters# (in this case: any type of quote or a slash)

        $patstring = gen_delimited_pat(q{'"`/});

# Generate a reference to an anonymous sub that is just like extract_tagged# but pre-compiled and optimized for a specific pair of tags, and consequently# much faster (i.e. 3 times faster). It uses qr// for better performance on# repeated calls, so it only works under Perl 5.005 or later.

        $extract_head = gen_extract_tagged('<HEAD>','</HEAD>');        ($extracted, $remainder) = $extract_head->($text);


The various "extract_..." subroutines may be used toextract a delimited substring, possibly after skipping aspecified prefix string. By default, that prefix isoptional whitespace ("/\s*/"), but you can change it to whateveryou wish (see below).

The substring to be extracted must appear at thecurrent "pos" location of the string's variable(or at index zero, if no "pos" position is defined).In other words, the "extract_..." subroutines don'textract the first occurrence of a substring anywherein a string (like an unanchored regex would). Rather,they extract an occurrence of the substring appearingimmediately at the current matching position in thestring (like a "\G"-anchored regex would). 

General behaviour in list contexts

In a list context, all the subroutines return a list, the first threeelements of which are always:
The extracted string, including the specified delimiters.If the extraction fails "undef" is returned.
The remainder of the input string (i.e. the characters after theextracted string). On failure, the entire string is returned.
The skipped prefix (i.e. the characters before the extracted string).On failure, "undef" is returned.

Note that in a list context, the contents of the original input text (the firstargument) are not modified in any way.

However, if the input text was passed in a variable, that variable's"pos" value is updated to point at the first character after theextracted text. That means that in a list context the varioussubroutines can be used much like regular expressions. For example:

        while ( $next = (extract_quotelike($text))[0] )        {                # process next quote-like (in $next)        }

General behaviour in scalar and void contexts

In a scalar context, the extracted string is returned, having first beenremoved from the input text. Thus, the following code also processeseach quote-like operation, but actually removes them from $text:

        while ( $next = extract_quotelike($text) )        {                # process next quote-like (in $next)        }

Note that if the input text is a read-only string (i.e. a literal),no attempt is made to remove the extracted text.

In a void context the behaviour of the extraction subroutines isexactly the same as in a scalar context, except (of course) that theextracted substring is not returned. 

A note about prefixes

Prefix patterns are matched without any trailing modifiers ("/gimsox" etc.)This can bite you if you're expecting a prefix specification like'.*?(?=<H1>)' to skip everything up to the first <H1> tag. Such a prefixpattern will only succeed if the <H1> tag is on the current line, since. normally doesn't match newlines.

To overcome this limitation, you need to turn on /s matching withinthe prefix pattern, using the "(?s)" directive: '(?s).*?(?=<H1>)' 


The "extract_delimited" function formalizes the common idiomof extracting a single-character-delimited substring from the start ofa string. For example, to extract a single-quote delimited string, thefollowing code is typically used:

        ($remainder = $text) =~ s/\A('(\\.|[^'])*')//s;        $extracted = $1;

but with "extract_delimited" it can be simplified to:

        ($extracted,$remainder) = extract_delimited($text, "'");

"extract_delimited" takes up to four scalars (the input text, thedelimiters, a prefix pattern to be skipped, and any escape characters)and extracts the initial substring of the text thatis appropriately delimited. If the delimiter string has multiplecharacters, the first one encountered in the text is taken to delimitthe substring.The third argument specifies a prefix pattern that is to be skipped(but must be present!) before the substring is extracted.The final argument specifies the escape character to be used for eachdelimiter.

All arguments are optional. If the escape characters are not specified,every delimiter is escaped with a backslash ("\").If the prefix is not specified, thepattern '\s*' - optional whitespace - is used. If the delimiter setis also not specified, the set "/["'`]/" is used. If the text to be processedis not specified either, $_ is used.

In list context, "extract_delimited" returns a array of threeelements, the extracted substring (including the surroundingdelimiters), the remainder of the text, and the skipped prefix (ifany). If a suitable delimited substring is not found, the firstelement of the array is the empty string, the second is the completeoriginal text, and the prefix returned in the third element is anempty string.

In a scalar context, just the extracted substring is returned. Ina void context, the extracted substring (and any prefix) are simplyremoved from the beginning of the first argument.


        # Remove a single-quoted substring from the very beginning of $text:                $substring = extract_delimited($text, "'", '');        # Remove a single-quoted Pascalish substring (i.e. one in which        # doubling the quote character escapes it) from the very        # beginning of $text:                $substring = extract_delimited($text, "'", '', "'");        # Extract a single- or double- quoted substring from the        # beginning of $text, optionally after some whitespace        # (note the list context to protect $text from modification):                ($substring) = extract_delimited $text, q{"'};        # Delete the substring delimited by the first '/' in $text:                $text = join '', (extract_delimited($text,'/','[^/]*')[2,1];

Note that this last example is not the same as deleting the firstquote-like pattern. For instance, if $text contained the string:

        "if ('./cmd' =~ m/$UNIXCMD/s) { $cmd = $1; }"

then after the deletion it would contain:

        "if ('.$UNIXCMD/s) { $cmd = $1; }"


        "if ('./cmd' =~ ms) { $cmd = $1; }"

See ``extract_quotelike'' for a (partial) solution to this problem. 


Like "extract_delimited", the "extract_bracketed" function takesup to three optional scalar arguments: a string to extract from, a delimiterspecifier, and a prefix pattern. As before, a missing prefix defaults tooptional whitespace and a missing text defaults to $_. However, a missingdelimiter specifier defaults to '{}()[]<>' (see below).

"extract_bracketed" extracts a balanced-bracket-delimitedsubstring (using any one (or more) of the user-specified delimiterbrackets: '(..)', '{..}', '[..]', or '<..>'). Optionally it will alsorespect quoted unbalanced brackets (see below).

A ``delimiter bracket'' is a bracket in list of delimiters passed as"extract_bracketed"'s second argument. Delimiter brackets arespecified by giving either the left or right (or both!) versionsof the required bracket(s). Note that the order in whichtwo or more delimiter brackets are specified is not significant.

A ``balanced-bracket-delimited substring'' is a substring bounded bymatched brackets, such that any other (left or right) delimiterbracket within the substring is also matched by an opposite(right or left) delimiter bracket at the same level of nesting. Anytype of bracket not in the delimiter list is treated as an ordinarycharacter.

In other words, each type of bracket specified as a delimiter must bebalanced and correctly nested within the substring, and any other kind of(``non-delimiter'') bracket in the substring is ignored.

For example, given the string:

        $text = "{ an '[irregularly :-(] {} parenthesized >:-)' string }";

then a call to "extract_bracketed" in a list context:

        @result = extract_bracketed( $text, '{}' );

would return:

        ( "{ an '[irregularly :-(] {} parenthesized >:-)' string }" , "" , "" )

since both sets of '{..}' brackets are properly nested and evenly balanced.(In a scalar context just the first element of the array would be returned. Ina void context, $text would be replaced by an empty string.)

Likewise the call in:

        @result = extract_bracketed( $text, '{[' );

would return the same result, since all sets of both types of specifieddelimiter brackets are correctly nested and balanced.

However, the call in:

        @result = extract_bracketed( $text, '{([<' );

would fail, returning:

        ( undef , "{ an '[irregularly :-(] {} parenthesized >:-)' string }"  );

because the embedded pairs of '(..)'s and '[..]'s are ``cross-nested'' andthe embedded '>' is unbalanced. (In a scalar context, this call wouldreturn an empty string. In a void context, $text would be unchanged.)

Note that the embedded single-quotes in the string don't help in thiscase, since they have not been specified as acceptable delimiters and aretherefore treated as non-delimiter characters (and ignored).

However, if a particular species of quote character is included in thedelimiter specification, then that type of quote will be correctly handled.for example, if $text is:

        $text = '<A HREF=">>>>">link</A>';


        @result = extract_bracketed( $text, '<">' );


        ( '<A HREF=">>>>">', 'link</A>', "" )

as expected. Without the specification of """ as an embedded quoter:

        @result = extract_bracketed( $text, '<>' );

the result would be:

        ( '<A HREF=">', '>>>">link</A>', "" )

In addition to the quote delimiters "'", """, and "`", full Perl quote-likequoting (i.e. q{string}, qq{string}, etc) can be specified by including theletter 'q' as a delimiter. Hence:

        @result = extract_bracketed( $text, '<q>' );

would correctly match something like this:

        $text = '<leftop: conj /and/ conj>';

See also: "extract_quotelike" and "extract_codeblock". 


"extract_variable" extracts any valid Perl variable orvariable-involved expression, including scalars, arrays, hashes, arrayaccesses, hash look-ups, method calls through objects, subroutine callsthrough subroutine references, etc.

The subroutine takes up to two optional arguments:

A string to be processed ($_ if the string is omitted or "undef")
A string specifying a pattern to be matched as a prefix (which is to beskipped). If omitted, optional whitespace is skipped.

On success in a list context, an array of 3 elements is returned. Theelements are:

the extracted variable, or variablish expression
the remainder of the input text,
the prefix substring (if any),

On failure, all of these values (except the remaining text) are "undef".

In a scalar context, "extract_variable" returns just the completesubstring that matched a variablish expression. "undef" is returned onfailure. 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 (andany specified prefix) removed. 


"extract_tagged" extracts and segments text between (balanced)specified tags.

The subroutine takes up to five optional arguments:

A string to be processed ($_ if the string is omitted or "undef")
A string specifying a pattern to be matched as the opening tag.If the pattern string is omitted (or "undef") then a patternthat matches any standard XML tag is used.
A string specifying a pattern to be matched at the closing tag. If the pattern string is omitted (or "undef") then the closingtag is constructed by inserting a "/" after any leading bracketcharacters in the actual opening tag that was matched (not the patternthat matched the tag). For example, if the opening tag patternis specified as '{{\w+}}' and actually matched the opening tag "{{DATA}}", then the constructed closing tag would be "{{/DATA}}".
A string specifying a pattern to be matched as a prefix (which is to beskipped). If omitted, optional whitespace is skipped.
A hash reference containing various parsing options (see below)

The various options that can be specified are:

reject => $listref
The list reference contains one or more strings specifying patternsthat must not appear within the tagged text.

For example, to extractan HTML link (which should not contain nested links) use:

        extract_tagged($text, '<A>', '</A>', undef, {reject => ['<A>']} );
ignore => $listref
The list reference contains one or more strings specifying patternsthat are not 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 ``gen_delimited_pat'' below).

fail => $str
The "fail" option indicates the action to be taken if a matching endtag is not encountered (i.e. before the end of the string or some"reject" pattern matches). By default, a failure to match a closingtag causes "extract_tagged" to immediately fail.

However, if the string value associated with <reject> is ``MAX'', then"extract_tagged" returns the complete text up to the point of failure.If the string is ``PARA'', "extract_tagged" returns only the first paragraphafter the tag (up to the first line that is either empty or containsonly 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 thencontinues until the next ``/endpara'' tag or until another ``/para'' tag isencountered:

        $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 "fail" behaviour applies to nested tags as well.

On success in a list context, an array of 6 elements is returned. The elements are:

the extracted tagged substring (including the outermost tags),
the remainder of the input text,
the prefix substring (if any),
the opening tag
the text between the opening and closing tags
the closing tag (or "" if no closing tag was found)

On failure, all of these values (except the remaining text) are "undef".

In a scalar context, "extract_tagged" returns just the completesubstring that matched a tagged text (including the start and endtags). "undef" is returned on failure. In addition, the original inputtext has the returned substring (and any prefix) removed from it.

In a void context, the input text just has the matched substring (andany specified prefix) removed. 


(Note: This subroutine is only available under Perl5.005)

"gen_extract_tagged" generates a new anonymous subroutine whichextracts text between (balanced) specified tags. In other words,it generates a function identical in function to "extract_tagged".

The difference between "extract_tagged" and the anonymoussubroutines generated by"gen_extract_tagged", is that those generated subroutines:

do not have to reparse tag specification or parsing options every timethey are called (whereas "extract_tagged" has to effectively rebuildits tag parser on every call);
make use of the new qr// construct to pre-compile the regexes they use(whereas "extract_tagged" uses standard string variable interpolation to create tag-matching patterns).

The subroutine takes up to four optional arguments (the same set as"extract_tagged" except for the string to be processed). It returnsa reference to a subroutine which in turn takes a single argument (the text tobe extracted from).

In other words, the implementation of "extract_tagged" is exactlyequivalent to:

        sub extract_tagged        {                my $text = shift;                $extractor = gen_extract_tagged(@_);                return $extractor->($text);        }

(although "extract_tagged" is not currently implemented that way, in orderto preserve pre-5.005 compatibility).

Using "gen_extract_tagged" to create extraction functions for specific tags is a good idea if those functions are going to be called more than once, sincetheir performance is typically twice as good as the more general-purpose"extract_tagged". 


"extract_quotelike" attempts to recognize, extract, and segment anyone of the various Perl quotes and quotelike operators (seeperlop(3)) Nested backslashed delimiters, embedded balanced bracketdelimiters (for the quotelike operators), and trailing modifiers areall 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 commentcontaining the current pattern delimiter will cause the regex to beimmediately 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.

"extract_quotelike" takes two arguments: the text to be processed anda 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,$_ is used.

In a list context, an array of 11 elements is returned. The elements are:

the extracted quotelike substring (including trailing modifiers),
the remainder of the input text,
the prefix substring (if any),
the name of the quotelike operator (if any),
the left delimiter of the first block of the operation,
the text of the first block of the operation(that is, the contents ofa quote, the regex of a match or substitution or the target list of atranslation),
the right delimiter of the first block of the operation,
the left delimiter of the second block of the operation(that is, if it is a "s", "tr", or "y"),
the text of the second block of the operation (that is, the replacement of a substitution or the translation listof a translation),
the right delimiter of the second block of the operation (if any),
the trailing modifiers on the operation (if any).

For each of the fields marked ``(if any)'' the default value on success isan empty string.On failure, all of these values (except the remaining text) are "undef".

In a scalar context, "extract_quotelike" returns just the complete substringthat matched a quotelike operation (or "undef" on failure). In a scalar orvoid context, the input text has the same substring (and any specifiedprefix) removed.


        # Remove the first quotelike literal that appears in text                $quotelike = extract_quotelike($text,'.*?');        # Replace one or more leading whitespace-separated quotelike        # literals in $_ with "<QLL>"                do { $_ = join '<QLL>', (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";                }

extract_quotelike and here documents

"extract_quotelike" can successfully extract ``here documents'' from an inputstring, but with an important caveat in list contexts.

Unlike other types of quote-like literals, a here document is rarelya contiguous substring. For example, a typical piece of code usinghere document might look like this:

        <<'EOMSG' || die;        This is the message.        EOMSG        exit;

Given this as an input string in a scalar context, "extract_quotelike"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 andconcatenated.

In a list context, "extract_quotelike" would return the list

``<<'EOMSG'\nThis is the message.\nEOMSG\n'' (i.e. the full extracted here document,including fore and aft delimiters),
`` || die;\nexit;'' (i.e. the remainder of the input text, concatenated),
"" (i.e. the prefix substring --- trivial in this case),
``<<'' (i.e. the ``name'' of the quotelike operator)
``'EOMSG''' (i.e. the left delimiter of the here document, including any quotes),
``This is the message.\n'' (i.e. the text of the here document),
``EOMSG'' (i.e. the right delimiter of the here document),
"" (a here document has no second left delimiter, second text, second rightdelimiter, or trailing modifiers).

However, the matching position of the input variable would be set to``exit;'' (i.e. after the closing delimiter of the here document),which would cause the earlier `` || die;\nexit;'' to be skipped in anysequence of code fragment extractions.

To avoid this problem, when it encounters a here document whilstextracting from a modifiable string, "extract_quotelike" silentlyrearranges the string to an equivalent piece of Perl:

        <<'EOMSG'        This is the message.        EOMSG        || die;        exit;

in which the here document is contiguous. It still leaves thematching position after the here document, but now the rest of the lineon which the here document starts is not skipped.

To prevent <extract_quotelike> from mucking about with the input in this way(this is the only case where a list-context "extract_quotelike" does so),you can pass the input variable as an interpolated literal:

        $quotelike = extract_quotelike("$var");


"extract_codeblock" attempts to recognize and extract a balancedbracket delimited substring that may contain unbalanced bracketsinside Perl quotes or quotelike operations. That is, "extract_codeblock"is like a combination of "extract_bracketed" and"extract_quotelike".

"extract_codeblock" takes the same initial three parameters as "extract_bracketed":a text to process, a set of delimiter brackets to look for, and a prefix tomatch first. It also takes an optional fourth parameter, which allows theoutermost delimiter brackets to be specified separately (see below).

Omitting the first argument (input text) means process $_ instead.Omitting the second argument (delimiter brackets) indicates that only '{' 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 thevalue of the second argument is to be used for the outermost delimiters.

Once the prefix an dthe outermost opening delimiter bracket have beenrecognized, code blocks are extracted by stepping through the input text andtrying the following alternatives in sequence:

Try and match a closing delimiter bracket. If the bracket was the samespecies as the last opening bracket, return the substring to thatpoint. If the bracket was mismatched, return an error.
Try to match a quote or quotelike operator. If found, call"extract_quotelike" to eat it. If "extract_quotelike" fails, returnthe error it returned. Otherwise go back to step 1.
Try to match an opening delimiter bracket. If found, call"extract_codeblock" recursively to eat the embedded block. If therecursive call fails, return an error. Otherwise, go back to step 1.
Unconditionally match a bareword or any other single character, andthen go back to step 1.


        # 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 usefulin some circumstances. For example, in the Parse::RecDescent module,parser actions which are to be performed only on a successful parseare specified using a "<defer:...>" directive. For example:

        sentence: subject verb object                        <defer: {$::theVerb = $item{verb}} >

Parse::RecDescent uses "extract_codeblock($text, '{}<>')" to extract the codewithin the "<defer:...>" directive, but there's a problem.

A deferred action like this:

                        <defer: {if ($count>10) {$count--}} >

will be incorrectly parsed as:

                        <defer: {if ($count>

because the ``less than'' operator is interpreted as a closing delimiter.

But, by extracting the directive using"extract_codeblock($text, '{}', undef, '<>')"the '>' character is only treated as a delimited at the outermostlevel of the code block, so the directive is parsed correctly. 


The "extract_multiple" 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 "extract_multiple" returns an array of substringsof the original string, as extracted by the specified extractors.In a scalar context, "extract_multiple" 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"extract_multiple" starts at the current "pos" of the string, andsets that "pos" appropriately after it matches.

Hence, the aim of of a call to "extract_multiple" 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 "extract_multiple" isa generalized form of Perl's "split" subroutine.

The subroutine takes up to four optional arguments:

A string to be processed ($_ if the string is omitted or "undef")
A 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"undef") the list:

        [                sub { extract_variable($_[0], '') },                sub { extract_quotelike($_[0],'') },                sub { extract_codeblock($_[0],'{}','') },        ]

is used.

An number specifying the maximum number of fields to return. If thisargument is omitted (or "undef"), split continues as long as possible.

If the third argument is N, then extraction continues until N fieldshave 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 "-w", a warning is issued if the argument has to be reset).

A 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.

The extraction process works by applying each extractor insequence to the text string.

If 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"extract_multiple". Note too that the value returned by an extractorsubroutine need not bear any relationship to the corresponding substringof 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 thecomplete match (i.e. $&).

If 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.

If 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,

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 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).

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 bracketedparts 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 quotelikeoperator (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:``Try and extract a ' or '' delimited string, otherwise extract anything up to a comma...".The undef third argument means:`` many times as possible...'',and the true value in the fourth argument means``...discarding anything else that appears (i.e. the commas)''.

If 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). 


The "gen_delimited_pat" subroutine takes a single (string) argument and
   > builds a Friedl-style optimized regex that matches a string delimitedby any one of the characters in the single argument. For example:


returns the regex:


Note that the specified delimiters are automatically quotemeta'd.

A typical use of "gen_delimited_pat" would be to build special purpose tagsfor "extract_tagged". 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]} );

"gen_delimited_pat" 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 delimiterand '' is a literal ' within the string):


Different 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:


If 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, '\' is used. 


Note that "gen_delimited_pat" was previously called "delimited_pat".That name may still be used, but is now deprecated. 


In a list context, all the functions return "(undef,$original_text)"on failure. In a scalar context, failure is indicated by returning "undef"(in this case the input text is not modified in any way).

In addition, on failure in any context, the $@ variable is set.Accessing "$@->{error}" returns one of the error diagnostics listedbelow.Accessing "$@->{pos}" returns the offset into the original string atwhich the error was detected (although not necessarily where it occurred!)Printing $@ directly produces the error message, with the offset appended.On success, the $@ variable is guaranteed to be "undef".

The available diagnostics are:

Did not find a suitable bracket: %s
The delimiter provided to "extract_bracketed" was not one of'()[]<>{}'.
Did not find prefix: /%s/
A non-optional prefix was specified but wasn't found at the start of the text.
Did not find opening bracket after prefix: %s
"extract_bracketed" or "extract_codeblock" was expecting aparticular kind of bracket at the start of the text, and didn't find it.
No quotelike operator found after prefix: %s
"extract_quotelike" didn't find one of the quotelike operators "q","qq", "qw", "qx", "s", "tr" or "y" at the start of the substringit was extracting.
Unmatched closing bracket: %c
"extract_bracketed", "extract_quotelike" or "extract_codeblock" encountereda closing bracket where none was expected.
Unmatched opening bracket(s): %s
"extract_bracketed", "extract_quotelike" or "extract_codeblock" ran out of characters in the text before closing one or more levels of nestedbrackets.
Unmatched embedded quote (%s)
"extract_bracketed" attempted to match an embedded quoted substring, butfailed to find a closing quote to match it.
Did not find closing delimiter to match '%s'
"extract_quotelike" was unable to find a closing delimiter to match theone that opened the quote-like operation.
Mismatched closing bracket: expected %c but found %s
"extract_bracketed", "extract_quotelike" or "extract_codeblock" founda valid bracket delimiter, but it was the wrong species. This usuallyindicates a nesting error, but may indicate incorrect quoting or escaping.
No block delimiter found after quotelike %s
"extract_quotelike" or "extract_codeblock" found one of thequotelike operators "q", "qq", "qw", "qx", "s", "tr" or "y"without a suitable block after it.
Did not find leading dereferencer
"extract_variable" was expecting one of '$', '@', or '%' at the start ofa variable, but didn't find any of them.
Bad identifier after dereferencer
"extract_variable" found a '$', '@', or '%' indicating a variable, but thatcharacter was not followed by a legal Perl identifier.
Did not find expected opening bracket at %s
"extract_codeblock" failed to find any of the outermost opening bracketsthat were specified.
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.
Missing second block for quotelike %s
"extract_codeblock" or "extract_quotelike" found one of thequotelike operators "s", "tr" or "y" followed by only one block.
No match found for opening bracket
"extract_codeblock" failed to find a closing bracket to match the outermostopening bracket.
Did not find opening tag: /%s/
"extract_tagged" did not find a suitable opening tag (after any specifiedprefix was removed).
Unable to construct closing tag to match: /%s/
"extract_tagged" 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.
Found invalid nested tag: %s
"extract_tagged" found a nested tag that appeared in the ``reject'' list(and the failure mode was not ``MAX'' or ``PARA'').
Found unbalanced nested tag: %s
"extract_tagged" found a nested opening tag that was not matched by acorresponding nested closing tag (and the failure mode was not ``MAX'' or ``PARA'').
Did not find closing tag
"extract_tagged" reached the end of the text without finding a closing tagto match the original opening tag (and the failure mode was not``MAX'' or ``PARA'').


Damian Conway ( 


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.

Bug reports and other feedback are most welcome. 


Copyright 1997 - 2001 Damian Conway. All Rights Reserved.

Some (minor) parts copyright 2009 Adam Kennedy.

This module is free software. It may be used, redistributedand/or modified under the same terms as Perl itself.



General behaviour in list contexts
General behaviour in scalar and void contexts
A note about prefixes
extract_quotelike and here documents

This document was created byman2html,using the manual pages.