To run under mod_perl you should move all the initialization code to a BEGIN block. This BEGIN code is then be run the first time the Apache daemon runs the program, and remembered for subsequent runs. If you had cleanup to do when the daemon dies, put it in an END block. I don't see any cleanup code in your program, though.

Your program works with two files for each run, one is the index and the other is the content. Under mod_perl the index, at least, should be held in RAM and you will be down to one file read per page. If you have enough RAM you should consider keeping both files in RAM. You could have a hash $file_content{$node} that stores a file per node as each file is read. This way the daemon would only read each file once.

To further speed up your code, eliminate as many print statements as possible. For example, change:

print $q->header('text/html'); print $q->start_html(-title=>$node, -author=>'schmidtd@co.delaware.pa.us', -BGCOLOR=>'white');

to

print $q->header('text/html'), $q->start_html(-title=>$node, -author=>'schmidtd@co.delaware.pa.us', -BGCOLOR=>'white');

This uses ',' to separate the arguments to print, rather that a '.' since the comma is faster than string concatenation.

The code goes to the trouble of reading the file into an array, and then printing it out one array element at a time. You don't need to do this. Instead, read the whole file into a single string and print the string.

If you don't want to follow my advice on not using an array, at least change

foreach(@lines){ print $_; }
to something like:
my $content; foreach(@lines){ $content .= $_; } print $content;
or a similar statement using join
print join "\n",@lines;

There are some other speedup tips at a previous node that I wrote on this topic.

It should work perfectly the first time! - toma


In reply to Re: I know this code could be better... by toma
in thread I know this code could be better... by derek3000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.