in reply to nested level section closing

Hi, welcome to PerlMonks

Firstly your input is not valid HTML, since each heading tag should have a closing tag:
<h1>Heading level 1</h1> <h2>Heading level 2</h2>
And so on. Fixing this may be advantageous before starting to parse the data.

I think you should take a look at using a combination of HTML::TokeParser (some people prefer HTML::TokeParser::Simple or HTML::TreeBuilder) and HTML::Template. Using HTML::TokeParser (or similar) to get the data structure (see Working with HTML from the tutorials section of this site for further info), and HTML::Template to format the HTML in the way you want to based on the data structure you have as a result from parsing your input (see Framework, Templating, and Content Management Systems from the tutorials section of this site). The modules are well documented.

If you need help installing modules look at Modules: How to Create, Install, and Use from the tutorials section of this site.

If you have any further problems, post any code and error message(s) you have.

Update: fixed typo

Hope this helps


Update:It would seem I have 'jumped the gun' as they say, please disregard this advice.

Martin