Hello all, I am making a archive section for a website so far it looks like http://skagitattic.no-ip.org/gokee2/grandpa/website/website/names.cgi. I made it using mostly a cgi file with print statements and a few of the prints just printing whatever is shoved into them from tt2. However as you can see from my post the code looks like a mess and my tt file is not much better. I can find where stuff is in the source now but I hate to think what it will be like a few weeks away... I was wondering what I should do to clean it up? I have seen mention of a tt2 mod for to have apache do cgi with a tt2 file, anyone know if that works well? Converting all my perl code to tt2 would take a while but if it cleans it up might be worth it. I made the code quite a while ago and it has some rather bad parts anyway. So what I am really asking is a design issue. The code works as it but is a pain to work with. Should I try to go all tt2 or try another mix? I was thinking about making a .tt file holds the perl code and trying to make another file that puts it together but it looked like a mess when I started to put it together. It does not seem like what I am doing is that complicated. Anyway here is the code.
#!/usr/bin/perl use strict; [% PROCESS archives.tt -%] #loads mods for easy web page CGI use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); #Loads a mod for easy file access use Tie::File; #Loads a mod used for determining how many pages worth of names there +are use POSIX; print "Content-type: text/html\n\n"; our $csv_file = '../archives.csv'; our $delete; our $search; our $page_start; our $page_end; our $host = "$ENV{HTTP_HOST}"; our $edit; our $current_page = 1; our @list; our $top; our $footer; our ($url_to_myself, $info) = split (/names.cgi\?/, $ENV{REQUEST_URI}) +; our $lines_per_page = 3; our $lines; our $pages; #our $lines_in_file = @names; our ($lines_in_file, undef) = split(/\s/, `wc -l $csv_file`); our $pages_in_file = ceil($lines_in_file/$lines_per_page); our @line_number; our $search_for; #Puts the information from the end of the url in usable form my @info = split("&", $info); foreach my $part(@info){ my ($property, $value) = split("=", $part); if ($property eq "page"){$current_page = "$value";} if ($property eq "edit"){$edit = "$value";} if ($property eq "search"){$search = $value;} if ($property eq "delete"){$delete = $value;} } if ($edit){ edit(); } elsif ($search && $search ne ""){ $search_for = $search; search(); page_search(); } elsif ($delete){ delete_name(); } else{ page_list(); } sub top{ if ($search && $search ne ""){ } else{ tie @list, 'Tie::File', "$csv_file" or print '<BR><BR><H3> The archi +ves are missing! Please email the webmaster.</H3>' and exit; } print '<FORM action="'. "names.cgi". '" method="get"> Search: <INPUT type="text" name="search" size="12" value="'. "$search_for". '"> <INPUT type="submit" value="Go"> <BR><BR> '; $lines = scalar @list; $pages = ceil($lines/$lines_per_page); } sub pages{ if ($pages > 1){ print "\n<BR><BR>Page\n"; my $page = 0; while ($page < $pages){ $page = $page+1; if ($current_page ne $page){ print "<A href=\"names.cgi?page=$page&search=$search_for\">$pa +ge</A>\n"; } else{ print "<B>$page</B>\n"; } } } print '[% foot %]'; } sub page_list{ print '[% head %]'; print '<H1 id="title">Archives</H1>'. "\n"; top(); print '<TABLE border="1">'; list(); print '</TABLE>'; pages{}; } sub page_search{ print '[% head %]'; print '<H1 id="title">Resaults</H1>'. "\n"; top(); if ($lines <= 0){ print "<BR>\n\"$search_for\" - did not match any names. "; } else{ print "Found $lines results for $search_for.\n<BR>"; print '<TABLE border="1">'; list(); print '</TABLE>'; } pages{}; } #list(); #@names = sort {uc($a) cmp uc($b)} @names; sub list{ my $page_end; if ($current_page > $pages){$current_page = 1;} my $page_start = (($current_page-1)*$lines_per_page); if ($current_page == $pages){$page_end = $lines;} else {$page_end = ($current_page*($lines_per_page));} my $count = ($page_start); while ($count < $page_end){ my $name; my $title; my $description; if ( $list[($count)]=~/"(.+)","(.+)","(.*)"/ ){ $name = $1; $title = $2; $description = $3; } else{ $name = ""; $title = "Bad Line"; $description = "Malformed line at line number ". ($count+1). "." +; } print "<TR><TD><A href=\"$name\">$title</A></TD><TD>$description< +/TD></TR>\n"; $count ++; } } sub search { tie my @data, 'Tie::File', "$csv_file" or print '<BR><BR><H3> The arch +ives are missing! Please email the webmaster.</H3>' and exit; my $count = 0; foreach my $line(@data){ if ($line =~ /$search_for/i){ push (@list, $line); push (@line_number, $count); } $count ++; } }
And the .tt file archives.tt looks like
[%- head = BLOCK -%] [% top_selected = "Archives"; PROCESS top.tt -%] <link rel="stylesheet" type="text/css" href="[%before%]style.css"> </head> <BODY id="main"> [% PROCESS topbar.tt %] <DIV class="normal"> <BR> [%- END %] [% outro = BLOCK %] Test [%- foovar %] [%END%] [%- foot = BLOCK%] </DIV> </DIV> </BODY> </html> [%- END %]

In reply to tt2 with perl cgi and a csv file by Gokee2

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.