• 1

    ..

  • 2

    ...

  • 3

    ...

Monday, 2 November 2015

PERL TUTORIAL PART 4 - Working with files

FILE HANDLING


     The basics of handling files are simple, just associate a filehandle with an external file and then use a variety of operators and functions within Perl to read and update the data stored within the data stream associated with the filehandle. The filehandle is the name for an I/O connection between the Perl process and the outside world.
     A filehandle is a named internal Perl structure that associates a physical file with a name. The mode of operation must be specified for the filehandle is opened.
     Three basic file handles are - STDIN, STDOUT, and STDERR which represent standard input, standard output and standard error devices respectively. Apart from these, any number of filehandles can be added in the code.
The different file operations are:
1) Opening a file
2) Closing the file
3) File Reading
4) File Writing
5) File tests

1) Opening a file

The syntax for opening a file is as follows:
open(FILEHANDLE,"<mode>file_name");
or simply
open(FILEHANDLE,"mode","file_name");
where mode specifies the type of file access.
MODE SYMBOL
ACCESS TYPE
< 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
Most commonly used are <, >, >>.
It is a good practice to use capital letters for filehandles. And for safe operation, it is always recommended to use the DIE(....) function for file open which will terminate the program when it fails to open the file.
open(FILEHANDLE,"mode","file_name") or die "Unable to open the file(file_name)";

Example:
open(FH1R,"<","file1.txt") or die "Unable to open the file(file1.txt) in read mode!";
open(FH2W,">file2.txt") || die "Unable to open the file(file2.txt) to write!";
$logfilename="log.txt"
open(LOG,">>",$logfilename) || die "Unable to open the file(log.txt) in append mode!!!";

2) Closing the file

To close a file simply call the CLOSE(...) function with the filehandle as input. All the filehanldes are automatically closed when the Perl program terminates. The syntax is
close(FILEHANDLE);

3) Reading from a file

To read the contents from the file, <> operator is used, just like STDIN.
Example:
$line_1=<FH1R>; #reads only one line
@lines=<FH1R>; #reads the entire file, & places in the array

4) Writing to a file

Writing data to a file is simply like printing to STDOUT, except the filehandle is used between the print keyword & data.
Example:
print FH2W "Hello!! This is 1st line...\n";

5) File testing

File tests are performed on file handles to determine the file properties. For example –e FileHandle returns true if the file pointed by FileHandle exists in the system. The below table shows the different file test that can be performed in Perl. Note that the CAPITAL and small letter tests produces different results.
PARAMETER
DESCRIPTION
-r
File or directory is readable
-w
File or directory is writable
-x
File or directory is executable
-o
File or directory is owned by user
-R
File or directory is readable by real user, not effective user
(differs from -r for setuid programs)
-W
File or directory is writable by real user, not effective user
(differs from -w for setuid programs)
-X
File or directory is executable by real user, not effective user
(differs from -x for setuid programs)
-O
File or directory is owned by real user, not effective user
(differs from -o for setuid programs)
-e
File or directory exists
-z
File exists and has zero size (directories are never empty)
-s
File or directory exists and has nonzero size (the value is the size in bytes)
-f
Entry is a plain file
-d
Entry is a directory
-S
Entry is a socket
-p
Entry is a named pipe (a "fifo")
-t
isatty() on the filehandle is true
-T
File is "text"
-B
File is "binary"
-c
Entry is a character-special file
-M
Modification age in days
-A
Access age in days
-C
Inode -modification age in days

Example:
if (-e FH1)
print "File FH1 exists\n";
if (-T FH1)
print "File FH1 exists & is a Text file\n";
$size= -s FH1;
print "File size is $size bytes...\n";
We welcome your valuable comments and suggestion.
It helps us to do better in future.
Thank you.

Search Here...