Tutorial Addendum on Perl - Allotment A - Account Agenda Entries
| |
Displaying Agenda Timberline - DirTree.pl
Both Unix and Windows systems adapt book directories into a timberline structure.
The afterward program. DirTree.pl, shows you how to bisect the agenda tree,
and affectation agenda entries:
#- DirTree.pl
#- Absorb (c) 1995 by Dr. Yang
#
($dir) = @ARGV;
$dir = "." unless $dir;
&loopDir($dir, "");
exit;
sub loopDir {
local($dir, $margin) = @_;
chdir($dir) || die "Cannot chdir to $dir
";
local(*DIR);
opendir(DIR, ".");
while ($f=readdir(DIR)) {
next if ($f eq "." || $f eq "..");
if (-d $f) {
&loopDir($f,$margin." ");
}
}
closedir(DIR);
chdir("..");
}
Be careful, don t try this program in the basis directory. It will aftermath
a actual actual continued account of files and directories. I approved it on the working
directory area I stored my Perl addendum and programs, and I got the following:
>DirTree.pl ..
htm
about.html
active_perl.html
book.css
book_fo.xsl
dot.gif
help.html
open.html
opendir.html
reference.html
toc.html
...
src
DirTree.pl
hello.pl
hello.prg
opendir.pl
...
If you analysis DirTree.pl, you will see some absorbing statements and techniques:
- " $dir = "." unless $dir;" is acclimated to accept a absence directory, if annihilation
specified on the command line.
- " local($dir, $margin) = @_;" is acclimated to create $dir and $margin as bounded variables.
This actual important, back loopDir() is alleged recursively.
- " local(*DIR);" is acclimated to create DIR as bounded capricious in any name spaces,
including agenda handle name space. In fact, this is the alone way to create
directory handle names local.
- " if (-d $f) {" is acclimated to analysis the agenda access to see if it is a
directory.
Counting Files - DirCount.pl
My additional of archetype of traversing agenda timberline is DirStats.pl. It collects several
statistics of the defined agenda tree:
#- DirCount.pl
#- Absorb (c) 1995 by Dr. Yang
#
($dir, $extension) = @ARGV;
$dir = "." unless $dir;
$extension = "pl" unless $extension;
$dirCount = 0;
$fileCount = 0;
$fileSize = 0;
$otherCount = 0;
$otherSize = 0;
&loopDir($dir);
book "Number of directories = $dirCount
";
book "Number of files with .$extension = $fileCount
";
book "Total admeasurement of files with .$extension = $fileSize
";
book "Number of additional files = $otherCount
";
book "Total admeasurement of additional files = $otherSize
";
exit;
sub loopDir {
local($dir) = @_;
chdir($dir) || die "Cannot chdir to $dir
";
$dirCount++;
local(*DIR);
opendir(DIR, ".");
while ($f=readdir(DIR)) {
next if ($f eq "." || $f eq "..");
if (-d $f) {
&loopDir($f,);
} elsif ($f=~/.$extension$/) {
$fileCount++;
$fileSize += -s $f;
} abroad {
$otherCount++;
$otherSize += -s $f;
}
}
closedir(DIR);
chdir("..");
}
The first run is on my Perl addendum alive directory. The achievement shows that I have
5 Perl files in 5 directories with absolute admeasurement of 8437 bytes. And there are some
other files in these directories:
>DirCount.pl ..
Number of directories = 5
Number of files with .pl = 5
Total admeasurement of files with .pl = 8437
Number of additional files = 44
Total admeasurement of additional files = 115580
Then I run my DirCount.pl on the Windows arrangement agenda to calculation how some executable
files do we accept the system:
>DirCount.pl c:winnt exe
Number of directories = 353
Number of files with .exe = 975
Total admeasurement of files with .exe = 111390120
Number of additional files = 10218
Total admeasurement of additional files = 1376214106
|
files, directory, local, loopdir, directories, dircount, print, extension, dirtree, chdir, margin, unless, notes, opendir, total, entries, , files with, & loopdir, directory tree, print number, total size, local *dir, local dir, unless dir, directory entries, dir unless, print total size, files with extension, local *dir opendir, reading directory entries, |
Also see ...
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
Special Subroutines for Accumulation and Beheading ProcessesIn a Perl antecedent cipher file, you can ascertain 4 appropriate subroutines, which will be accomplished automatically by the ac