in reply to Search engine result limiting (was: help!!)
If you want display one title for a search that's found a match and a different one for a search that fails, then your script should first do the search, THEN generate the HTML for the page. That's in fact what you're doing, so it would be as simple as adding a title into the string that you push onto the @match array.
However, you should change what you're doing => notice that you're adding a mini-HTML document for *each match* you find ... that's bad HTML, and it's also unnecessary. Just add the information about each document during the search to the @match array, and when the search is done, check to see that the array has at least one item (if (@match) { } is idiomatic Perl that will do this test): if it does, generate a standard HTTP header and your "we found one" HTML header, and then print out the match results. If you don't find any matches, print the "we're sorry, we couldn't find any matches" message.
Here are a few more general suggestions about your code; the idea here is to tell you about some tools and strategies that will make your life easier if you learn how to use them; strictly speaking, most of them are not necessary to get this script to work the way you want it to, but some of them are very important.
First, you appear to be using some sort of custom form-parsing routine. see use CGI or die; for why this is not a good idea. The standard (i.e. already installed) CGI module is well tested and secure, and heavily documented both here and just about everywhere Perl is spoken. Use it to get at your form data; if you want, you can also use it to generate HTML.
If you want to extract a string in between HTML tags (your title-snagging routine) there's a much cleaner way to do it, assuming the <title> opening and closing tags are all on the same line (if they might not be, consider using HTML::Parser, which handles such things nicely):
my $page_title = "unknown" # (default value) if (/<title>(.*?)<\/title>/) { $page_title = $1; # set the value of $page_title # to be whatever was found in # between the <title> and </title> # tags }
I noted why this is not perfect; but I just wanted to introduce you to the notion of using backreferences to capture data from a regular expression.
Then of course, there is the standard (and it's standard for good reasons) exhortation to use strict; in all your scripts and to turn on warnings, especially while developing. It catches errors you will make, will show you where you're making certain assumptions that you shouldn't be making, and so forth.
HTH
Philosophy can be made out of anything. Or less -- Jerry A. Fodor
|
|---|