1. Perl
  2. Operators
  3. here

Line input operator <> - Read one line from a file

Use the line input operator "<>" to read files line by line . When there are no more lines to read, undef is returned.

my $line = <$fh>;

Note that unlike other operators, you put the filehandle between "<" and">".

Usually used in combination with while statement to read lines repeatedly.

while (my $line = <$fh>) {
  # ...

If you think about it as above, you may feel that it will not work properly if a false value such as "0" is returned. Since a line feed code is inserted in the middle of the line, "0" is not returned, but "0" may be returned at the end.

But don't worry. This is because the file operator is interpreted as follows only when used in combination with while.

while (defined(my $line = <$fh>)) {
  # ...

So, as long as you use it in combination with while, the first writing is fine.

Diamond operator

The line input operator "<>" with nothing between "<" and">" is specially called the diamond operator . If the file handle is omitted, it can be read line by line from the standard input and the file name specified by command line argument.

# Diamond operator
my $line = <>;

You can easily read a single line from a file using the diamond operator. Reads line by line from the file given by standard input and command line arguments.

while (my $line = <>) {

The advantage of using the diamond operator is that you can easily write a "program that can receive the output of other programs from standard input". This is the same behavior as common UNIX utilities such as cat, sed, awk, grep, etc.

# Receive file name from command line argument
script.pl file.txt

# Receive the output of other programs from standard input using pipes
grep hello file.txt | script.pl

Another advantage is that you don't have to bother to open or close. For short programs, it is convenient to use the diamond operator because it is easy to write the program.

This is an example that outputs the received line as it is.

# Output the received line as it is
use strict;
use warnings;

while (my $line = <>) {
  print $line;

Get into the array at once

You can populate an array with all rows by evaluating the row input operator in a list context. However, be aware that if the file is large, it will use a lot of memory.

my @lines = <$fh>

Import all the contents of the file

To capture the entire contents of a file, it is common to write something like this:

my $content = do {local $/; <$fh>};

"$/" Is the line delimiter. If you leave this undefined, the line input operator will populate the entire contents of the file. I'm using local to temporarily undefine it. The do block returns the last evaluated value. So, with this description, you can read the contents of the file at once.


This is an example that reads a file line by line using the line input operator. Repeat reading one line in a while loop.

use strict;
use warnings;

# File read
# readline $fh
# <$fh>

# File name you want to read
my $file = shift;

open(my $fh, "<", $file)
  or die "Cannot open $file:$!";

print "1: General description of file read\n";
# Read one line with the readline function.
while (my $line = readline $fh) {
    # Remove a line break with chomp function
    chomp $line;
    # Something to do with $line.
    # Export to standard output.
    print $line, "\n";

    # Repeat reading one line until the file reaches EOF (END OF FILE).

close $fh;

Code explanation

(1) Read 1 - line file

while (my $line = readline $fh) {
    # process ...

To read the file line by line, use the readline function. $fh is the filehandle opened by the open function. Repeat reading one line with while statement to read to the end of the file.

It may seem strange to read one line at the position where conditional statement of the while loop is written, but such a description is possible. With the description my $line = readline $fh, I declare a lexical variable called $line and assign the read line to $line. $line is valid only inside a while loop and is not visible from the outside.

When the end of the file is reached, the readline function returns undef and $line is assigned undef. Since $line is undef, while statement ends.

(2) Remove a line break with chomp function

while (my $line = readline $fh) {
  chomp $line;
  # process ...

Line breaks are often unnecessary after reading a line, so use the chomp function to remove the line break.

(3) Another way to write readline <$fh>

while (my $line = <$fh>) {
  # process ...

In Perl, putting a filehandle inside <> has the same meaning as readline $fh. Use this if you prefer a simple description.

Related Informatrion