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?
" 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.