In PERL files are given a name, also called handle.
A D V E R T I S E M E N T
All the input and output of the file is
achieved by filehandling functions. Filehandles are also a means to communicate from one program
to another program.
How to assign handles
A filehandle is nothing but name given for the files which you intend to use in your
PERL programs and scripts. A handle is a name which is temporarly assigned to a file.
The example below shows how to use a file handle in your PERL program.
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$FilePath = "home/html/myhtml.html"
sysopen(HANDLE, $FilePath, O_RDWR);
printf HANDLE "Welcome to Tizag!";
close (HANDLE);
Files with the die Function
The die function also exists in several other programming languages. It is used to
kill your scripts and also helps to pinpoint where/if your code is failing. We use this function as
as shown below.
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$filepath = "htmlpage.html";
sysopen (HTML, '$filepath', O_RDWR|O_EXCL|O_CREAT, 0755) or die "$filepath
cannot be opened.";
printf HTML "<html>\n";
printf HTML "<head>\n";
printf HTML "vtitle>My Home Page</title>";
printf HTML "</head>\n";
printf HTML "<body>\n";
printf HTML "<p align='center'>Here we have an HTML
page with a paragraph.</p>";
printf HTML "v/body>\n";
printf HTML "</html>\n";
close (HTML);
If due to some problem PERL is unable to open or create our file, we will be informed.
It is good practice to make use of the die function and we will be using it as we go
deeper into the file handling.
How to Open the File
Files can be opened using either of open and sysopen function.
for either of the function can pass upto 4 arguments, the first argument is always the
file handle, then the file name also known as a URL or filepath, flags, and
finally any of the permissions that are to be granted to the file.
The following program opens up a previously saved HTML document.
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$FH = "filehandle";
$FilePath = "htmlpage.html";
open(FH, $FilePath, permissions);
or
sysopen(FH, $FileName, permission);
Files which are having unusual file names or special characters are all best opened by
declaring the URL first, as a variable. This method removes the confusion that might
occur when PERL tries to interpret the program. However, filenames require a step for a
brief character substitution before which they can be placed into the open statements.
Various File Permissions
File permissions are crucial to the file function and security. For instance, in order to
function, a PERL file (.pl) must have executable file permissions in order to function
on your web server. Also, you may not want all of your HTML files to be set to allow
others to write to them or over them. Here's a listing of what to pass to the open
function when working with file handles.
Shorthand Flags:
Entities Definition
< or r Read Only Access
> or w Creates, Writes, and Truncates
>>or a Writes, Appends, and Creates
+< or r+ Reads and Writes
+> or w+ Reads, Writes, Creates, and Truncates
+>> or a+ Reads, Writes, Appends, and Creates
O_ Flags:
Value Definition
O_RDWR Read and Write
O_RDONLY Read Only
O_WRONLY Write Only
O_CREAT Create the file
O_APPEND Append the file
O_TRUNC Truncate the file
O_EXCL Stops if file already exists
O_NONBLOCK Non-Blocking usability
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
use Fcntl; #The Module
Files are opened and created using the same function "sysopen".
Our syntax open(FILEHANDLE, '$filename', permissions, CHMOD);
or sysopen(FILEHANDLE, $filename, permissions, CHMOD);
#!/usr/bin/perl
use Fcntl; #The Module
print "content-type: text/html \n\n"; #The header
sysopen (HTML, 'myhtml.html', O_RDWR|O_EXCL|O_CREAT, 0755);
printf HTML "<html>\n";
printf HTML "<head>\n";
printf HTML "<title>My Home Page";
printf HTML "</head>\n";
printf HTML "<bodyv\n";
printf HTML "<p align='center'>Here we have an HTML
page with a paragraph.</p>";
printf HTML "</body>\n";
printf HTML "</html>\n";
close (HTML);
With sysopen you can also set hexadecimal priviledges; CHMOD values. Sysopen needs
the declaration of a new module for PERL. We will now be using the Fcntl module.
Reading from a File
It is easy to read lines from files and then input them using the input operator <>. By
placing the file handler inside the input operator, then your script will input that line
of the file..
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$HTML = "htmlpage.html";
open (HTML) or die "Can't open the file!";
print <HTML>;
close (HTML);
Here we have a small PERL script to display several lines of HTML code. Each line is
stored into an array and it is automatically printed to the browser in HTML format using
the input operator <> .
Copy a File
Using the copy function we can duplicate the file. Copy takes two arguments, the URL of the
file which needs to be copied and the URL of the new file/directory to which the file is
to be copied.If the same file name is used
or the same URL, PERL rewrite over the file if permissions is allowed.
#!/usr/bin/perl
use File::Copy;
print "content-type: text/html \n\n"; #The header
$filetobecopied = "htmlpage.html.";
$newfile = "html/htmlpage.html.";
copy($filetobecopied, $newfile) or die "File cannot be copied.";
Here, we have simply duplicated the "htmlpage.html" file and will be using it in future
examples. While using PERL on the web, it is best to use the complete internet URL.
We have used a shorthand way in the example, but it is better to hardcode the full URL
like: http://www.vyom.co.in/htmlpage.html.
Moving the Files
Moving the file requires the use of the "move" function. This function works similarly
to the copy function from above and we send the same module to PERL. The difference is here is,
instead of copying we just 'cut' the file and send it to a new location. This functioning is
same as cutting and pasting text from office document to another.
Our file has now been completly removed from its present location to the
new location.
Deleting the Files
To delete specific files from your web server use the "unlink" function. The best
way is often to set a variable name equal to URL of the file you wish to delete.
#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
$file = "newtext.txt";
if (unlink($file) == 0) {
print "File deleted successfully.";
} else {
print "File was not deleted.";
}
Removing Multiple Files at Once
To remove multiple files at once, we should first create an array of files that has to
be deleted and then loop through each one. There are many other ways to go about this
process.