in reply to tt2 with perl cgi and a csv file

At first sight, this looks like a mess, but it's actually not so bad.
The good part: you have divided the work into manageable pieces (subs). The bad part: an enormous amount of global vars make it hard to follow.
To rewrite, I would start by building up an array of hashes. The array represents the lines from the csv-file, and the hashes is the data on each line. Iterating over the csv file you decide if the current line is to be included or treated special. A simple rewrite of the template, and feed the AoH to it. Done.
cheers

Replies are listed 'Best First'.
Re^2: tt2 with perl cgi and a csv file
by Gokee2 (Acolyte) on Mar 27, 2008 at 19:52 UTC
    "I would start by building up an array of hashes. The array represents the lines from the csv-file, and the hashes is the data on each line." So you would load the whole csv file into memory? "Iterating over the csv file you decide if the current line is to be included or treated special." So now I have only parts of the csv file in memory? So if I searched to foo I would only then have the lines that have foo in then in my array? What about editing? In my older even worse write of this (kept track of a mailing list) I would edit the item based on line number. I guess I could put the line number into my hash in the array though. I guess I am not really seeing how this cleans it up much. I might try it though, I will think about it some more. I have just been changing it about so that all the url options go into %options. That is helping a bit. I have also tried to get rid of some of the global vars that are not used anymore. Then I put my page_list and page_search right into the "if ($options{search}){" and else. I put head foot and outside of the if statement. Its helping a little. Also what is "AoH"? Thanks!
      AoH is the Array (lines) of Hashes (fields).

      Your Mother's answer below is very sound advice. If you have more than one script / template, time invested in learning a framework will pay back great. If you want to try that, I'd recomend strongly that you check out catalyst. The new book they advertise is very good (I've read it) if you like a stepwise and practical introduction. Otherwise they have lots of online tutorials and documentation if you prefer that.

      If you don't want to go that route yet, try factor out the parsing of the csv data (take a look at Text::CSV_XS and DBD::CSV. The latter of these will make your csv file feel like a database, simplifying searching and paging. Also, pull together one coherent template by extracting the print statements from the code.

      hth