Difference Between Compiler and Interpreter
Difference Between Compiler and Interpreter
Compiler
PERL: Perl is a general-purpose programming language originally developed for text manipulation
and now used for a wide range of tasks including system administration, web development, network
programming, GUI development, and more.
What is Perl?
It is used for mission critical projects in the public and private sectors.
Perl Features
Perls database integration interface DBI supports third-party databases including Oracle,
Sybase, Postgres, MySQL and others.
Perl used to be the most popular web programming language due to its text manipulation
capabilities and rapid development cycle.
Perl is interpreted
Perl is an interpreted language, which means that your code can be run as is, without a compilation
stage that creates a non portable executable program.
Traditional compilers convert programs into machine language. When you run a Perl program, it's first
compiled into a byte code, which is then converted ( as the program runs) into machine instructions.
First program
Your first program will look like this:
1. use strict;
2. use warnings;
3. print"Hello World";
Let me explain it step-by-step.
Output: Hello world
1. print"Hello World\n";
As you can see statements in perl end with a semi-colon ;. The \n is the sign we used to denote a
newline. Strings are enclosed in double-quotes ". The print function prints to the screen. When this is
executed perl will print the text and at the end it will print a newline.
Save the file as hello.pl and then you can run the code by selecting terminal . Run as perl hello.pl
User Input
Now let's improve our example by asking the user her name and including it in the response.
1. use5.010;
2. use strict;
3. use warnings;
4.
5. say "What is your name? ";
6. my $name =<STDIN>;
7. say "Hello $name, how are you?";
$name is called a scalar variable.
Scalar: A scalar is a single unit of data. That data might be an integer number, floating point, a
character, a string, a paragraph, or an entire web page.
Variables are declared using the my keyword. (actually that's one of the requirements strict adds.)
Scalar variables always start with a $ sign. The <STDIN> is the tool to read a line from the keyboard.
It will ask for your name. Type in your name and press ENTER to let perl know you have finished
typing in your name.
You will notice that the output is a bit broken: The comma after the name appears on a newline. That's
because the ENTER you pressed, when typing in your name, got into the $name variable.
Getting rid of newlines
1. use5.010;
2. use strict;
3. use warnings;
4.
5. say "What is your name? ";
6. my $name =<STDIN>;
7. chomp $name;
8. say "Hello $name, how are you?";
It is such a common task in Perl, that there is a special function called chomp to remove the trailing
newlines from strings.
Conclusion
In every script you write you should always add use strict; and use warnings; as the first two statements.
It is also very recommended to add use 5.010;.
Exercises
1. use strict;
2. use warnings;
3. use5.010;
4. say "Hello ";
5. say "World";
It did not show on one line. Why? How to fix it?
Exercise 2
Write a script that asks the user for two numbers, one after the other. Then prints out the sum of the two
numbers.