Opening and Reading a Directory in Perl
opendir(HU, ".") || print "Can't open... maybe try chmod 777";
@hu=readdir(HU);
closedir(HU);
-The period between quotations means the current directory (the directory the script is in).
-You could use a relative or root path to a directory you want to read.
-Your array @hu now contains a list of all the files in the current directory.
NOTES:
-The words HU are called a Filehandle. Always use ALL CAPS for filehandles. The HU could be pretty much anything you like except STDIN and STDOUT, which are reserved for other things.
-The || means
or. We said "or print this error if you can't read the directory."
opendir(HU, "../cities");
The above line would open the directory
cities provided it was one directory up from our script.
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
opendir(HU, ".");
@hu=readdir(HU);
closedir(HU);
foreach $hu(@hu) {
print "$hu<br>";
}
exit;
IN-CASE-YOU-WONDER NOTE: You might notice our script prints a period and a couple more periods. Those symbolize the directories above the one we're in.
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
| | | |
| | | |
| | | HTML Gary B. Shelly, Th... |