Hi everyone. Today I will start a new series of esssays introducing the elementary knowledge of Perl, a computer language of which probably many of us never heard. I‘ll try my best to articulate my idea. If there is something you disgree with, please feedback to me and I will seriously consider your views.
I. What‘s Perl (Practical Extraction and Report Language)?
Just as C/C++, Perl is just another member of the kingdom of computer language. Actually it‘s a kind of old-fashioned scripting language. There was a time when most web applications were writen by Perl. Nowdays Perl is gradually taken place by Python and PHP partly because of its fecklessness, boundless flexbility and some tricky grammars. However, those defects can not obscure its virtues. Perl has a powerful weapon---Regular Expression, which make this language extraordinary when text processing. Besides, Perl is also an object-oriented programming(OOP) language. Although it‘s not as efficient as C, you can simply write several lines of perl code to implement some complicate functions, which will largely shorten development cycles.
II. What do I need to know before learing Perl?
To be honest, it‘s not a good choice if you start with perl when you know nothing about computer programing. In some ways perl is over free, which may give the learner an illusion that you can write the code in any way. Unfortunately, there are still borders. If you ignore these limits, your code will crash and you even don‘t know what‘s wrong. You‘d better know something about C or Python or any other popular languages. And if you are familiar with linux, you‘ll find it very easy to learn perl regular expressions, because RE is actually an enhanced version of awk/sed.
III. Reference Books
For a starter, the book ‘Learning Perl‘ is recommended, you can get a basic idea of perl. If you want to go further, you can read ‘Programing Perl‘ to learn some powerful tools. And if you want to know how Perl works, which means some fundamental operational mechanism, ‘Advance Perl Programing‘ will be right choice.
IV. How to Setup Perl Environment
For windows, you can download and install ActivePerl, which is a integrated script interpreter.When you finish the installation, you can open the Cmd window and type "perl -v", you‘ll see some information of perl.
For Linux users, normally ubuntu system has integrated perl interpreter. To check for that you can press Ctrl+Alt+T to call out terminal and then type perl -v.
V. Perl Editor: Sublime Text
In fact, you can use anything to write perl codes, as long as the suffix of your file is ‘.pl‘ or ‘.pm‘. However sublime text will make the code writing more enjoyable. Now let me tell you how to use sublime text to write and run perl codes.
First search and download sublime text, then install the software.
Just do as instruction, you can easily finish the installation.
The installation is finished. Now you can write your perl codes by sublime. Note that the suffix of the file must be ".pl" or ".pm". However, you can not run your codes. To implement this function, you should move one more step.
Open sublime text, you‘ll find option "Tools" in the toolbar. Click Tools->Build System->new Build System.
Then copy these codes into the new dialog box.
{ "cmd": ["perl", "-w", "$file"], "file_regex": ".* at (.*) line ([0-9]*)", "selector": "source.perl" }
Then press Ctrl+S ,save the file with the name of "Perl.sublime-build" into the default directory. Done.
Now your sublime text is able to run perl. (use Ctrl+B)
VI. First Perl Program: Hello World
Open Sublime, create a file saved as "hello.pl", type your code as follows:
#!/usr/bin/perl -w use strict; print "Hello World!"; 1;
Then press Ctrl+B, you‘ll get the results.
‘#’ is an annotation for Perl, just as ‘/***/’ for C language.
#!/usr/bin/perl -w is a linux-style sentence. #!/usr/bin/perl means that the system will find the perl interpreter at /usr/bin/perl, for windows users, this line is dispensable, but it‘s recommended to keep it in consideration of portability. "-w" mean use waring mode, the interpreter will show some warning if your codes lack of standardization.
use strict; means that the interpreter will use the most strict grammar to check your codes. It‘s better to add this line for novice.
1; replaces the return-value of this program.
print "Hello World" means that the system print the sentence "Hello World" into the default output handle,the screen in other words.
Now you have already know how to write your first perl program. Just try more for yourself!