Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Perl Search script not working for my site 1

Status
Not open for further replies.

asleep24

Technical User
May 5, 2004
4
0
0
US
Hi All
I'm very new to Perl and was trying to add a simple perl search to my website.
I have a very basic form on my search page, literally just a search box and it is linked to this search.cgi file in my cgi-bin folder.
I have my permissions set to 755, but get an error when I run a search. Here is the script please let me know if you need more information or if you see where I could be going wrong.
Thanks!!!

#!/usr/local/bin/perl
use script;
use CGI qw:)standard);
use File::Find;
my $query = param("query");
print header();
print start_html();
print $query;
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;
},
'print "</ol>\n";
print end_html();
End
 
You seem to have some typos in your code. For example, the line

print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n;

is missing a trailing quote, and the two lines after that don't make sense - is that all supposed to be one line?

Have you tried running this through the Perl debugger? From the command-line:

perl -c search.pl

That should help you track down what line typos are on and what the problem is.

--G
 
Thanks G

I made a couple of changes, spotted some more typos. I think I may not have defined a correct directory for the search to begin from, the code looks like this now.
My search URL is The code currently looks like this, it still doesn't work.

#!/usr/local/bin/perl
use script;
use CGI qw:)standard);
use File::Find;
my $query = param("query");
print header();
print start_html();
print $query;
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";
print "</ol>\n";
print end_html();
End
 
There are still errors in how you are trying to use File::find. Builder.com has an introduction to the use of File::Find that may be helpful:


However, there may be a much easier way to do this. Are you on a UNIX-based server, and if so, do you have access to the UNIX utilities grep, fgrep, or find? If so, I can show you a much shorter approach.

--G
 
Thanks for the builder.com tip, I'll check it out.
Yes this is being run on a UNIX server and I have access to grep and find at the very least. I would really appreciate an easier way to do this thanks.

Steve
 
You'll want to tweak this to add your own HTML layout, etc. I took a guess at your web root.

I used fgrep here instead of grep because its less memory/cpu intensive for doing simple keyword matching. If you don't have fgrep you can use grep with the same syntax.

Code:
#!/usr/local/bin/perl

use CGI qw(:standard);
my $query = param("query");

# set a global var with the base path to your Web root
$WEBROOT = "/users/moulste/design";

# use the UNIX 'find' command to get a list of html pages
@pages = `find $WEBROOT -name \"*.[Hh][Tt][Mm][Ll]\"`;
chomp @pages;

# look through pages for hits
undef $/; 
foreach $page (@pages) {
  # use grep if you don't have fgrep
  if (`fgrep -ic \"$query\" \"$page\"\n`) {  # this page has hits in it
    
    # get contents of file
    open(PAGE,"<$page") or die "ERROR: Could not open file $page\n";
    $contents = <PAGE>;
    close PAGE;
    
    # get the page title if there is one, otherwise use URL
    if ($contents =~ m/<title>(.*?)<\/title>/i) {      
      $title = $1;
      $title =~ s/\n/ /g;
    } else {
      $rel_page = $page;
      $rel_page =~ s/$WEBROOT//; # strip web root off of path
      $title = "[URL unfurl="true"]http://itwebmaster.iit.edu/~moulste/$rel_page";[/URL]
    }
    
    $results{$page}=$title; # store pages with hits and their titles in a hash
    
  } # done with this page

} # done processing pages

# start printing things back
print header();
print start_html();

# print back result list 
print "\n<p>For the query $query, these results were found:</p>\n<ol>\n";
foreach $page (%results) {
  print "<a href=\"$page\">$results{$page}</a><br>\n";
}

print end_html();


See how that works for ya.

--G
 
Thanks again G

I'm past the error now, I'm not generating results but I'm sure thats just a matter of doing some tweaking.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top