A First Step in perl Programming
A computer program is a list of instructions that tell the computer what to do.
A program, like any system, can be seen as sequence of
Input → Process → Output
The Hello World program is the common first program used to introduce
a programming language, it has the objectives of
- Verifying that someone is able to properly run a program
- Verifying that a program is able to send output to the user
- Verifying that someone is able to enter the source code (program statements)
of a program.
perl is an interpreted language, which means that
perl is a program itself that reads statements
that are written in the perl language, and executes the statements.
Running perl Interactively
If you have perl installed, you can run perl interactively, which
means you can enter commands and have perl read and interpret statements as you
type them in. Text entered by the user/programmer is shown below in blue.
Example: running perl from a Linux command line
perl
print 'Hello world.';
[Ctrl]-D
Hello World.
Explanation:
The
perl command starts the perl interpreter.
perl will not send the user any output or prompt for input; perl
is waiting for program instructions.
The first command entered by the user is the perl statement:
print 'Hello world.';
The user enters
[Ctrl]-D (control key + the letter D) to let perl know there is
no more input. The [Ctrl]-D may show up as ^D on some systems, or may not
show up at all. ^D is often used as the end-of-file marker. Here the ^D
marks the end of the standard input file, the keyboard.
When the user enters
^D perl executes the print statement, sending the
output
"Hello World." to the screen and perl terminates.
Running perl with a stored program
Most progams will be entered using a text file, where the programs
can be stored, edited as needed, and reused.
Create a text file named helloWorld.pl
and enter the following one line program
With the program saved, perl can be run from the command line with the name of the stored program.
Example: running perl with a program name
perl helloWorld.pl
Hello World.
Explanation:
The
perl helloWorld.pl command starts the perl interpreter
and perl will immediately begin to process the program
in file
helloWorld.pl.
perl will not display any program information about
itself, nor will it provide a prompt. perl executes
the program helloWorld.pl sending the output
Hello World. and then terminates.
Making a perl program executable from the command line
When you run perl from the command line, the operating system
will search your path for the perl interpreter. In unix/Linux you can
find out where a program is located using the which command.
To find out where perl has been installed, issue the following
Linux command:
In my system, the which command replies with:
By adding the following line as the first line of helloWorld.pl,
and making the file executable, the operating system will always
know to use /usr/bin/perl to execute the program:
#!/usr/bin/perl
print "Hello World."
Make the program helloWorld.pl executable using the following unix/Linux command:
The program helloWorld.pl can be run from the command line using only the name of the stored program.
Example: running an executable perl program
./helloWorld.pl
Hello World.
Explanation:
When the operating system is told to execute the helloWorld.pl program, it will see by
the first line in the file that it should use the perl interpreter,
and perl will immediately begin to process the program.
perl will not display any program information about
itself, nor will it provide a prompt. perl executes
the program helloWorld.pl and then terminates.
The ./ before helloWorld.pl
in the command tells the operating system to look in 'the current directory' for the program
helloWorld.pl