6

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?

mike
  • 46,876
  • 44
  • 102
  • 112

5 Answers5

9

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
  • 4,524
  • 1
  • 24
  • 24
4

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
  • 129,424
  • 31
  • 207
  • 592
2

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
  • 730,956
  • 141
  • 904
  • 1,278
1

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.

cjm
  • 61,471
  • 9
  • 126
  • 175
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
0

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

codelogic
  • 71,764
  • 9
  • 59
  • 54
  • That's actually a neat idea, if you're sure the input is valid and all that. Thanks. Edit: Perl's auto-flattening would potentially lose a lot of info from the input though. – Sundar R Jul 24 '13 at 07:34