print() - Printing Output to File Handles

This section describes various ways to use the print() function in different ways to output data to file handles.

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, http://www.herongyang.com/
#
   $_ = "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?

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.

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

 Converting Perl Script to Executable Binary

 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

 References

 PDF Printing Version