Yoshiro has asked for the
wisdom of the Perl Monks concerning the following question:

Greetings Monks

I am stuck with another problem working with server. I'm making a cgi script to search for a word in html files located within my server file structure.

Im using the xampp 1.7.7 running Apache. In my htdocs directory i created a new one called 'myDir' in which i will have a few html files. I am trying to create a cgi script in which i use file::find. I read the cpan article and i understand that since i'm doing the search in one specific directory i need to replace the @directories_to_search array with just my directory name so it looks like this:

find(\&wanted, '/myDir/');

The problem is that when i use the full disk path the script finds the files but not when i use the short localhost path as shown below:

http://localhost/myDir/ simply is not a directory name on your file system, which is why File::Find fails. HTTP and file systems are different - I recommend to learn the difference and use the approach that you already found, using the absolute directory name.

One thing to add: it should be possible to use a relative pathname, but that would be 'myDir', not '/myDir'. Starting a pathname with a slash makes it an absolute pathname, so it looks for myDir in the root filesystem (whatever that means on Windows; maybe C:\myDir?). A relative pathname is looked for inside your current working directory, so if your CGI runs from C:/www/htdocs, then passing 'myDir' to find() should cause it to look in C:/www/htdocs/myDir.

If that doesn't work, you'll need to figure out in what current working directory your CGI does run. Or use chdir to switch to the right directory before calling find(). Or use the absolute pathname, as Corion pointed out and you've already discovered works fine.

Your approach assumes that the "current working directory" is predictable. It usually isn't, so using absolute path names eliminates one more point of guesswork when debugging a (new) server environment.

I understand now, i did some reading and found out about documentroot on Apache. Seems the way i did it specifying the full path is good.

My mistake is that i was creating the link badly using just the $File::Find::name variable. Instead now use the $_ to create a good HTTP address or could extract $DocumentRoot from $File::Find::name if there are more subdirectories in 'myDir', again to create valid HTTP addresses.