Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.1 Standard Output and the print Operator

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

print "Hello my name is Fred\n";
A string variable:

$name="fred"; print $name;
A mathematical calculation:

print 2+4;
Or even a mixture of the three:

print "I asked $name and he told me 2+4 equals ", 2+4, ".\n";
which will display:

I asked fred and he told me 2+4 equals 6.
The print operator can also be used to print an array:

print @array;
The above command will print all the items contained in an array. For example:

#!/usr/bin/perl @colorarray = qw { white red green blue yellow black }; print @colorarray;
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:

To display the array as an interpolated array put the array in double quotes:

#!/usr/bin/perl @colorarray = qw { white red green blue yellow black }; print "@colorarray";
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:

white red green blue yellow black

  Published under the terms of the GNU General Public License Design by Interspire