Perl - Part 3
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
Programming Component 1: The Editor
We'll be using the slightly ancient editor known as vi. It has the advantage of being available on most *NIX platforms so once you get used to it your knowledge will be transferable. That being said, you might prefer to have that part of your brain amputated afterwards; vi is old, awkward and strict and may cause you slight mental damage if you let it.
To edit a program with vi first cd to the directory with the file; then type vi filename on the command line. Immediately enter insert mode by pressing i. Enter or edit your program as required. When finished, press the ESC key once, press :wq and return to write and quit.
If you haven't already done so, create a folder called perl_progs, enter that folder and create the Hello World program. Use the name hello.pl for the file to clearly identify the pupose of the program as well as the type of file. Into that file, enter the following code to make your own Hello World program. Enter the code exactly as given, complete with indentation, capitalisation, symbols etc as shown:
#!/usr/bin/perl
# This is my first program.
print "Hello World!";
Programming Component 2: Running Your Program
Now use the generic command perl filename to run the program. This calls the Perl interpreter, passes it the name of the file to run and gets the Perl interpreter to first check and then carry out the commands in the file.
The checking process is essential; it makes sure the program is typed correctly. If you get error messages don't blame the computer: go back, edit the file again, and see what you did wrong. Then try to run the program again. When you succeed you'll see the message like this:

As you can see, this displays the correct message, but doesn't move on to the next line. In programming, appearance is all important; so it's worth while to make a small alteration:
#!/usr/bin/perl
# This is my first program.
print "Hello World!\n";
The use of the escape sequence \n will cause the output (in this case the next command prompt) to move to a new line. It looks better and is more thorough; either one is as good a reason as is required to make the change. The output should now look like this:

This completes the creation of the essential first Perl program, confirming that we can edit and run a program. Read on for more......
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
We'll be using the slightly ancient editor known as vi. It has the advantage of being available on most *NIX platforms so once you get used to it your knowledge will be transferable. That being said, you might prefer to have that part of your brain amputated afterwards; vi is old, awkward and strict and may cause you slight mental damage if you let it.
To edit a program with vi first cd to the directory with the file; then type vi filename on the command line. Immediately enter insert mode by pressing i. Enter or edit your program as required. When finished, press the ESC key once, press :wq and return to write and quit.
If you haven't already done so, create a folder called perl_progs, enter that folder and create the Hello World program. Use the name hello.pl for the file to clearly identify the pupose of the program as well as the type of file. Into that file, enter the following code to make your own Hello World program. Enter the code exactly as given, complete with indentation, capitalisation, symbols etc as shown:
#!/usr/bin/perl
# This is my first program.
print "Hello World!";
Programming Component 2: Running Your Program
Now use the generic command perl filename to run the program. This calls the Perl interpreter, passes it the name of the file to run and gets the Perl interpreter to first check and then carry out the commands in the file.
The checking process is essential; it makes sure the program is typed correctly. If you get error messages don't blame the computer: go back, edit the file again, and see what you did wrong. Then try to run the program again. When you succeed you'll see the message like this:

As you can see, this displays the correct message, but doesn't move on to the next line. In programming, appearance is all important; so it's worth while to make a small alteration:
#!/usr/bin/perl
# This is my first program.
print "Hello World!\n";
The use of the escape sequence \n will cause the output (in this case the next command prompt) to move to a new line. It looks better and is more thorough; either one is as good a reason as is required to make the change. The output should now look like this:

This completes the creation of the essential first Perl program, confirming that we can edit and run a program. Read on for more......
[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
Now use the generic command perl filename to run the program. This calls the Perl interpreter, passes it the name of the file to run and gets the Perl interpreter to first check and then carry out the commands in the file.
The checking process is essential; it makes sure the program is typed correctly. If you get error messages don't blame the computer: go back, edit the file again, and see what you did wrong. Then try to run the program again. When you succeed you'll see the message like this:

As you can see, this displays the correct message, but doesn't move on to the next line. In programming, appearance is all important; so it's worth while to make a small alteration:
#!/usr/bin/perl
# This is my first program.
print "Hello World!\n";
The use of the escape sequence \n will cause the output (in this case the next command prompt) to move to a new line. It looks better and is more thorough; either one is as good a reason as is required to make the change. The output should now look like this:
