Perl programming is a sequence programming language. It read and run the program from top to the bottom of script. Besides, it supports subroutines, loops and control structure which make the program more flexible. Every statement line must be end with a semicolon (;), if not the script will encounter compile error due to syntax error.
Perl Installation
Before we start to writing our first Perl program, let's setup our Perl environment in the system. First come first, Perl is supported in various of operating system (OS). The OS which are commonly used by the most users are listed at below:
- Unix (Linux, SunOS, etc.)
- Window (9x/NT/2000/XP/Vista)
- Debian GNU
- Others OS
In this series of tutorials, I would like to use Linux OS. But don't worry about it, the language still are the same just the GUI interfacing might be different. In a general sense, when you installing Linux OS into your desktop / laptop, the Perl environment already been provided in the system. To be confirm whether it is already supported in the system or which version are been used, you can simply open the terminal to check for it.
Press Ctrl + Alt + T as a shortcut to open Terminal in Linus OS. Then enter perl -v. You will get a printing message in the Terminal if your system already supported for the Perl environment. The scene as shown in Figure 2.1.
If your system not support for the Perl environment yet. Please get the most up-to-date and current code which is available at the official website of Perl: http://www.perl.org/.
Figure 2.1 |
If your system not support for the Perl environment yet. Please get the most up-to-date and current code which is available at the official website of Perl: http://www.perl.org/.
First Perl Program
For the very first Perl program, we will be learn how to execute the Perl statements from the command line in Terminal. Execute the Perl statements from the command line, we need to use the Perl interpreter with -e option. If without this interpreter, we will not able to execute the Perl statements. Let's try put the command as below into the Terminal.
$perl -e 'print "Welcome to Perl World\n"'
In this step, we are successfully to execute the Perl statement through the Terminal. But if you have many Perl statements need to execute, this is not the best way for scripting. Thus, we need to open a text file to save the Perl script and then execute it in the Terminal. There are several type of test editor in the Linux such as vi editor, vim editor, gedit editor or nano editor.
In this tutorial, we prefer to using nano editor because it is more suitable use by programmer. It will show the different color for the command such as variable and conditional command. Thus it will be easy to the programmer to differentiate the commands. Now try to open a text file and type the following code and save it as .pl file.
#!/usr/bin/perl
#this will print out Welcome to Perl World
print "Welcome to Perl World\n";
#this will print out Welcome to Perl World
print "Welcome to Perl World\n";
If you notice that there have a command /usr/bin/perl. Actually that is the Perl interpreter binary. Without this command line the system will not able to know this is a Perl script and not able to compile it.
Beside that, before we execute the script, we need to change the authority of the script file. This is because by default, there is no authority for the user to execute the file. So now, we need to change the mode of the script file and give execution privilege. We can do it by using the code at below.
$chmod 755 First.pl
After change the mode of the script we should able to execute it by typing ./First.pl command in the Terminal. Congratulation to you! You are successfully to run your first Perl program and it should work as the video below.
In the next tutorial, we will learn three type of data type which is existing in the Perl language. Be stay tuned for my tutorial. Hope you have fun with it.
No comments:
Post a Comment