Hi monks,

I'm currently fixing a few bugs in Mail::IMAPClient that for whatever reason have gone unnoticed. One of these is that its fetch_hash() method can't handle a parenthesized value if contains parentheses. In other words, its not dealing with nested parentheses correctly.

A typical server response to a FETCH command might look something like this:

* 2 FETCH (UID 2 FLAGS (\Answered \Seen) INTERNALDATE "05-Jun-2008 0 +7:19:01 +1000" RFC822.SIZE 5144 BODYSTRUCTURE ("text" "plain" ("chars +et" "ISO-8859-1") NIL NIL "7bit" 2053 46 NIL ("inline" NIL) NIL NIL))

fetch_hash() seeks to turn that into the following hash:

"UID" => "2", "FLAGS" => "\Answered \Seen", "INTERNALDATE" => "05-Jun-2008 07:19:01 +1000", "RFC822.SIZE" => "5144", "BODYSTRUCTURE" => "text" "plain" ("charset" "ISO-8859-1") NIL NIL " +7bit" 2053 46 NIL ("inline" NIL) NIL NIL",

In other words, the parenthesized data consists of key/value pairs. The values are either simple strings (matched by \S+), quoted strings (matched by "[^"]*") or a parenthesized fragment, which may include parentheses itself.

What's inside the parentheses is actually completely opaque as far as this method is concerned. The original code used \([\)]*\) to try and match it, which works great until there is a closing paren inside.

My attempt matching/capturing the parenthesized fragment is based on mjd's regex to match balanced parentheses. Its actually part of a larger regex that matches the other stuff described above, but this doesn't work in isolation either so I don't think those things are affecting it:

(?: (?{ local $d=0 }) # set depth to 0 ( # start capture (?: \( # opening paren (?{$d++}) # increment the depth | \) # or closing paren (?{$d--}) # decrement the depth (?(?{$d==0}) # if we're back to the start (*ACCEPT) # we're done ) | (?>[^()]*) # or there's just some normal text )* (?(?{$d!=0}) # done. did we reach a matching closing paren (?!) # nope, failed ) ) # end capture )

Running a basic test program with -Mre=debug seems to suggest that this is doing something close to what I want, stopping when it gets to the correct closing paren. The only problem is that nothing gets captured, even though the description of (*ACCEPT) in perlre makes it sound like it should.

So, what's wrong here? And is there an easier way to do this? Note that I'm restricted to Perl 5.6, which sucks as I'd really like to try the PARNO stuff that came with 5.10.

Cheers,
Rob.


In reply to Extracting a parenthesized fragment from a string by fce2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.