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

Hey guys,
Ok, all I want to be able to do is go to a directory on my server, using a cgi script, to search a list of files.
The dir is determined by a form the user fills out. I wat to search the files and then return data. What command will open the dir?
Here is some of my code:
#!/usr/bin/perl print "Content-type: text/html\n\n"; require("cgi-lib.cgi"); &ReadParse; @stuff = ""; $i = 0; foreach $key (keys %in) { @stuff[$i] = "$in{$key}"; $i = $i + 1; } chomp @stuff; $site = @stuff[4]; $year = @stuff[1]; $month = @stuff[5]; $day = @stuff[6]; $star = @stuff[2]; $var = @stuff[0]; $date = $year . $month . $day; $dir = $site . $date; #opendir DIR, "/www2/hardwiredsolutions.com/concam/$site/$dir" or die +"\nError opening directory"; @filecheck = <*.txt>; foreach $file (@filecheck) { open FILE, "$file" or die "\nError reading file(1)"; @lines = <FILE>; close FILE; open FILE, "$file" or die "\nError reading file(2)"; foreach $line (@lines) { @time = unpack "a26a4a2a*", $line; @words = unpack "a6a7a5a4a4a3a6a6a10a9a10a*", $line; s/^\s+// for @time; s/^\s+// for @words; if (@time[1] eq "GST") { chomp @time[3]; print "@time[3]"; } if (@words[1] eq $star) { print "@words[$var+1]<br>"; } } } print <<WEB_PAGE; <html> <title>Graph the Night Sky!</title><h1>Graph:</h1><hr> You researched $star, on $date, at $site, for $var. </html> WEB_PAGE
The files I need to access are in a directory like this: kp/kp021020/files_to_search. The dir depends on which file the user enters in the form.
Thanx as always,
Dushu

Replies are listed 'Best First'.
Re: open dir
by emilford (Friar) on Nov 07, 2002 at 17:28 UTC
    First off, you could have done a search for opendir and you would have found exactly what you needed. I didn't look through your code, but if all you want to do is read in the file contents of a directory, you could do something like this:
    # create a filehandle to the directory - check for errors opendir (DIR, "path/to/directory") or die "Unable to open directory: $ +!"; # read in all file names except . and .. @files = grep {$_ ne "." && $_ ne ".."} readdir (DIR); # close the filehandle to the directory closedir (DIR); # do whatever you want with filenames in @files
Re: open dir
by tachyon (Chancellor) on Nov 07, 2002 at 17:31 UTC
    # you can do this $dir = 'c:\\'; opendir DIR, $dir or die "Can't open $dir perl says $!\n"; while ( $file = readdir DIR ) { next unless $file =~ m/\.txt$/i; print "Found *.txt file: $file\n"; } # or this $dir = 'c:\\'; opendir DIR, $dir or die "Can't open $dir perl says $!\n"; my @files = grep { /\.txt$/i } readdir DIR; print "$_\n" for @files; # or this $dir = "c:\\*.txt"; @files = glob $dir; print "$_\n" for @files;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: open dir
by samurai (Monk) on Nov 07, 2002 at 18:15 UTC
Re: open dir
by fglock (Vicar) on Nov 07, 2002 at 17:51 UTC
    require("cgi-lib.cgi"); &ReadParse;

    Change this to  use CGI; if you can. It is safer, and more standards compliant.