Herong's Tutorial Notes on Perl - Part A
Dr. Herong Yang, Version 4.09

Input to and Output from Perl Programs

Part:   1  2  

(Continued from previous part...)

Printing Output to a File Handle

Printing output to a file handle can be done by calling the print() function. Examples of print() function calling syntaxes:

1. Print the value of the default variable $_ to the pre-defined standard output channel:

rc = print;

2. Print the specified value to the pre-defined standard output channel:

rc = print value;

3. Print a list of values to the pre-defined standard output channel:

rc = print(list_of_values);

4. Print a list of values to the output channel represented by the file_handle with a very interesting syntax. The file handle is separated by a space character " " from the rest of the arguments:

rc = print(file_handle list_of_values);

5. Print a list of values to the output channel represented by the file_handle with a very interesting syntax. The file handle is separated by a space character " " from the rest of the arguments:

rc = print(file_handle list_of_values);

In order to test the different forms of the print function, I wrote the following program, print.pl:

#- print.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
   $_ = "Starting:\n";
   print;
   print("1+1=",1+1,"\n");
   print(STDOUT "(1+2)*3=",(1+2)*3,"\n");
   $v = "OUT";
   open($v,"> out.tmp");
   print($v);
   print(OUT);
   print($v "The end.\n");
   exit;

Before you running the program, try to guess what you will get in the standard output channel, and what you will get in the output file, out.tmp:

>print.pl
Starting:
1+1=2
(1+2)*3=9
OUT

>type out.tmp
Starting:
The end.

Are you surprised about the behavior of the following 3 statements in the program?

  • " print($v);" prints the value in $v to STDOUT.
  • " print(OUT));" prints the value in $_ to OUT.
  • " print($v "The end.\n");" prints the specified string to OUT.

Also note that \n will be printed as \r\n on Windows system. Check the size of out.tmp. You will see 21 bytes, but the total length of the two printed strings is only 19 bytes. You can also verify this by open out.tmp with a hex editor.

Reading Input from a File Handle

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:

  • There is a special variable, $/, which stores the input record separator. The default value is \n (0x0A).
  • With ActivePerl on Windows system, \r (0x0D) is automatically removed from the input record, because \r\n is normally used as text line delimiters on Windows system.
  • Input record separator will be kept as part of the returning string of the input operator <>.

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

#- InputStat.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
   ($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:

>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:

>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:

>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:

  • \r will be removed by the input operator if found as \r\n. This is why I got only 8 total characters, instead of 10.
  • \r will be kept as part of the input record, if found with \n. This is why I got one record on the third test.

Part:   1  2  

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - Input to and Output from Perl Programs