[Linux] Perl for Dummies

sam @ www sam@www-inc.com
Wed, 30 Aug 2000 18:44:50 -0400


My first script and it won't do what I would expect (of course).

Here's the directories the scipts is working on:

[sam@lin2m testing]$ l *
-rwxrwxr-x   1 sam      sam          1012 Aug 30 11:01 dirdig.pl
-rw-rw-r--   1 sam      sam            54 Aug 28 15:10 file1

dir1:
total 8
drwxrwxr-x   2 sam      sam          4096 Aug 30 10:44 dir1.1
drwxrwxr-x   2 sam      sam          4096 Aug 30 10:45 dir1.2
-rw-rw-r--   1 sam      sam             0 Aug 30 10:45 file1.1

dir2:
total 0

dir3:
total 0


***HERE's the scipt.
## The first chunk make an array of all directories in the same directory as
the script.

$FileOrDirName = "";
@DirList = ();
opendir(topDIR,'.');
@AllFiles = readdir(topDIR);         #Array of all files and directories in
top directory
print "in the directory \"Root for script\"\n";       #Debugging stuff
foreach $FileOrDirName (@AllFiles) {
  if ($FileOrDirName =~ /^\./) {     # Ignore files and directories starting
with "."
  }
  elsif(-d $FileOrDirName) {      #Tests if item is directory
  print "\"$FileOrDirName\" is a directory\n";    #Debugging stuff
   push (@DirList, $FileOrDirName);       #Array of directories only in top
directory
   }
else {
  print "\"$FileOrDirName\" is not a directory\n";    #Debugging stuff
  }
}

### Now this attempt to do the same thing to the first directory in array
(dir1)

$FileOrDirName = "";
@AllFiles = ();
opendir(secondDIR, $DirList[2]); #List of all files and directories in first
second level directory
@AllFiles = readdir(secondDIR);
print "the array for \"$DirList[2]\" \= @AllFiles\n";   #Debugging stuff
print "in the directory \"$DirList[2]\"\n";    #Debugging stuff
foreach $FileOrDirName (@AllFiles) {  #Checks each item in array
        if(-d $FileOrDirName) { #Asks if item is directory
  print "\"$FileOrDirName\" is a directory\n";
 }
else {
  print "\"$FileOrDirName\" is not a directory\n";
  }
}


Now notice in the output how the (-d) test fails to recognize directories,
dir.1, dir1.2

[sam@lin2m testing]$ perl dirdig.pl
in the directory "Root for script"
"dir3" is a directory
"dir2" is a directory
"dir1" is a directory
"file1" is not a directory
"dirdig.pl" is not a directory
the array for "dir1" = . .. dir1.1 dir1.2 file1.1
in the directory "dir1"
"." is a directory
".." is a directory
"dir1.1" is not a directory
"dir1.2" is not a directory
"file1.1" is not a directory

I didn't filter out the "." files to show that it does recognize those as
directories.

How should this be done?

thanks
sam