
| Contents |
|||||||||||||||
| [an error occurred while processing this directive] |
9.1 Standard Output and the print OperatorBy default output from a Perl program goes to the standard output stream. The basic operator for this output is the print operator. Essentially the print operator is passed a list of items and outputs them to the standard output stream. Values passed to print can be in a number of forms. For example: A string value:
A string variable:print "Hello my name is Fred\n";
A mathematical calculation:$name="fred"; print $name;
Or even a mixture of the three:print 2+4;
which will display:print "I asked $name and he told me 2+4 equals ", 2+4, ".\n";
The print operator can also be used to print an array:I asked fred and he told me 2+4 equals 6.
The above command will print all the items contained in an array. For example:print @array;
will output each element of the array. Note that none of the elements in the array have newline characters so they are all displayed on the same line:#!/usr/bin/perl @colorarray = qw { white red green blue yellow black }; print @colorarray;
To display the array as an interpolated array put the array in double quotes:whiteredgreenblueyellowblack
The print command treats the array as though it had been interpolated into a string variable and will output the values as a single string separated by spaces:#!/usr/bin/perl @colorarray = qw { white red green blue yellow black }; print "@colorarray";
white red green blue yellow black |