[sf-perl] opendir question..for search script

Quinn Weaver quinn at fairpath.com
Tue Jan 23 23:19:47 PST 2007


On Mon, Jan 22, 2007 at 11:33:09AM -0800, Vargas Media wrote:
> Hi All,
> I have a quick question.
> I'm using "opendir" and trying to get my search.cgi script to search in my main www directory instead of my cgi-bin 
> as the HTML pages found can't be accessed there.

Not sure I see the problem.  Why can't you just prefix the path you want
to $dir ?  This depends on 

Can you just prefix your www path to $dir before calling opendir?
That's the quick-and-dirty fix.  If it doesn't work, then you have to
delve into your web server configuration (for security reasons, it may
forbid your CGI script from seeing directories besides your cgi-bin).

As a side note, I think you are using an outdated tutorial.  It uses
local instead of my variables--a very old, deprecated Perl
practice--and it prints out HTML from Perl code.

Nowadays most people write Web programs using templating systems like
HTML::Mason.  There are many to choose from; here's a good overview:

    http://www.perl.com/pub/a/2001/08/21/templating.html

Another option is Catalyst, which is the Perl answer to Ruby on Rails.
It effects an even cleaner separation of concerns among data storage,
business logic, and (X)HTML presentation.

> I did this as a tutorial from a book I have.
> Here are my Env Variables:
> http://www.autotection.com/cgi-bin/env_var.pl
> any help very appreciated.
> Thanks Steve
> ############search.cgi
> 
> #!/usr/bin/perl
> &get_form_data();
> $search_term = $FORM{'search'};
> print "Content-type: text/html\n\n";
> &search(".");
> 
> sub get_form_data
>  {
>   # get the data
> read(STDIN, $buffer, $ENV{ 'CONTENT_LENGTH' } );
> 
> @pairs = split(/&/, $buffer);
> foreach $pair (@pairs)
> {
>   ($firstname, $value) = split(/=/, $pair);
> 
>  $value =~ tr/+/ /;
>  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
>  $value =~ s/<!--(.|\n)*-->//g;
>  $FORM{$firstname} = $value;
>   }
> }
> 
> print <<EOF;
> <HTML>
> <HEAD>
> <TITLE>
> Search
> </TITLE>
> </HEAD>
> 
> <BODY BGCOLOR="#FFFFFF">
> <P>Results of your search: $search_term</P>
> EOF
> foreach $file (@found_set)
> {
>     print "<A HREF=\"$file\">$Title{$file}</A>\n";
>     print "<BR>\n";
> }
> if ((@found_set) != "") {
>   print "<H3>Above Are Matches for the Search Term: $search_term</H3>\n";
> } else {
>   print "<H3>Sorry, no files found under the search term: $search_term</H3>\n";
> }
> 
> print <<EOF;
> </BODY>
> </HTML>
> EOF
> exit;
> sub search
> {
>  local ($dir) = @_;
>  if($dir eq ".")
>  {
>   opendir(DIR, ".");
>   $dir = "";
>  }
>  else
>  {
>   opendir(DIR, $dir);
>   $dir .= "/";
>  }
>  foreach $file (sort readdir(DIR))
>  {
>   next if($file eq "." || $file eq "..");
>   $file = $dir . $file;
>   next if(($file !~ /.htm/) && (!(-d $file)));
> 
>   if(-d $file)
>   {
>    &search($file);
>    next;
>   }
>   open(FILE, $file);
>   $found_match = 0;
>   $title = "";
>   while(<FILE>)
>   {
>    if(/$search_term/i)
>    {
>     $found_match = 1;
>    }
>    if(/<TITLE>/)
>    {
>     chop;
>     $title = $_;
>     $title =~ s/<TITLE>//g;
>     $title =~ s/<\/TITLE>//g;
>    }
>   }
>   if($found_match)
>   {
>    push(@found_set, $file);
>    if($title eq "")
>    {
>     $Title{$file} = $file;
>    }
>    else
>    {
>     $Title{$file} = $title;
>    }
>   }
>   close(FILE);
>   print "<P>\n";
>  }
>  closedir(DIR);
> }
> 

> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm

-- 
Quinn Weaver, independent contractor  |  President, San Francisco Perl Mongers
http://fairpath.com/quinn/resume/     |  http://sf.pm.org/


More information about the SanFrancisco-pm mailing list