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

I use Perl for the web. A lot. So I am trying to do this neat little thing with the grep function. That is the key to my little search engine for looking up names. The only trouble is when I try to use it, the darn thing doesn't work. I thought it was with the file that the contains the names. But it isn't that. I use HTML::Template heavily here, so could someone check over my code and see if it is correct, or maybe where I am going wrong?
#!/usr/bin/perl #Start the modules use strict; use CGI; #Create a new CGI object my $Page = new CGI; #The secret ingredient! use lib "../lib"; use HTML::Template; #Save that shit my $Shit = $Page->param('Info'); my $Le_File = $Page->param('file'); #If they used more than one word my $Wrong_Text = <<'TEXT'; You used a space in your query, which you should have not done. Instead, just use only one word. Please try your query again. TEXT #If they typed a symbol at the front of the word my $Wrong_Symbol = <<'WRONG'; Don\t type a symbol at the front of the word. Instead, just type the word in and hope for the best. Or, use the symbol \^ at the start of the word for matching names beginning with that word. Conversely, use \$ at the end of the word to get names ending in that word. Plus, this stuff is case sensitive. WRONG #If there is no file unless(open 'FILE', "../$Le_File") { print $Page->header(); print <<HTMLSTUFF; <html> <head> <title> No file </title> <body> <p>Make sure the file $Le_File is there</p> <p>Now exiting program</p> </body> </html> HTMLSTUFF exit; } #Start a new template my $Template = HTML::Template -> new(filename => '../Template/Search_E +ngine.tmpl'); #Check the variable to see if it has spaces in it if ($Shit =~ m/ /g) { $Template->param(Bad_Stuff => "$Wrong_Text", option => 'value' +); print $Template->output(); exit; } #Weed out other shit elsif ($Shit =~ m/^(\+|\-|\!|\#|\\)/g) { $Template->param(Bad_Stuff => "$Wrong_Symbol", option => 'valu +e'); print $Template->output(); exit; } #Read the whole thing into an array my @Lines = <FILE>; close(FILE); #Use grep to find the goods my @Names = grep(m/$Shit/, @Lines); ############The whole thing #Print out the word Results $Template->param(Results => "<p>Here\'s the results for $Shit<\/p> +"); #Use the looping schtick in HTML::Template to print out of the informa +tion in the array $Template->param(LE_LOOPY => [Le_Names => "@Names"]); ##########End it all #And finally print $Template->output();
Thanks for any help.

Edit by dws for a more appropriate title

Replies are listed 'Best First'.
(jeffa) Re: Problem using HTML::Template.
by jeffa (Bishop) on Jun 14, 2002 at 00:02 UTC
    You have a number of potention pitfalls in this script. The first is that you are not correctly using HTML::Template by generating HTML that should be encapsulated into a template file. A good rule of thumb is, if you print any HTML in your script whatsoever, such as
    $Template->param(Results => "<p>Results: $shtuff<\/p>");
    or
    print <<HTMLSTUFF; <html> <head> <title>No file</title> ...
    you are using it wrong. To correct the first example, only pass a variable (such as the less offensive $shtuff scalar) and do not pass any HTML - the template should contain the markup, not the script. To correct the second example, place everything in the heredoc into a template file.

    Also, this:

    my $Template = HTML::Template->new( filename => '../Template/Search_Engine.tmpl' );
    is very non-portable. You specify a full path to the file instead of using dot dot.

    Please read HTML::Template Tutorial - it will take you less time to read it then it took me to write it, and you will be better off after having worked through the examples it presents.

    Last note, this:

    #The secret ingredient! use lib "../lib"; use HTML::Template;
    Is a non-portable hack that can be improved by reading A Guide to Installing Modules, specifically this section.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Problem using HTML::Template.
by samtregar (Abbot) on Jun 13, 2002 at 22:08 UTC
    Can you be a little more specific about what exactly isn't working? Are you getting a "500 Internal Server Error"? If so, do you get an error message in your server's error logs?

    Maybe the problem is that you're not printing an HTTP header? Try adding this before the last print line:

    print "Content-Type: text/html\n\n";

    -sam