[Za-pm] behaviour of opendir

Oskar Pearson oskar at qualica.com
Wed Aug 6 07:25:16 CDT 2003


Hi there


> #!/usr/bin/perl
> 
> print "usual headers";
> 
> $input = shift;
> $path = "some directory"; # or a value passed from something else
> 
> opendir(DIR, $path) || die();
> @folder=readdir(DIR);
>   foreach $entry (@folder) {
>   print "<a href=some.cgi?string>$entry</a><br>";
>   }
> 
> So far as I can see, there's nothing wrong with it and it usually works
> fine. Now, I've added a couple new directories to the $path folder and
> opendir (or, maybe, readdir) fails to see them and insists in printing
> the older list of folders in $path.


> Ownerships and privileges are fine
> and the new directories are seen from the shell and the desktop file
> manager. This happens under a browser environment as well as while
> testing from the shell. Using a different function (open pipe) works
> fine so that the following snippet prints all the directories (including
> the new ones):

A couple of thoughts here. First off, the code looks fine - it shouldn't
be doing strange things like this.

Normally I do something like this:
#!/usr/bin/perl -w
use strict;
my $path = "/tmp";
opendir(DIR, $path) || die("Cannot opendir: $!");
foreach my $entry (readdir(DIR)) {
        print "$entry\n";
}


So, I think the reason you've not got any answers (at least on the list)
is that something is very strange :)

Here are some possibilities:

1) are you doing this in an nfs mounted directory? I've seen caching
that sometimes returns old results in NFS. This would explain why you
get it in all cases (not just from a web page).

2) Are you doing this in mod-perl? If so, it's perhaps possible that
one or more variables isn't being cleared out when you hit the page
the second time. In the simple example above this is less likely,
but if it's just a "for example case" then perhaps this is the problem.
You might want to try giving everything a defined scope, and using
variables to hold the directory name:

#!/usr/bin/perl -w

use strict;
my $path = "/tmp";

{
	# scoped to ensure limited lifetime
	my $dir;
	opendir($dir, $path) || die("Cannot opendir: $!");
	foreach my $entry (readdir($dir)) {
		print "$entry\n";
	}
}


Please let us know the results!

Oskar



More information about the Za-pm mailing list