tags:

views:

337

answers:

5

Is there a CPAN module that can read a string like this:

"[[<asdf>, <foo>], (abc, def, ghi), ({'jkl'})]"

...and parse it into some sort of tree structure that's easy to walk and pretty-print?

+1  A: 

Damian Conway's Parse::RecDescent

I haven't used this, but it's a good bet that a Perl module written by Damian Conway is worth using.

Bill Karwin
A: 

If the string is identical to the Data Dumper format, then it can be eval-ed back into the original structure.

codelogic
+9  A: 

I think that you could build on top of Text::Balanced, which will do a lot of the heavy lifting for you. You'll still need to build a tree structure, though.

Dave Rolsky
+1  A: 

Perl 6 is going to have built-in facilities to help support this. In the interim, Text::Balanced (as mentioned by Dave Rolsky) is probably the module of choice. Note that it, too, was written by Damian Conway (as commended by Bill Karwin).

Jonathan Leffler
+3  A: 

Perl 5.10's regular expressions can handle balanced structures like that. See the (?PARNO) (?-PARNO) (?+PARNO) (?R) (?0) section in perlre.

brian d foy