Gokee2 has asked for the wisdom of the Perl Monks concerning the following question:
And the .tt file archives.tt looks like#!/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 ++; } }
[%- 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 %]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: tt2 with perl cgi and a csv file
by stiller (Friar) on Mar 26, 2008 at 09:18 UTC | |
by Gokee2 (Acolyte) on Mar 27, 2008 at 19:52 UTC | |
by stiller (Friar) on Mar 27, 2008 at 20:44 UTC | |
|
Re: tt2 with perl cgi and a csv file
by Your Mother (Archbishop) on Mar 26, 2008 at 16:01 UTC | |
by Gokee2 (Acolyte) on Mar 27, 2008 at 20:04 UTC | |
by Your Mother (Archbishop) on Mar 27, 2008 at 20:22 UTC | |
by Gokee2 (Acolyte) on Mar 28, 2008 at 04:24 UTC | |
by Your Mother (Archbishop) on Mar 28, 2008 at 06:28 UTC | |
| |
by stiller (Friar) on Mar 28, 2008 at 05:24 UTC | |
|