Pure Programmer
Blue Matrix

Cluster Map

Console Output

Every program, every function, everything we do when we program consists of three fundamental operations: input, processing and output. This pattern of accepting input, doing some sort of processing action, then emitting output is at the heart of writing programs. Once you master these ideas, you will be able to construct many types of programs from console utilities, to background daemons to [[GUI]] (Graphical User Interface) programs.

The first of these that we will explore is output. For console-type programs, output consists of a stream of characters that are displayed on the console. In Perl our programs automatically have access to the standard output stream ([[stdout]]) which is represented by the symbol STDOUT. We can send characters and strings of characters to STDOUT to cause them to appear on the console. This is done using the print function.

#!/usr/bin/env perl

use strict;
use warnings;
use utf8;

	binmode(STDOUT, ":utf8");

	print STDOUT 'H','e','l','l','o',"\n";
$ perl ConsoleOutput1.pl Hello

Starting with our boilerplate code for a console Perl program, we then change the code inside the body of the MAIN block. The body of the MAIN block consists of the statements between the curly braces {} in the above example. Note that programming statements in Perl always end with a semicolon.

In the above example we have one statement in the body of the MAIN block. This statement uses the print function to send a individual characters to the console (STDOUT). If the STDOUT symbol is omitted, Perl will assume that you mean STDOUT. The last character literal is a special escaped sequence that outputs the newline character to begin a new line. Each of the characters are wrapped in single quotes which is one of the ways Perl represents character literals. The other is to use double quotes.

The end-of-line character is not universal across different types of computers. Text output (and text files) on Unix/Linux systems use the [[ASCII]] line feed character (LF is value 10) to mark the end of a line. Macintosh OS X text uses the ASCII carriage return character (CR is value 13) as the end-of-line character. DOS/Windows systems use the CR LF character pair to signify end-of-line. Output to the standard output in Perl is smart enough to use the correct end-of-line character(s) for the operating system on which our program is running when we use the print function in our code.


To try out the program above, copy and paste the source into your programming text editor, then save it in a file called "Hello.pl". To organize your work, it is easiest to create a folder on your desktop to hold your source files. For our purposes we will assume that you name this folder "tutorial". After saving your source file, bring up the command-line terminal and execute the commands illustrated below.

$ cd Desktop $ cd tutorial $ perl hello.pl Hello $

The cd commands cause the current directory (cd) to be changed. Since in a new terminal we start with the current directory set to your own home directory, we change once to the "Desktop" directory, then again into the "tutorial" directory. The next command launches the Perl interpreter to run source file. If all goes well you will see the five characters "Hello" printed on the console. If the run command prints out error messages, take note of the error and line number and then correct your source file. Then you can try the process again.

String Output

While on a fundamental level all output is done one character at a time, it can be rather inconvenient to write code that outputs one character at a time. To make things easier, we can also output a sequence of characters known as a string. Like a single character literal that can be written inside single quotes or double quotes, a sequence of characters or string can also be written inside a pair of single or double quotes. The following program performs the same output as the program above but uses a string literal instead.

#!/usr/bin/env perl

use strict;
use warnings;
use utf8;

	binmode(STDOUT, ":utf8");

	print "Hello, world!\n";
$ perl ConsoleOutput2.pl Hello, world!
Common Escape Sequences
carriage return\r
any char\x##

Special Characters

While we can represent most of the printable characters inside character or string literals using the character itself, there are some exceptions: single quote, double quote and the control characters. These exceptions can, however, be embedded into our literals by using a special escape notation. The escape notation consists of the backslash followed by another character. See the table to the right for the most common escape sequences.

It turns out that any character can be printed using an escape sequence if needed. The escape sequence \x## can be used to represent any [[ASCII]] character as long as you know the [[hexadecimal]] value for that character. Just replace the #'s with the corresponding hexadecimal value and you have the escape sequence for any character. Use the \x{####} sequence to represent [[Unicode]] code points above 0x100.

Escape character sequences are handled differently in single quoted vs double quoted strings. Escape characters are interpreted and replaced by the character they represent if the string is double quoted. But if the string is single quoted, then escape character sequences are not interpreted and we see the escape sequence insted. Try changing the program above to use a single quoted string and notice that \n actually appears on the console instead of a new line.



More ★'s indicate higher difficulty level.

Windows command line terminal does not support [[Unicode]] characters so some programs will not display Unicode characters correctly. If you redirect the program output to a file then open it in a Unicode aware text editor you should see the correct output. For example: C:> perl Output6.pl > temp.txt But if you use [[Cygwin]], the default terminal program mintty supports Unicode so this is not necessary.