To work with files and directories in Perl, you have to understand filehandles. A filehandle is a named internal structure which is associated with a file name. It is used to perform read/write/update operations on files.
The basic filehandles are
STDIN
for standard input,
STDOUT
for standard output, and
STDERR
for standard error devices.
Open Function
To open a file in read-only mode, < symbol is used with file
name. In the following example,
DATA
is the file handle.
In this context to print the content of the file on terminal, a
loop can be used to iterate over
DATA
as shown below.
To open sample.txt in writing mode, > symbol is used before the file name as shown below. Note that, in this mode the file is truncated (previous content is deleted).
If a file needs to be opened for both reading and writing, the plus sign (+) needs to be added before the > or <.
To open a file for updation without truncation, open the file as
To truncate the file, you can use
To open a file in append mode (file pointer is set at the end), you can use the follwing.
But in this case, read operation can not be performed.
To perform append along with read operation, you can use
Sysopen Function
In this function, the parameters are passed to it as the parameters
for the system function. To open a file for updation, instead of
using
+<
, the file is opened as
or to truncate the file before updation
To create a new file
O_CREAT
can be used. Similarly,
O_WRONLY
and
O_RDONLY
can be used to open file in write only mode and read only mode
respectively.
Close Function
To close a filehandle, and therefore disassociate the filehandle from the corresponding file, you use the close function. This flushes the filehandle's buffers and closes the system's file descriptor.
The function
close(FILEHANDLE)
is used to close a specific filehandle, by flushing buffer and
closing filedescriptor.
If filehandle is not specified, the currently selected filehandle is closed.
Reading and Writing Files
The content of a file can be read by using a filehandle operator as shown below.
To import content of a file having many lines, we can try the following program.
Copying Files
The following program can be used to open an existing file, read the content line by line and copy the content in another file.
Renaming a file
The function
rename
can be used to rename a file as shown below.
Deleting an Existing File
The
unlink
function can be used to delete an existing file.
Positioning inside a File
tell Function
The
tell
function, returns current position (in Bytes) of the cursor within
a file.
seek Function
The
seek
function is used to move the cursor to a specific location within
the file
Here zero (0) sets the positioning relative to the start of the file.
File Information
To get file permission related information, we can run the following code.
Following are the standard functions used to play with directories.
Display all the Files
To get list of files available in a particular directory, the
glob
operator is used as shown below.
The following program shows the list of files available inside a directory.
Create Directory
The
mkdir
function is used to create a new directory.
Remove a directory
The
rmdir
function is used to remove a directory from file system. The user
should have proper permission, moreover the directory should be
empty.
Change a Directory
To move to a new directory location,
chdir
function is used. Note that the user should have proper permission.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.