Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

I recently had an interesting problem.

I had a long HTML file divided up into sections which were marked by <A> tags for the section names, like

<A NAME="audio"> blah blah blah <A NAME="video">etc etc etc

What I wanted to do was divide this file up into separate files, one for each section, and name them according to the section.

I messed about with various solutions and wrote three or four bits of code in order to do it in three or four passes, but I knew all along that there'd be a smart, Perlish way to do it.

How I did it isn't important -- how would you have done it?

Replies are listed 'Best First'.
Re: Dividing HTML up into chunks
by nysus (Parson) on Jun 28, 2001 at 06:11 UTC
    #1: How you did it is important. It helps us to point out where your thinking is wrong. Plus, people will be more likely to help you here.
    #2: You should post the code. Shorten it up to the bare essentials or else use a READMORE tag. Again, if you do this, people will be more likely to help you.
    #3: I'm not trying to be a jerk, just want to make sure you get the best answer you can get.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot";
    $nysus = $PM . $MCF;

Re: Dividing HTML up into chunks
by andreychek (Parson) on Jun 28, 2001 at 07:34 UTC
    One possible way to handle this would be to use HTML::Template. It offers you the ability to use include statements within your HTML code. So, using your example, you could do something like:
    main.html --------- <html> <body> <TMPL_INCLUDE="audio.html> ... <TMPL_INCLUDE="video.html> ... </body> </html>
    And then of course, audio.html and video.html would contain all the HTML code for those particular sections.

    There are other templating systems though. Here is a node that describes various templating systems. Template Toolkit may end up being more powerful in the end, but I find it easier to keep presentation/code seperated using HTML::Template. So check em both out and decide :-)
    -Eric
Re: Dividing HTML up into chunks
by srawls (Friar) on Jun 28, 2001 at 06:09 UTC
    Note: untested code, written as I post.
    open FH, "$file"; { local $/; $text = <FH>; } for($text =~ /<A NAME="([^"]+)">(.*?)(?!<A NAME=")/) { #file name is $1 #text is $2 }
    Note, I saved time and used .*?. See death to dot star for why not to do that. Also, look at unrolling the loop. I leave that as an exercise to you, because I can't just hand you everything now can I : )

    The 15 year old, freshman programmer,
    Stephen Rawls