File input / output

Using filehandles for reading

A text file typically looks like:
_____________________ \n
_____________________ \n
_____________________ \n
_____________________ \n
end-of-file
Now we will demonstrate three possible ways to read lines from the file 1c1e.pdb (A catalytic antibody taken from the PDB database).

1. Read file line by line

#!/usr/bin/perl

my $infile = "1c1e.pdb";
my ($line1, $line2, $line3);

open (PDB, $infile) || die "cannot open \"$infile\": $!";

$line1 = <PDB>;  # read first line
print $line1;   # proccess line (here we only print it)

$line2 = <PDB>;  # read next line
print $line2;   # proccess line (here we only print it)

$line3 = <PDB>;  # read next line
print $line3;   # proccess line (here we only print it)

# ...

close (PDB);

2. Read all lines at once and assign them into an array variable

When <FILEHANDLE> is assigned into an array variable, all lines up to the end of the file are read at once. Each line becomes a separate element of the array.
#!/usr/bin/perl

$infile = "1c1e.pdb";
open (PDB, $infile) || die "cannot open \"$infile\": $!";

@lines = <PDB>;
chomp (@lines);   # chomp each element of @lines

close (PDB);

# to process the lines you might wish to iterate
# over the @lines array with a foreach loop:

foreach $line (@lines) {
   # process $line. here we just print it.
   print  "$line\n";
}

3. Most useful: iterate over lines until end of file

Using a while loop, read one line at a time and assign it into a scalar variable, as long as the variable is not an empty string (which will happen at end-of-file).

Note that a blank line read from the file will not result in an empty string, since it still contains the terminating \n.

#!/usr/bin/perl

$infile = "1c1e.pdb";
open (PDB, $infile) || die "cannot open \"$infile\": $!";

while ($line = <PDB>) {    

   chomp ($line);
   print "$line\n"; # process line. here we just print it.
   
}

close (PDB);
The best way, is to use the following shorthand:
#!/usr/bin/perl

open (PDB, "1c1e.pdb) || die "cannot open PDB: $!";

while (<PDB>) {    

   chomp ($_);   # The $_ variable gets the values of the line processed.
   print "$_\n"; # process line. here we just print it.
   
}

close (PDB);

Table of Contents.
Next.