the script is supposed to be run in a cron job to check the directory one step above itself
In fact, it checks the directory one step above the current directory of the cron process. If you need the script to start running in a specific directory, then put a 'chdir("/pathname")' at the beginning of the script.
Alternatively, just open the directory you want to open: opendir("/pathname")
Or, more simply, don't open it at all - use Perl's glob operator in your loop:
foreach my $file (</pathname/*>) {
if( ($file ne "index.html") && ($file ne ".")
&& ($file ne "..") && ($file ne "cgi-bin") ) {$files = 1}
}
Also, you might as well break out of the loop once you've set $files to 1 - it's never going to change on subsequent iterations. In fact, you don't really need a loop at all. Try this:
my @files = grep(!/^(\.\.?|index\.html|cgi-bin)$/, </pathname/*>);
if(@files) {
# send email
}