Perl was designed to control files and I/O operations effectively. Perl main advantage is in file parsing and handling the files. There are several inbuilt functions and operators used throughout file handling in Perl. Basically, file operations are performed with Perl is done using FILE HANDLE. we have to outline this FILE HANDLE throughout the opening a file for either read or write.
Open File
By using the open() function which are available in Perl is as shown below.
[code]
open(FILEHANDLE, "filename or complete path of the file");
[/code]
Read & Write File
Perl Have several modes which are used to read or write and append a file the snippet below demonstrates the Read, write and append as shown below.
[code]
Read – open(FH,"<filename or complete path of the file");
Write – open(FH,">filename or complete path of the file");
Append – open(FH,">>filename or complete path of the file");
[/code]
The image below demonstrates the file read operation as shown below.
The code below demonstrates to creating a file name as file.txt which have several lines of text in it user need to open this file and print as same.
[perl]
open(FH,"<file.txt");
my @content=<FH>; # specifying the input of the array is FH.
foreach(@content)
{
print "$_";
}
close FH;
[/perl]
Now user need to write a program to create and write data to a Perl file is as shown in below image.
The code below demonstrates to write input during the run-time and creates a file test.txt as shown.
[perl]
open(FH,">test.txt");
my $var=<>;
print FH $var;
close FH;
[/perl]
After creating the file and writing data user can append the file is a shown below image.
The table below demonstrates the different modes and its description.
Modes |
Description |
$_$ARG |
newline. |
< |
Read |
+< |
Reads and Writes. |
> |
Which creates, writes and truncates. |
+> |
Which Read, creates, writes and truncates. |
>> |
Which write, appends, creates. |
+>> |
Which Read, write, appends, creates. |
The code below demonstrates to append the file as shown below.
[perl]
open(FH,">>test.txt");
my $var=<>;
print FH $var;
close FH;
[/perl]
Perl Tell
The Perl method is used to return the current position of the FILEHANDLER in bytes which will be considered last line as the position is as shown in below snippet.
[perl]
open(FH, "test.pl");
while(<FH>)
{
$a=tell FH;
print "$a";
}
[/perl]
Perl Seek
Seek function is similar to the seek system call. This method is used to position the file pointer to a particular location by specifying the bytes followed by either start of the file pointer or end of the file pointer. The syntax below demonstrates the Perl Seek in which WHENCE is the starting point of the File and Zero will set it from a beginning of the file and the code below demonstrates the Perl seek as shown below.
[perl]
seek FH, bytes, WHENCE;
[/perl]
The code below demonstrtes the Perl Seek as shown below.
[perl]
open FH, '+<','input.txt';
seek FH, 5, 0; # This will start reading data after 5 bytes.
$/ = undef;
$out = <FH>;
print $out;
close FH;
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.