- 5,178
- Worcester, MA
- skip0110
I'd greatly appreciate any help here. Just started hacking together a quick little perl search script which is mostly copied from A List Apart. This is the first Perl I have written.
Basically, the code currently looks through all the files in a directory and lists them if they contain the $query (passed in POSTDATA). I'd like it to parse subdirectories too. I tried to do this with the recursive call find( \&d,$File::Find::name); but although it is "seeing" the subdirectories (prints stuff like "saw directory ../htdocs/scion/images") it is obviously not searching through them. What am I missing? I'm pretty much going blind looking at all this $_=~/^\.\ stuff.
Thanks in advance.
Basically, the code currently looks through all the files in a directory and lists them if they contain the $query (passed in POSTDATA). I'd like it to parse subdirectories too. I tried to do this with the recursive call find( \&d,$File::Find::name); but although it is "seeing" the subdirectories (prints stuff like "saw directory ../htdocs/scion/images") it is obviously not searching through them. What am I missing? I'm pretty much going blind looking at all this $_=~/^\.\ stuff.
Code:
#!/usr/bin/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( \&d,'../htdocs');
print "</ol>\n";
print end_html();
sub d
{
return if($_ =~ /^\./);
if (-d) {
print "saw directory $File::Find::name<br>\n";
find( \&d,$File::Find::name);
}
return unless($_ =~ /\.html/i);
stat $File::Find::name;
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=\"http://www.carsource.org/index.php?$File::Find::name\">$page_title</a></li>\n";
}