Web Automation: Generating Dynamic Tables of Contents Page 3
There are many ways to do anything- And this function is no exception. I initially wrote an more expanded, twelve-line version that essentially did the same thing, but decided that I should use a bit of Perl magic, and make it a little shorter.
The Glue: the "Main" function
Extracting those three functions from the code cleans it up quite a bit (although there is still room for another function or two). So now we have to build a system that utilizes those functions to perform our four tasks:
- Obtain a list of directories
-
For every directory, open the
index.htmlfile and extract the title of the page - Print back the title, with a link to the page
- Dive into the directory, and back to Step 1
If you were wondering why I was concerned with "depth" back in the Functions section, here's where it will make sense. I use the depth of the folder, to stratify where in an unordered list the item belongs (how "deep" in the list). This give our table of contents a more readable, and understandable feel, than just throwing out a flat list of links:
1: my $dir="/usr/local/apache/htdocs/";: unshift(@dirs,Get_Dirs("$currdir")); |
