A lot of nodes above are pointing out solutions using various modules. I'm just thinking about how to do this without using an existing formatter (if only for the intellectual exercise).
Keep a variable named $tab_count, intitilized to 0. Each time you see a valid begin tag, increment $tab_count, and print a newline followed by "\t" x $tab_count after the begin tag. Whenever you see a valid end tag, decrement $tab_count and print the same string as above.
----
Reinvent a rounder wheel.
Note: All code is untested, unless otherwise stated