Perl File Operations
Perl uses a variable type called a file handle to manipulate files.
To read from or write to a file, a file handle is required.
A file handle (file handle) is the name of an I/O connection.
Perl provides three file handles: STDIN
, STDOUT
, STDERR
, which represent standard input, standard output, and standard error output, respectively.
Files can be opened in Perl using the following methods:
open FILEHANDLE, EXPR
open FILEHANDLE
sysopen FILEHANDLE, FILENAME, MODE, PERMS
sysopen FILEHANDLE, FILENAME, MODE
Parameter descriptions:
- FILEHANDLE: The file handle, used to store a unique identifier for a file.
- EXPR: An expression consisting of the file name and file access type.
- MODE: The file access type.
- PERMS: The permission bits.
The open Function
The following code uses the open function to open the file file.txt
in read-only mode <
:
open(DATA, "<file.txt");
<
indicates read-only mode.
In the code, DATA is the file handle used to read the file. The following example will open the file and output its contents:
Example
#!/usr/bin/perl
open(DATA, "<file.txt") or die "Cannot open file.txt, $!";
while(<DATA>){
print "$_";
}
The following code opens the file file.txt
in write mode >
:
open(DATA, ">file.txt") or die "Cannot open file.txt, $!";
>
indicates write mode.
If you need to open the file in read-write mode, you can add a +
sign before the >
or <
characters:
open(DATA, "+<file.txt") or die "Cannot open file.txt, $!";
This method will not delete the original contents of the file. To delete the contents, the format is as follows:
open DATA, "+>file.txt" or die "Cannot open file.txt, $!";
To append data to a file, simply open the file in append mode before appending:
open(DATA, ">>file.txt") || die "Cannot open file.txt, $!";
>>
indicates appending data to the end of an existing file. If you need to read the contents of the file to be appended, you can add a +
sign:
open(DATA, "+>>file.txt") || die "Cannot open file.txt, $!";
The following table lists different access modes:
Mode | Description |
---|---|
< or r | Open in read-only mode, pointing the file pointer to the beginning of the file. |
> or w | Open in write mode, pointing the file pointer to the beginning of the file and truncating the file size to zero. If the file does not exist, attempt to create it. |
>> or a | Open in write mode, pointing the file pointer to the end of the file. If the file does not exist, attempt to create it. |
+< or r+ | Open in read-write mode, pointing the file pointer to the beginning of the file. |
+> or w+ | Open in read-write mode, pointing the file pointer to the beginning of the file and truncating the file size to zero. If the file does not exist, attempt to create it. |
+>> or a+ | Open in read-write mode, pointing the file pointer to the end of the file. If the file does not exist, attempt to create it. |
The sysopen Function
The sysopen function is similar to the open function, but with a different parameter format.
The following example opens the file in read-write mode (+<filename
):
sysopen(DATA, "file.txt", O_RDWR);
If you need to clear the file before updating it, the format is as follows:
sysopen(DATA, "file.txt", O_RDWR|O_TRUNC);
You can use O_CREAT to create a new file, O_WRONLY for write-only mode, and O_RDONLY for read-only mode.
The PERMS parameter is an octal attribute value representing the permissions of the file after creation, with a default of 0x666.
The following table lists possible mode values:
Mode | Description |
---|---|
O_RDWR | Open in read-write mode, pointing the file pointer to the beginning of the file. |
O_RDONLY | Open in read-only mode, pointing the file pointer to the beginning of the file. |
O_WRONLY | Open in write mode, pointing the file pointer to the beginning of the file and truncating the file size to zero. If the file does not exist, attempt to create it. |
O_CREAT | Create the file |
O_APPEND | Append to the file |
O_TRUNC | Truncate the file size to zero |
O_EXCL | If used with O_CREAT and the file exists, return an error message. It can test whether the file exists. |
O_NONBLOCK | Non-blocking I/O ensures that the operation either succeeds or returns an error immediately without being blocked. |
The Close Function
After using a file, it should be closed to flush the input/output buffer associated with the file handle. The syntax for closing a file is as follows:
close FILEHANDLE
close
FILEHANDLE is the specified file handle. If the file is successfully closed, it returns true.
close(DATA) || die "Cannot close file";
Reading and Writing Files
There are several different ways to read from and write to a file:
The <FILEHANDLE> Operator
The primary method for reading information from an open file handle is the <FILEHANDLE> operator. In scalar context, it returns a single line from the file handle. For example:
Example
#!/usr/bin/perl
print "URL of tutorialpro.org?\n";
$name = <STDIN>;
print "URL: $name\n";
When the above program is executed, it displays the following information, and the print statement outputs the URL after input:
When using the <FILEHANDLE> operator, it returns a list of every line in the file handle, for example, we can import all lines into an array.
Create the import.txt
file with the following content:
$ cat import.txt
1
2
3
Read import.txt
and place each line into the @lines array:
Example
#!/usr/bin/perl
open(DATA, "<import.txt") or die "Cannot open data";
@lines = <DATA>;
print @lines; # Output the array content
close(DATA);
Executing the above program will output:
1
2
3
The getc Function
The getc function returns a single character from the specified FILEHANDLE, or from STDIN if none is specified:
getc FILEHANDLE
getc
If an error occurs or the file handle is at the end of the file, it returns undef.
The read Function
The read function is used to read information from the buffer of a file handle.
This function is used to read binary data from a file.
read FILEHANDLE, SCALAR, LENGTH, OFFSET
read FILEHANDLE, SCALAR, LENGTH
Parameter descriptions:
- FILEHANDLE: The file handle, used to store a unique identifier for a file.
- SCALAR: Stores the result. If OFFSET is not specified, the data is placed at the beginning of SCALAR. Otherwise, the data is placed OFFSET bytes after the start of SCALAR.
- LENGTH: The length of the content to be read.
- OFFSET: The offset.
If the read is successful, it returns the number of bytes read. If it reaches the end of the file, it returns 0. If an error occurs, it returns undef.
The print Function
For all functions that read information from a file handle, the primary writing function in the backend is print:
print FILEHANDLE LIST
print LIST
print
Using the file handle and the print function, you can send the results of the program to the output device (STDOUT: standard output), for example:
print "Hello World!\n";
File Copy
The following example opens an existing file file1.txt
and reads each line of it to write into the file file2.txt
:
Example
#!/usr/bin/perl
# Open the file in read-only mode
open(DATA1, "<file1.txt");
# Open the new file in write mode
open(DATA2, ">file2.txt");
# Copy data
while(<DATA1>)
{
print DATA2 $_;
}
close(DATA1);
close(DATA2);
File Renaming
The following example renames the existing file file1.txt
to file2.txt
, with the specified directory being /usr/tutorialpro/test/
:
#!/usr/bin/perl
rename ("/usr/tutorialpro/test/file1.txt", "/usr/tutorialpro/test/file2.txt");
The rename function only accepts two parameters and only renames existing files.
Deleting a File
The following example demonstrates how to use the unlink function to delete a file:
Example
#!/usr/bin/perl
unlink ("/usr/tutorialpro/test/file1.txt");
Specifying File Position
You can use the tell function to get the current position in a file and the seek function to set the position within the file:
The tell Function
The tell function is used to get the current position in a file:
tell FILEHANDLE
tell
If a FILEHANDLE is specified, it returns the position of the file pointer in bytes. If none is specified, it returns the position of the default selected file handle.
The seek Function
The seek() function moves the file read/write pointer within the file handle in bytes:
seek FILEHANDLE, POSITION, WHENCE
Parameter descriptions:
- FILEHANDLE: The file handle, used to store a unique identifier for a file.
- POSITION: The number of bytes to move the file handle (read/write pointer).
- WHENCE: The starting position for the file handle (read/write pointer) movement, with possible values of 0, 1, 2; representing the beginning of the file, the current position, and the end of the file, respectively.
The following example reads 256 bytes from the beginning of the file:
seek DATA, 256, 0;
File Information
Perl's file operations can also test whether a file exists, is readable, or writable.
First, create the file1.txt
file with the following content:
$ cat file1.txt
www.tutorialpro.org
Example
#!/usr/bin/perl
my $file = "/usr/test/tutorialpro/file1.txt";
my (@description, $size);
if (-e $file)
{
push @description, 'is a binary file' if (-B _);
push @description, 'is a socket' if (-S _);
push @description, 'is a text file' if (-T _);
push @description, 'is a special block file' if (-b _);
push @description, 'is a special character file' if (-c _);
push @description, 'is a directory' if (-d _);
push @description, 'file exists' if (-x _);
push @description, (($size = -s _)) ? "$size bytes" : 'empty';
print "$file information: ", join(', ', @description), "\n";
}
Executing the above program will output:
file1.txt information: is a text file, 15 bytes
The file test operators are as shown in the table below:
Operator | Description |
---|---|
-A | Last access time of the file (in days) |
-B | Whether the file is a binary file |
-C | Inode change time of the file (in days) |
-M | Last modification time of the file (in days) |
-O | File is owned by the real UID |
-R | File or directory can be read by the real UID/GID |
-S | Is a socket |
-T | Whether the file is a text file |
-W | File or directory can be written by the real UID/GID |
-X | File or directory can be executed by the real UID/GID |
-b | Is a block special file (e.g., mounted disk) |
-c | Is a character special file (e.g., I/O device) |
-d | Is a directory |
-e | File or directory name exists |
-f | Is a regular file |
-g | File or directory has the setgid bit set |
-k | File or directory has the sticky bit set |
-l | Is a symbolic link |
-o | File is owned by the effective UID |
-p | File is a named pipe (FIFO) |
-r | File can be read by the effective UID/GID |
-s | File or directory exists and is not zero size (returns byte count) |
-t | File handle is a TTY (result of isatty(); cannot be used on file names) |
-u | File or directory has the setuid bit set |
-w | File can be written by the effective UID/GID |
-x | File can be executed by the effective UID/GID |
-z | File exists and has a size of zero (always false for directories), i.e., whether it is an empty file |