=head1 NAME

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


=head1 SYNOPSIS

 use Text::Balanced qw (
			extract_delimited
			extract_bracketed
			extract_quotelike
			extract_codeblock
			extract_variable
			extract_tagged
			extract_multiple

			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 HTML/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.

	@extracted = extract_multiple($text,
				      [ \&extract_bracketed,
					\&extract_quotelike,
					\&some_ther_extractor_sub
				      ]);

# 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 = 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);


=head1 DESCRIPTION

The various C<extract_...> subroutines may be used to extract a leading 
delimited string (possibily after skipping a specified prefix string).

In a list context, they all return a list, the first three elements of
which are always: the extracted string (including the specified
delimiters) , the remainder of the input string, the skipped prefix.
Note that the original input text (the first argument) is not modified
in any way.

In a scalar context, the extracted string is returned, having first been
removed from the input text.

In a void context, the input text has the extracted string (and any
specified prefix) removed from it.


=head2 C<extract_delimited>

The C<extract_delimited> function formalizes the common idiom
of extracting a single-character-delimited substring from the start of
a string. For example, to extract a single-quote delimited string, the
following code is typically used:

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

but with C<extract_delimited> it can be simplified to:

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

C<extract_delimited> takes up to three scalars (the input text, the
delimiters, and a prefix to be skipped) and extracts the initial
substring of the text that is appropriately delimited. If the
delimiter string has multiple characters, the first one encountered in
the text is taken to delimit the substring (the delimiters are simply
interpolated into a regex character class, so care is needed with some
pattern metacharacters). The third argument specifies a prefix pattern
that is to be skipped (but must be present!) before the substring is
extracted.

All arguments are optional. If the prefix is not specified, the
pattern C<'\s*'> - optional whitespace - is used. If the delimiter set
is also not specified, the set C</["'`]/> is used. If the text to be processed
is not specified either, C<$_> is used.

In list context, C<extract_delimited> returns a array of three
elements, the extracted substring (I<including the surrounding
delimiters>), the remainder of the text, and the skipped prefix (if
any). If a suitable delimited substring is not found, the first
element of the array is the empty string, the second is the complete
original text, and the prefix returned in the third element is an
empty string.

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

Examples:

	# Remove a single-quoted substring 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 I<not> the same as deleting the first
quote-like pattern. For instance, if C<$text> contained the string:

	"if ('./cmd' =~ m/$UNIXCMD/s) { $cmd = $1; }"
	
then after the deletion it would contain:

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

not:

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

See L<"extract_quotelike"> for a (partial) solution to this problem.


=head2 C<extract_bracketed>

Like C<"extract_delimited">, the C<extract_bracketed> function takes
up to three optional scalar arguments: a string to extract from, a delimiter
specifier, and a prefix pattern. As before, a missing prefix defaults to
optional whitespace and a missing text defaults to C<$_>. However, a missing
delimiter specifier defaults to C<'{}()[]E<lt>E<gt>'> (see below).

C<extract_bracketed> extracts a balanced-bracket-delimited
substring (using any one (or more) of the user-specified delimiter
brackets: '(..)', '{..}', '[..]', or '<..>'). Optionally it will also
respect quoted unbalanced brackets (see below).

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

A "balanced-bracket-delimited substring" is a substring bounded by
matched brackets, such that any other (left or right) delimiter
bracket I<within> the substring is also matched by an opposite
(right or left) delimiter bracket I<at the same level of nesting>. Any
type of bracket not in the delimiter list is treated as an ordinary
character.

In other words, each type of bracket specified as a delimiter must be
balanced 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 C<extract_bracketed> in a list context:

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

would return:

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

since both sets of C<'{..}'> brackets are properly nested and evenly balanced.
(In a scalar context just the first element of the array would be returned. In
a void context, C<$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 specified
delimiter 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 C<'(..)'>s and C<'[..]'>s are "cross-nested" and
the embedded C<'E<gt>'> is unbalanced. (In a scalar context, this call would
return an empty string. In a void context, C<$text> would be unchanged.)

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

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

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

then

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

returns:

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

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

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

the result would be:

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

Note that only C<'>, C<">, and C<`> quoting can be specified with
C<extract_bracketed>. Where embedded Perl quotelike operators (C<q{}>, etc.)
must also be catered for, use C<"extract_quotelike"> and
C<"extract_codeblock"> instead.


=head2 C<extract_tagged>

C<extract_tagged> 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<undef>)

=item 2.

A string specifying a pattern to be matched as the opening tag.
If the pattern string is omitted (or C<undef>) then a pattern
that matches any standard HTML/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<undef>) then the closing
tag is constructed by inserting a C</> after any leading bracket
characters in the actual opening tag that was matched (I<not> 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<reject =E<gt> $listref>

The list reference contains one or more strings specifying patterns
that must I<not> appear within the tagged text.

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

	extract_tagged($text, '<A>', '</A>', undef, {reject => ['<A>']} );

=item C<ignore =E<gt> $listref>

The list reference contains one or more strings specifying patterns
that are I<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 L<"delimited_pat"> below).


=item C<fail =E<gt> $str>

The C<fail> 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<reject> pattern matches). By default, a failure to match a closing
tag causes C<extract_tagged> to immediately fail.

However, if the string value associated with <reject> is "MAX", then
C<extract_tagged> returns the complete text up to the point of failure.
If the string is "PARA", C<extract_tagged> 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<fail> 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<undef>.

In a scalar context, C<extract_tagged> returns just the complete
substring that matched a tagged text (including the start and end
tags). C<undef> 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<gen_extract_tagged>

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

C<gen_extract_tagged> generates a new anonymous subroutine which
extracts and segments text between (balanced) specified tags. In other words,
it generates a function identical in function to C<extract_tagged>.

The difference between C<extract_tagged> and the subroutines generated by
C<gen_extract_tagged>, is that those anonymous subroutines:

=over 4

=item *

are limited to the actual tags specified to C<gen_extract_tagged>
(whereas C<extract_tagged> can automatically select tags as they
are encountered during extraction);

=item * 

do not have to reparse tag specification or parsing options every time
they are called (whereas C<extract_tagged> 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<extract_tagged> uses standard string variable interpolation 
to create tag-matching patterns).

=back

The subroutine takes up to four optional arguments (the same set as
C<extract_tagged> 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<extract_tagged> is exactly
equivalent to:

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

(although C<extract_tagged> is not currently implemented that way, in order
to preserve pre-5.005 compatibility).

Using C<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, since
their performance is typically three times better than the more general-purpose
C<extract_tagged>.


=head2 C<extract_quotelike>

C<extract_quotelike> attempts to recognize, extract, and segment any
one of the various Perl quotes and quotelike operators (see
L<perlop(3)>) 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 Perl 5.004 interpreter.

C<extract_quotelike> 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<s>, C<tr>, or C<y>),

=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<undef>.


In a scalar context, C<extract_quotelike> returns just the complete substring
that matched a quotelike operation (or C<undef> 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 "<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";
		}


=head2 C<extract_codeblock>

C<extract_codeblock> attempts to recognize and extract a balanced
bracket delimited substring that may contain unbalanced brackets
inside Perl quotes or quotelike operations. That is, C<extract_codeblock>
is like a combination of C<"extract_bracketed"> and
C<"extract_quotelike">.

C<extract_codeblock> takes the same three parameters as C<extract_bracketed>:
a text to process, a set of delimiter brackets to look for, and a prefix to
match first. No prefix argument implies optional whitespace at the start, no
delimiter brackets indicates that only C<'{'> is to be used. No input
no text means process C<$_>.

Once the prefix has been disposed of, code blocks are extracted by
stepping through the 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<extract_quotelike> to eat it. If C<extract_quotelike> 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<extract_codeblock> 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, "(){}", '[^(]*';


=head2 C<extract_multiple>

The C<extract_multiple> subroutine takes a string to be processed and a 
list of extraction subroutines to apply to that string. In an array context
C<extract_multiple> returns an array of substrings of the original string,
as extracted by the various extraction subroutines. In a scalar context,
C<extract_multiple> 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.

Hence, the aim of of a call to C<extract_multiple> 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 extractor
subroutines to the remainder of the string. Thus C<extract_multiple> is
a generalized form of Perl's inbuilt C<split> subroutine.

In a scalar and void context, a call to C<extract_multiple> removes the
leading substring which would have been removed by any one of the
specified extraction subroutines.

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

=item 2.

A reference to a list of subroutine references, specifying the extractor
subroutines to be used to split the string. If this argument is omitted (or
C<undef>) 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<undef>), split continues as long as possible.

If the third argument is I<N>, then at most I<N-1> splits will be
performed and the array return value of C<extract_multiple> will
contain at most I<N> elements (where the very last element is always
the unsplit remainder of the original string)

Note that in scalar and void contexts the value of this argument is 
automatically set to 2, unless the fourth argument (see below) is set
to true, in which case the maximum is set to 1..

=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 subroutine in sequence
to the text string. Each subroutine is called in a list context and is expected
to return two elements: the extract substring and the remainder of the original
string. If the subroutine returns an undefined value, it is assumed to have
failed to extract.

If an extractor subroutine returns a defined value, that value is immediately
treated as the next extracted field and pushed onto the list of fields. The
text being processed is then assigned the remainder returned by the successful
extractor function. 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 disgarded).

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:

	@fields = extract_multiple($text,
				   [
					sub { extract_delimited($_[0],q{'"}) },
					sub { extract_bracketed($_[0],'{}') },
				   ]);

This call extracts the next single substring that is a valid Perl quotelike
operator (discarding everything before it):

	$quotelike = extract_multiple($text,
				      [
					sub { extract_quotelike($_[0]) },
				      ]);

This does the same, but also captures the discarded preliminary substring:

	($prelim,$quotelike) = extract_multiple($text,
				      [
					sub { extract_quotelike($_[0]) },
				      ],
				      2);

Putting the maximum count in that last example is important because otherwise
C<extract_multiple> would process the entire string (possibly extracting many
fields) and then discard all but the first two.

Finally, here is yet another way to do comma-separated value parsing:

        @fields = extract_multiple($csv_text,
                                  [
					sub { extract_delimited($_[0],q{'"}) },
                                        sub { $_[0] =~ /\A([^,]+)(.*)/ },
                                  ],
                                  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<delimited_pat>

The C<delimited_pat> 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:

	delimited_pat(q{'"})

returns the regex:

	(?:\"(?:\\\"|(?!\").)*\"|\'(?:\\\'|(?!\').)*\')

Note that the specified delimiters are automatically quotemeta'd.

A typical use of C<delimited_pat> would be to build special purpose tags
for C<extract_tagged>. For example, to properly ignore "empty" XML elements
(which might contain quoted strings):

	my $empty_tag = '<(' . delimited_pat(q{'"}) . '|.)+/>';

	extract_tagged($text, undef, undef, undef, {ignore => [$empty_tag]} );


C<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 delimiter
and '' is a literal ' within the string):

	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:

	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.
	

=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<undef>
(in this case the input text is not modified in any way).

In addition, on failure in I<any> context, one of the following explanatory
diagnostic messages is returned in the standard C<$@> variable (on success the
C<$@> variable is guaranteed to be C<undef>):

=over 4

=item  C<Did not find a suitable bracket: "%s">

The delimiter provided to C<extract_bracketed> was not one of
C<'()[]E<lt>E<gt>{}'>.

=item  C<Did not find prefix: /%s/>

A non-optional prefix was specified but wasn't found at the start of the text.

=item  C<Could not extract "%c"-delimited substring>

C<extract_delimited>, C<extract_quotelike> or C<extract_codeblock>
couldn't find an initial substring (after the prefix) that was
delimited by the delimiter(s) specified.

=item  C<Did not find opening bracket after prefix: "%s">

C<extract_bracketed> or C<extract_codeblock> was expecting a
particular kind of bracket at the start of the text, and didn't find it.

=item  C<No quotelike operator found after prefix: "%s">

C<extract_quotelike> didn't find one of the quotelike operators C<q>,
C<qq>, C<qw>, C<qx>, C<s>, C<tr> or C<y> at the start of the substring
it was extracting.

=item  C<Unmatched closing bracket: "%c">

C<extract_bracketed>, C<extract_quotelike> or C<extract_codeblock> encountered
a closing bracket where none was expected.

=item  C<Unmatched opening bracket(s): "%s">

C<extract_bracketed>, C<extract_quotelike> or C<extract_codeblock> ran 
out of characters in the text before closing one or more levels of nested
brackets.

=item  C<Mismatched closing bracket: expected "%c" but found "%s">

C<extract_bracketed>, C<extract_quotelike> or C<extract_codeblock> 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<No block delimiter found after quotelike "%s">

C<extract_quotelike> or C<extract_codeblock> found one of the
quotelike operators C<q>, C<qq>, C<qw>, C<qx>, C<s>, C<tr> or C<y>
without a suitable block after it.

=item  C<Missing second block for quotelike "%s">

C<extract_codeblock> or C<extract_quotelike> found one of the
quotelike operators C<s>, C<tr> or C<y> followed by only one block.

=item  C<Nested codeblock failed to balance from "%s...">

A block within the substring being extracted by C<extract_codeblock> was
incorrectly nested or had a unmatched delimiter in it.

=item C<Did not find opening tag: /%s/>

C<extract_tagged> did not find a suitable opening tag (after any specified
prefix was removed).

=item C<Unable to construct closing tag to match: /%s/>

C<extract_tagged> 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<Found invalid nested tag: %s>

C<extract_tagged> found a nested tag that appeared in the "reject" list
(and the failure mode was not "MAX" or "PARA").

=item C<Found unbalanced nested tag: %s>

C<extract_tagged> 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<Did not find closing tag>

C<extract_tagged> 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@cs.monash.edu.au)


=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 (c) 1997-1998, Damian Conway. All Rights Reserved.
 This module is free software. It may be used, redistributed
and/or modified under the terms of the Perl Artistic License
     (see http://www.perl.com/perl/misc/Artistic.html)

