Tutorial Addendum on Perl - Allotment A - Account Agenda Entries
This affiliate explains:
- How to accessible a agenda and apprehend its entries.
- How to bisect a agenda tree.
- A simple argument seek program.
Directory Accessing Functions
1. opendir() - A action to accessible the defined agenda and accessory it
to a agenda handle with the afterward syntax:
rc = opendir(dir_handle, expression);
where "expression" specifies the aisle name of the agenda to be opened, and
"dir_handle" is the capricious name of the new agenda handle. opendir() allotment
true, if operation is successful.
Directory handle has its own name space. Aforementioned name can co-exist in altered name spaces.
2. readdir() - A action to admission the agreeable of the defined agenda handle.
If the action is alleged in a scalar context, it will acknowledgment the next entry.
If the action is alleged in an arrangement context, it will acknowledgment the blow of entries.
Examples of calling readdir() in scalar and arrangement contexts:
$s = readdir(dir_handle);
@a = readdir(dir_handle);
3. closedir() - A action to abutting the agenda associated with the specified
directory handle.
closedir(dir_handle);
Sample Program - opendir.pl
The afterward sample program, opendir.pl, opens the accepted directory
twice. The first time, it reads the agenda one access at a time.
The additional time, it reads all entries of the agenda to an array.
#- opendir.pl
#- Absorb (c) 1995 by Dr. Yang
#
book "Reading one access at a time...
";
opendir(DIR,".");
while ($f=readdir(DIR)) {
book "$f
";
}
closedir(DIR);
book "
";
book "Reading all entries into an array...
";
opendir(DIR,".");
@a = readdir(DIR);
closedir(DIR);
for (@a) {
book "$_
";
}
exit;
Now run this program by entering the program book name anon in a command window,
and you will get the afterward output:
>opendir.pl
Reading one access at a time...
.
..
hello.pl
hello.prg
opendir.pl
Reading all entries into an array...
.
..
hello.pl
hello.prg
opendir.pl
Note that:
- Like Unix system, a Windows book agenda has two appropriate entries,
"." and ".." apery accepted agenda and ancestor directory.
|
Tags: program, array, handle, reading, directory, entries, sample, entry, print directory, handle, opendir, entries, readdir, print, function, array, program, closedir, reading, entry, following, , dir handle, readdir dir, directory handle, one entry, closedir dir, directory and, opendir dir, sample program opendir, reading directory entries, |
Also see ...
PermalinkArticle In : Computers & Technology - perl