<file_handle> - Reading Data from File Handles

This section describes various ways to use the <file_handle> operator in different ways to read data from file handles.

Reading input from a file handle is done by using the input operator, <file_handle>, which returns the next record from the input channel in a scalar context, and returns the rest of the records from the input channel in an array context:

$s = <file_handle>;
@a = <file_handle>;

Note that:

In order to test how the input operator reacts to different record separators, I wrote the following program, inputStat.pl, to count characters and records:

#- InputStat.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.
#
   ($file) = @ARGV;
   die "Missing file name.\n" unless $file;
   $recordCount = 0;
   $charCount = 0;
   $totalCount = 0;
   open(IN, "< $file");
   while (<IN>) {
      $recordCount++;
      $totalCount += length($_);
      chop;
      $charCount += length($_);
   }
   close(IN);
   print "Number of records = $recordCount\n";
   print "Number of characters after chop = $charCount\n";
   print "Number of total characters = $totalCount\n";
   exit;

The first file, text.rn, has \r\n at the end of each record, like a normal text file on a Windows system. text.rn has only 10 bytes in two records: "123\r\nABC\r\n". Now run InputStat.pl with this file, you will get:

C:herong>InputStat.pl text.rn
Number of records = 2
Number of characters after chop = 6
Number of total characters = 8

The second file, text.n, has \n at the end of each record, like a normal text file on a Unix system. text.n has only 10 bytes in two records: "1234\nABCD\n". Now run InputStat.pl with this file, you will get:

C:herong>InputStat.pl text.n
Number of records = 2
Number of characters after chop = 8
Number of total characters = 10

The third file, text.r, has \r at the end of each record. text.r has only 10 bytes in two records: "1234\rABCD\r". Now run InputStat.pl with this file, you will get:

C:herong>InputStat.pl text.r
Number of records = 1
Number of characters after chop = 9
Number of total characters = 10

As you can see from the test results:

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

File Handles and Data Input/Output

 open() - Opening File Handles for Input and Output

 print() - Printing Output to File Handles

<file_handle> - Reading Data from File Handles

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB