Summary: in this tutorial, you will learn how to read a file in scalar context and read the file using diamond operator (<>).
Please follow the open file tutorial before going forward with this tutorial. If you want to write to a file, check it out Perl writing to file tutorial.
Perl read file in scalar context
In order to read from a file in read mode, you put the filehandle variable inside angle brackets as follows:
<FH>
Code language: Perl (perl)
To read the next line of the file with newline included, you use the following syntax:
$line = <FH>;
Code language: Perl (perl)
You can use the Perl while loop to read a file line by line to the end of the file:
while(<FH>){
#...
}
Code language: Perl (perl)
The following program demonstrates how to read a text file line by line and display its content:
#!/usr/bin/perl
use warnings;
use strict;
my $filename = 'c:\temp\test.txt';
open(FH, '<', $filename) or die $!;
while(<FH>){
print $_;
}
close(FH);
Code language: Perl (perl)
Let’s examine the program in detail:
- First, we used the
open()
function to open a file for reading. - Second, the syntax
while()
is equivalent towhile(defined($_ = )
. We read a line from a file and assigned it to the special variable$_
. The loop is terminated when the end of the file reached. - Third, we displayed each line of the file by passing the variable
$_
to theprint
function.
The following is the output of the program:
Perl read file with the diamond operator
Let’s take a look at the following program:
#!/usr/bin/perl
use warnings;
use strict;
while(<>){
print $_;
}
Code language: Perl (perl)
The Perl source code file path is c:\perlws\perl-read-file2.pl
Now, you can invoke the program from the command line as follows:
C:\>perl c:\perlws\perl-read-file2.pl c:\temp\test.txt
Code language: Perl (perl)
And you will see the content of the file c:\temp\test.txt
displayed.
C:\>perl c:\perlws\perl-read-file2.pl c:\temp\test.txt
This is a text file.
We use Perl filehandle to read its content.
we should always close the filehandle after processing it.
C:\>
Code language: Perl (perl)
What happened? Interesting! Let’s examine the program above in more detail:
First we use diamond operator (<>) in the while loop statement. The diamond operator checks if the program was invoked with the command-line argument. If so, it reads from the file in scalar context, one line at a time.
If multiple files are provided, it will read the content of all files in sequence in list context. Try to create a new file c:\temp\test2.txt
and type the following command in the command-line window:
C:\>perl c:\perlws\perl-read-file2.pl c:\temp\test.txt c:\temp\test2.txt
Code language: Perl (perl)
You will get the following output:
C:\>perl c:\perlws\perl-read-file2.pl c:\temp\test.txt c:\temp\test2.txt
This is a text file.
We use Perl filehandle to read its content.
we should always close the filehandle after processing it.
text from test2.txt file
C:\>
Code language: Perl (perl)
“text from test2.txt file” is the content of the test2.txt file.
One more interesting point of the diamond operator is that if you invoke program without command-line arguments, it will read from standard input until end-of-file, just like <STDIN>. You can run the program without command-line arguments. Remember to use ctrl-z enter in Windows or Ctrl-D to input end-of-file.
In this tutorial, we’ve shown you how to read the file from filehandle in scalar context. In addition, we also showed you how to read file using the diamond operator by passing filenames as the command-line arguments