Maybe you could use
Parse::RecDescent for your aims.
You can find a good documentation in
Parse::RecDescent::FAQ.
Maybe this module is a little oversized for this task, but it's the best way imho for write a clear code.
----------
kral
(I apologise for my english!)