Perl Tutorial

Page 25

2.5 The Traditional “Hello World” Program

13

NOTES The use of Autoexec.bat is now obsolete starting from Windows 2000. Setting of environment variables should be carried out by right-clicking on the “My Computer” icon, and then choose the “Properties” option. Now select the “Advanced” tab and then click on the “Environment Variables” button at the bottom. To make the perl interpreter available to all users on the system, the path should be appended to the PATH variable in the “System variables” section. If you modify the PATH variable in the “User variables” section, only the user concerned (presumably you) will be affected.

EXAMPLE 2.1

1 2 3 4 5

#!/usr/bin/perl -w # Example 2.1 - Hello World # print the text to the screen print "Hello, World!\n";

Here we outline the steps to create this simple program on Windows and Linux. Microsoft Windows

1.

Open Notepad (or any other text editor you choose) and type in the source code shown above. Note that the line numbers on the left are for identification of lines only and do NOT type them into the text editor. Please make sure word wrap is disabled.

2.

Save the file as hello.pl. A few text editors, like Notepad, usually append the “.txt” extension to the filename when saving. You may put a pair of double quotes around the filename to circumvent this behaviour. Also, if you are using Windows 2000 or above and would like to use Notepad, please ensure that the file encoding is set to ANSI instead of Unicode.

3.

Bring up an MS-DOS prompt window and change to the directory containing your newly created file. Say if you have saved to ”C:\perl examples”, then type cd C:\perl examples and press Enter. Put a pair of double quotes around the path if any directories in the path contains spaces (In fact I don’t recommend placing Perl source files in directories with names containing spaces. It only complicates matters).

4.

Execute the program by typing perl -w hello.pl and press enter.

Unix or GNU/Linux

1.

Open any text editor (vim, emacs, pico, kedit ....) and type in the source code shown above. Note that the line numbers on the left are for identification only and do NOT type them into the text editor. Please make sure word wrap is disabled.

2.

Save the file as hello.pl. Note that the path on line 1 has to match the path to perl on your system. Also, no spaces should precede the ‘#’ character and no empty lines are allowed before this special line (traditionally known as the ‘shebang’ line).


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.