Perl Script to Read Files in Directory

They have: 40 posts

Joined: Oct 2001

I am trying to use a simple perl script to list the contents in a directory. The script is below. It tells me there's an error in line 4. The path to the directory is correct. Is it something to do with the directory being the cgi-bin directory? If so, is there some way to get around this without moving the files? Thanks.

#!/usr/bin/perl
use CGI::Carp 'fatalsToBrowser';
$dirtoget="/fishthenet-www/cgi-bin";
opendir(IMD, $dirtoget) || die("Cannot open directory");
@thefiles= readdir(IMD);
closedir(IMD);

print "Content-type: text/html\n\n";
print "";

foreach $f (@thefiles)
{
unless ( ($f eq ".") || ($f eq "..") )
{
print "$f";
}
}

print "";

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm pretty sure that it requires the full path, not from the root web, but the UNIX root.

They have: 601 posts

Joined: Nov 2001

Yeah, I go with what Mark said.

However, if you would of taken advantage of the Perl built-in error reporter (for the lack of a better word ) it would of told you this. Ammend the following line to see what the error is:

opendir(IMD, $dirtoget) or die("Cannot open directory. $!");

Notice the $!. That's the special Perl variable that holds your error message, and I bet you $5 virtual dollars it will tell you exactly what Mark said above Smiling

- wil

They have: 40 posts

Joined: Oct 2001

Thanks Mark and Wil,

I did what you said (Wil) and the error message is now "no such file or directory". So I guess you're right. But I don't understand the difference between the root web and the unix root. I guess I have the root web in the script. How do I know/find the unix root?

They have: 601 posts

Joined: Nov 2001

Your web root, is the path the web browser sees after your domain name. Here's an example:

http://www.domain.com/foo/bar/

The web path would be /foo/bar/, but on my machine, my web directory is mapped to /home/wil/domain.com/ so the unix path would be /home/wil/domain.com/foo/bar/.

Now, you can either ask your web hosting provider (possibly thebest idea!) what your unix working root is, or if you have telnet access, loginto your account and when logged in, type pwd at the command prompt. This should tell you where you are on the system, and just copy and paste that.

Cheers!

- wil

They have: 40 posts

Joined: Oct 2001

Thanks Wil,

I'll ask the isp, and write back if I have any more problems.

cheers,...

They have: 601 posts

Joined: Nov 2001

Good luck! Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.