1. Perl
  2. builtin functions
  3. here

getc function - read character by character from file

You can also use the getc function to read character by character.

my $c = getc $fh;

Example

The following is an example that reads one character using the getc function and while statement.

use strict;
use warnings;

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

print "1: Read character by character.\n";
open(my $fh, "<", $file)
  or die "Cannot open $file:$!";

# Use the getc function to read one character at a time.
while (defined(my $c = getc $fh)) {
  print $c;
}

close $fh;

Related Informatrion