wiz has asked for the wisdom of the Perl Monks concerning the following question:

Hello again. I'm in the next production stage of this project of mine. I added the feature to search for yes or no to an existing program. there is a large DB file, where, on the first 20 documents, i put in yes or no.
Here is the DB file, and Here is the site as it runs
Here is the code to run it.
#!/usr/bin/perl -w use strict; use CGI qw(:standard); print header(), start_html(-title=>"Street Print CD Catalog", -background=>'/image +s/catalogbackground.jpg'); print "<img src=\"/images/StPrintImageLogo.gif\"><br><br>"; if (param()) { chomp(my $color = param("color")); chomp(my $pattern = param("pattern")); chomp(my $cd_number = param("cdnumber")); chomp(my $highres = param("highres")); print p(h4('You searched for:'),br(), h5("Color : $color"), h5("Patter +n : $pattern"), h5("Cd Number : $cd_number"), h5 ("Printable Images : + $highres")); print "<table border=1><tr align=\"center\" valign=\"middle\"><td widt +h =\"150\"><h3>Name</h3><h6>Picture number<br>\& name</h6></td><td wi +dth =\"100\"><h3>Color</h3></td><td width =\"100\"><h3>Pattern</h3></ +td><td width =\"100\"><h3>CD Number</h3></td><td width =\"100\"><h3>P +rintable image on cd?</h3></td><td width =\"150\"><h3>Thumbnail Image +</h3><h6>**Click to enlarge**</h6></td></tr>"; open (DBFILE, 'scfrom.db') or die "$!"; while (my $line = <DBFILE>) { chomp $line; my @record = split /\|/, $line, 7; for ($record[6]) { s/\r\n//; } ## $record[0] is name, 1 => color, 2 => pattern, 3 => cd, 4 => thumbna +il picture, 5=> screen res picture, 6=> yes or no high res. if( $color eq "Any" || $record[1] =~ /^$color$/i and $pattern eq "Any" || $record[2] =~ /^$pattern$/i and $cd_number eq "Any" || $record[3] =~ /^$cd_number$/i and $highres eq "Any" || $record[6] =~ /^$highres$/i ) { print "<tr align=\"center\" valign=\"middle\"><td width=\"150\">$recor +d[0]</td><td width=\"100\">$record[1]</td><td width=\"100\">$record[2 +]</td><td width=\"100\">$record[3]</td><td width=\"100\">$record[6]</ +td><td width=\"150\"><a href=\"/images/screenres/$record[5]\"><img BO +RDER=\"0\" src=\"/images/thumbnails/$record[4]\"></td></tr>\n"; next; } } close (DBFILE); print "</table>"; } else { print start_form(); print p('What Color do you want? ', popup_menu('color', ['Any', 'Brick +', 'Terracotta','Burnt Sienna','Hunter Green','Safety Blue','White', +'Bedrock','Slate','Sierra', 'Granite'])); print p('What Pattern do you want? ', popup_menu('pattern', ['Any', 'O +ffset Brick','Herringbone','Herr. Diagonal','Ashlar Slate','British C +obble','Frisco Cobble','Random Stone','Eurofan', 'Accent Circle'])); print p('What CD/Type of Photo do you want? ', popup_menu(-name=>'cdnu +mber', -default=>'Any', -values=>[qw(Any 1 2 3 4 5 6 7 8 9 10 11 12)] +, -labels=>{Any=>'Any CD', 1=>'CD1 : Municipal RoadWays', 2=>'CD2 : M +unicipal Crosswalks', 3=>'CD3 : Municipal Entrance Ways', 4=>'CD4 : M +unicipal Traffic Calming', 5=>'CD5: Municipal Walkways', 6=>'CD6 : Mu +nicipal Walkways', 7=>'CD7 : Residential Driveways', 8=>'CD8 : Reside +ntial Walkways', 9=>'CD9 : Residential Entrance Ways', 10=>'CD10 : Ac +cents and Misc.', 11=>'CD11 : Commercial Business/Parking Lots', 12=> +'CD12 : Commercial Entrance Ways'})); print p('Do you want high res printable images only? (Availible on the + CDs)', popup_menu(-name=>'highres', -default=>'Any', -values=>[qw(An +y Yes)], -labels=>{Any=>'All Images', Yes=>'Only with High res'})); print p(submit("Find"), reset("Reset")); print end_form; } print end_html;

----------------------------
Wiz, The VooDoo Doll
Head Master of 12:30 Productions
----------------------------

Replies are listed 'Best First'.
Re: Adding features to a CGI search engine
by tachyon (Chancellor) on Aug 02, 2001 at 03:09 UTC

    Dont't quite know what you want but here is a suggestion. To output HTML from a Perl script using HERE pages saves heaps of \" and print statements. Here is an example - note we can use them in assignment and also in printing. As shown strings interpolate within them. You can avoid interpolation by using single quotes ie <<'NO_INTERPOLATION':

    my $title = "Hello World"; my $body = <<BODY; <h1>Hello World</h1> <table> <tr> <td>42</td> </tr> </table> BODY print <<HTML; <html> <head> <title>$title</title> </head> <body> $body </body> </html> HTML

    Here pages are a lot more convenient for this sort of thing. You can use any string as the token but the closing token needs to be on a line by itself as shown with no leading or trailing whitespace.

    Some people like using CGI.pm to output HTML and others prefer templates like HTML::Template. Raw prints are the least favoured option. A convenient thing with herepages or templates is you can cut and paste HTML from your favourite editor.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Adding features to a CGI search engine
by arturo (Vicar) on Aug 02, 2001 at 03:25 UTC

    There are a few suggestions I'd make here. The first is to make your code a little *less* paranoid about newlines. You control all the form input already, so those chomps aren't going to do anything. The *only* place where you're going to potentially have extra newlines is on the end of each line, i.e. at the end of $record[6]. Just chomp each line as you read it in, and you can leave the rest. Since the DB and the CGI are running on the same OS, they should have the same "concept" of a newline. One trick you can use to find out if there are lingering characters is to print out the value within delimiters, i.e. print "<p>color is '$color'</p>\n";, so if it is a newline problem you can track it down a little quicker.

    Which reminds me, for ($record[6]) { s/\r\n//; } is an *odd* piece of code -- you're not iterating over anything. Just do $record[6] =~ tr/\r\n//d; instead (if chomping doesn't help).

    More importantly, I'd suggest using a different logic to generate matches. That if is long and hard to maintain. But that would take a lot of thought and effort, and it's rather late in the day.

    HTH!

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Adding features to a CGI search engine
by wiz (Scribe) on Aug 02, 2001 at 03:20 UTC
    Here's a fixed version of the code, toned down for only the things you need (i hope)
    #!/usr/bin/perl -w use strict; use CGI qw(:standard); print header(), start_html(-title=>"Street Print CD Catalog", -background=>'/image +s/catalogbackground.jpg'); if (param()) { chomp(my $highres = param("highres")); print "<table border=1> <tr align=\"center\" valign=\"middle\"> <td width =\"150\"><h3>Name</h3><h6>Picture number<br>\& name</h6></td +> <td width =\"100\"><h3>Color</h3></td> <td width =\"100\"><h3>Pattern</h3></td> <td width =\"100\"><h3>CD Number</h3></td> <td width =\"100\"><h3>Printable image on cd?</h3></td> <td width =\"150\"><h3>Thumbnail Image</h3><h6>**Click to enlarge**</h +6></td></tr>"; open (DBFILE, 'scfrom.db') or die "$!"; while (my $line = <DBFILE>) { chomp $line; my @record = split /\|/, $line, 7; $record[6] =~ tr/\r\n//; ## $record[0] is name, 1 => color, 2 => pattern, ##3 => cd, 4 => thumbnail picture, 5=> screen res picture, ##6=> yes or no high res. if( $color eq "Any" || $record[1] =~ /^$color$/i and $pattern eq "Any" || $record[2] =~ /^$pattern$/i and $cd_number eq "Any" || $record[3] =~ /^$cd_number$/i and $highres eq "Any" || $record[6] =~ /^$highres$/i ) { print "<tr align=\"center\" valign=\"middle\"> <td width=\"150\">$record[0]</td> <td width=\"100\">$record[1]</td> <td width=\"100\">$record[2]</td> <td width=\"100\">$record[3]</td> <td width=\"100\">$record[6]</td> <td width=\"150\"><a href=\"/images/screenres/$record[5]\"><img BORDER +=\"0\" src=\"/images/thumbnails/$record[4]\"></td> </tr>\n"; next; } } close (DBFILE); print "</table>"; } else { print start_form(); ##choose color ##choose pattern ##choose CD print p('Do you want high res printable images only? (Availible on the + CDs)', popup_menu(-name=>'highres', -default=>'Any', -values=>[qw(Any Yes)], -labels=>{Any=>'All Images', Yes=>'Only with +High res'})); print p(submit("Find"), reset("Reset")); print end_form; } print end_html;

    ----------------------------
    Wiz, The VooDoo Doll
    Head Master of 12:30 Productions
    ----------------------------