search.cgi

K

Ken Saunders

I'm writing a script to search through all of my html files and return
the results of the query. I have used an html form file in the past I
want to integrate it all into one cgi file. can someone help here is
the code I've come up with.

#!perl -w
use strict;
use File::Find;
use CGI qw:)standard);

print "Content-type: text/html\n\n";
print "<HTML><HEAD>";
print "<TITLE>Ken's Site Search</TITLE>";
print "</HEAD>";
print "<BODY>";
print <form action="/cgi-bin/assignment_3.cgi" method="post">;
print <input type="text" name="quety" size="40" /> ;
print <input type="submit" /> ;
print </form>;
print "</BODY></HTML>";

my $query = param("query");
print header();
print start_html();
print "\n<p>For the query $query, these results were
found:</p>\n<ol>\n";
undef $/;

find( sub
{
return if($_ =~ /^\./);
return unless($_ =~ /\.html/i);
stat $File::Find::name;
return if -d;
return unless -r;

open(FILE, "< $File::Find::name") or return;
my $string = <FILE>;
close (FILE);

return unless ($string =~ /\Q$query\E/i);
my $page_title = $_;
if ($string =~ /<title>(.*?)<\/title>/is)
{
$page_title = $1;
}
print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n";
},
'/home/ksaund01/public_html');

print "</ol>\n";
print end_html();

End

Were am I going wrong, any help is most appreciated.

(e-mail address removed)
 
G

Gunnar Hjalmarsson

Ken said:
Were am I going wrong, any help is most appreciated.

You are not telling us what the script outputs and how that differs from
what you had expected.

Btw, 'quety' ne 'query'.
 
J

John Bokma

Ken said:
#!perl -w

Just some remarks:

put -T there, and remove -w
use strict;

use warnings; # instead of -w
use File::Find;
use CGI qw:)standard);

my $cgi = new CGI;
print "Content-type: text/html\n\n";
print "<HTML><HEAD>";
[...]

Uhm, you are already returning a HTML page here, so what you are doing
below is wrong:

Also don't print Content-type: yourself, and read on here doc
if you want to output several lines (ie. print <<"HTML"; )
my $query = param("query");

check what you get here.

You probably want "if $query is undefined then show the search page (as
given above, otherwise do the following:"
print header();

Note that CGI defaults to xhtml, is that what you want? (Most of the
time the answer is NO).
print start_html();
print "\n<p>For the query $query, these results were
found:</p>\n<ol>\n";
undef $/;

find( sub
{
return if($_ =~ /^\./);
return unless($_ =~ /\.html/i);

I prefer more an assertion like style:

/\.html?$/i or return;

note that I check html and htm (case insensitive), and also the file
must end with it (yours matches bla.html.gz too)
stat $File::Find::name;

Doesn't stat default to $_, and there is no need for $File..etc? (Not
sure)
return if -d;
return unless -r;

open(FILE, "< $File::Find::name") or return;
my $string = <FILE>;
close (FILE);

I prefer the

open my $html, $File::Find::name or return;
my $string = <$html>;
close $html; # not really needed

way

Or even File::Slurp, in this case.
return unless ($string =~ /\Q$query\E/i);

$string =~ ... or return;
my $page_title = $_;
if ($string =~ /<title>(.*?)<\/title>/is)
{
$page_title = $1;
}
print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n";
},
'/home/ksaund01/public_html');

Note that file path and URL are not the same thing, you need to do some
magic to get an URI in the href part.
 
K

Ken Saunders

On Thu, 18 Nov 2004 01:49:08 +0100, Gunnar Hjalmarsson


Thanks for replying. I'll try and be more clear. I have two files
right now. I'm trying to merge the two into one script file that will
display the search form with submit button and then will return the
results with links.

The current HTML page is simply

<form action="/cgi-bin/search.pl" method="post">
<input type="text" name="query" size="50" />
<input type="submit" />
</form>

along with background and such.

The other is the search.cgi and is

#!perl -w
use strict;
use File::Find;
use CGI qw:)standard);
my $query = param("query");
print header();
print start_html();
print "\n<p>For the query $query, these results were
found:</p>\n<ol>\n";
undef $/;

find( sub
{
return if($_ =~ /^\./);
return unless($_ =~ /\.html/i);
stat $File::Find::name;
return if -d;
return unless -r;

open(FILE, "< $File::Find::name") or return;
my $string = <FILE>;
close (FILE);

return unless ($string =~ /\Q$query\E/i);
my $page_title = $_;
if ($string =~ /<title>(.*?)<\/title>/is)
{
$page_title = $1;
}
print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n";
},
'/home/username/public_html');

print "</ol>\n";
print end_html();

End

Thanks again for any assistance.

Ken
 
T

Tad McClellan

Ken Saunders said:
I'm writing a script to search through all of my html files and return
the results of the query. I have used an html form file in the past I
want to integrate it all into one cgi file. can someone help here is
the code I've come up with.


Get it to run from the command line *first*.

Move it to the CGI environment after you think you have it working
on the command line.

Adopt a more heathful style of indenting.

Use here-docs instead of a bazillion print()s.

Understand what a function will do for you *before* you
call that function.

print "Content-type: text/html\n\n";
print "<HTML><HEAD>"; [snip]
print header();
print start_html();


Why are you printing 2 content types and 2 start tags?

Do you know what the functions you've called there will do for you?

stat $File::Find::name;


Why are you calling stat() here?

It is not doing whatever it is that you think it is doing...
 
T

Tad McClellan

Ken Saunders said:
<form action="/cgi-bin/search.pl" method="post"> ^^^
^^^

The other is the search.cgi and is
^^^^
^^^^

One of these things is not like the other,
one of these things just doesn't belong...
 
J

Joe Smith

Ken said:
open(FILE, "< $File::Find::name") or return;

I'm guessing that your problem will become visible if you
change "or return" to something that prints the name of
the file that could not be open for reading.
<a href=\"$File::Find::name\">

Your script make no reference to $ENV{DOCUMENT_ROOT}, and
that means that your open()s are doomed to failure.

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top