Hello There! If you had read my previous post, you would have guessed it already, the new language that am trying to learn is PERL.
The contents of this post comes straight out of the notes that I make on my rough book while am trying to learn PERL from scratch. I promise to keep it as simple and concise as possible. I’m just a novice, so mistakes are bound to occur, apologizing in advance.
- PERL – Practical Extraction and Reporting Language (most well-known acronym in use) – developed by Larry Wall.
- Powerful like any other high level languages, convenient like any other scripting languages.
- Doesn’t require compiler, linker and all those stuffs.
- Requires the PERL interpreter though.
Note: The gist is simple – there is no conversion of the script that you write into a machine language. That is where you need compiler and linker. But it is required to have PERL installed and running on the system where you are trying to execute a PERL script.
So you need PERL to be installed on your system.
- Check if you have PERL already installed on your system by typing “perl -v” at command prompt on Windows OS. This basically returns the version of PERL installed on your system (if you have it, else bad command) – I have v5.14.2 installed.
- Instead of ‘-v’, you could substitute it with other letters to display various other things. (Check here for more details)
- You can install PERL from here. This is DWIM PERL, the one I use. It contains a bundle of things as mentioned in the link. There are many other links and variants, so feel free to install whichever you like.
- A PERL script can be typed on any text editor like notepad ++, sublime text etc. All you need to do is save the file with the extension .pl
- Since I installed DWIM PERL, I will be using PADRE for typing and debugging my script as it comes along in the bundle. PADRE stands for PERL Application Development and Refactoring Environment.
- Now for some reason you want to know the path where PERL is installed, try this at command prompt – perl -e “print ^X”. This and more commands can be found here.
Writing the first PERL script
- This is a simple script which will take in an input line from the user and print it out. Type and save it with extension .pl. Here name of script is read_and_write.pl
- This script can be executed from the command prompt. Ensure that you are at the location where the script is saved. Type the command perl followed by name of file.
For example perl read_and_write.pl
Then you type a line and when you press enter the same line will be outputted. Now let’s analyse the code snippet that we wrote above.
- In the script read_and_write.pl, there are 2 lines – the first line is where the input from the user is taken in and the second line is where it is shown to the user.
- $ – this is the symbol used to represent any scalar variable be it string or integer or float. You don’t have to declare the type of variable you are going to use; PERL will do that for you.
So $input is the scalar variable that we are using here. We can have any names instead of input.
- <STDIN> – this is standard input i.e.; the user is inputting something on the command window (here “hi this is the line”). It gets stored into a standard input file and is present there till it is over written by another user input.
- So in the first line of code, we are assigning the RHS to LHS which in simple terms means we are copying the contents of the standard input (user input) to a scalar variable called $input. The = in between makes this possible. So now $input has content “hi this is the line”
- print – This is a library function which takes an argument and outputs it to the user on the standard output; here command window itself. In this code the user input (<STDIN>) is copied to the scalar variable ($input) which is then outputted by using the function print.
- # – this indicates the start of a comment. So whatever follows # on a line is simply ignored by the interpreter.
- ; – each line of code must end in semi colon. That’s how the interpreter identifies that a line or statement of code has ended. The interpreter considers everything before it encounters a ; as a single statement. You can have any number of white spaces in a statement for formatting or readability.