1. First example
Perl takes files as arrays and not strings.
Perl installation comes with modules. They are additional packages
we may import into the Perl environment. For example the module
related to files is "File::Finf". We call as:
use File::Find;
Example:
--------
use File::Find;
$dir = "C:/www/PERL/CGI/counter";
#The directory where to find files
find (\&fun, $dir);
#The fontion fun takes 2 parameters: A subroutine
# and the directory.
sub fun() {
print "The name of the file is $_\n\ Its full path is $File::Find::name\n";
}
2. Second example
To See if a directory Exists in Perl, we use:
if (-d "searched_directory") {
print "There is a directory searched_directory .";
}
else {
print "There is a no such directory.";
}
3. Third example
To check for existing file in Perl, we use:
if (-e "a_directory/file.cgi") {
print " This file exists.";
}
else {
print "This file does not exist.";
}
4. Fourth example
# Program to make substitution within a file .
use File::Find;
$dir = "C:/www/PERL/tests";
find(\&a_function, $dir);
sub a_function() {
$count = 0;
if ( -f and /.html?/ ) {
# Is there any file.html? If it is true then continue
$file = $_;
open FILE, $file;
@lines = ;
close FILE;
for $line ( @lines ) {
if ( $line =~ s/Example/INSTANCE/ ) {
# Substitute "INSTANCE" to "Example"
$count++;
}
}
open FILE, ">$file";
print FILE @lines;
close FILE;
}
}
print "$count";
# Noe check the file.html ...
|