in reply to Matching string, then getting next line
One thing that's good to note is that I'd personally say don't just assume that $_ holds what you think it does. My advice would be NOT to use $_, particularly when using regular expressions.
My reasoning is that anonymous variables almost always store up a world of trouble for you, and although I haven't looked at your example in any detail, I would think that doing multiple matches MIGHT have the effect of altering $_......
Don't take this the wrong way, though. But you'll find that a combination of use strict; and named variables will also make the code waaay more obvious!
Of course, I could just be talking total cack.... :)