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)) {
}
closedir(DIR);
book "Reading all entries into an array...
";
opendir(DIR,".");
@a = readdir(DIR);
closedir(DIR);
for (@a) {
}
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.
|
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 ...
Displaying Agenda Timberline DirTree.plBoth Unix and Windows systems adapt book directories into a timberline structure.The afterward program. DirTree.pl, shows you how to bisect the agen
Searching Files DirGrep.plMy next program, DirGrep.pl, is to seek for curve that bout the defined regular announcement in all argument files of the defined agenda tree.
Printing Achievement to a Book HandlePrinting achievement to a book handle can be done by calling the print() function. Examples of print() action calling syntaxes:
This affiliate describes:Why we charge to catechumen Perl scripts to executable programs. How to install Perl Dev Kit and use it to catechumen Perl scripts.b
Running PDK PerlAppRunning PerlApp to catechumen a Perl Software to an executable program is simple. Let yield my DirGrep.pl as an example: DirGrep.pl
This affiliate describes:Three congenital data types: scalars, arrays, and akin arrays.How to assemble scalar objects.How scalar altar are interpreted in operations.
Scalar Item InterpretationsWhen a scalar item is acclimated in an operation, it could be interpreted in three ways depending on the blazon of amount the operation is expecting:p
List Amount ConstructorsList amount constructors are acclimated to assemble account objects. A account item is about identical to an arrangement object. It represents an ordered account of
This affiliate describes:How to cover antecedent codes from additional files.How to ascertain and use name spaces.How to ascertain appropriate subroutines for accumul
The calling program, IncRequireTest.pl, IncRequireTest.pl Absorb (c) 1995 by Dr. Yang print(" Testing require()... "); require("MyRequireLib.inc"); require("MyRequireL