Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
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) HerongYang.com. All Rights Reserved. # $_ = "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:
C:\herong> print.pl Starting: 1+1=2 (1+2)*3=9 OUT C:\herong> more 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
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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