Summary: in this tutorial, you will learn about the basic Perl syntax to get started with Perl language quickly including variables, expressions, statements, block, comments, whitespaces, and keywords.We will go into every detail of each topic in the next tutorial.
Values and Variables
To manipulate data such as changing, adding, etc., you need variables. You use a variable to store a value and through the name of the variable, you can process the value.
The following illustrates some variables in Perl:
$x = 10;
$y = 20;
$s = "Perl string";
In Perl, an expression is anything that returns a value. The expression can be used in a larger expression or a statement. The expression can be a literal number, complex expression with operators, or a function call. For example, 3 is an expression that returns value of 3. The
$a + $b is an expression that returns the sum of two variables:
A statement is made up of expressions. Statement is executed by Perl at run-time. Each Perl statement must end with a semicolon (;). The following example demonstrates the statements in Perl:
$c = $a + $b;
A block is made up of statements wrapped in curly braces. You use blocks to organize statements in program. The following example demonstrates a block in Perl:
$a = 1;
$a = $a + 1;
Any variable declared inside a block has its own scope. It means the variables declared inside a block only last as long as the block is executed. We will discuss more about scope of variables in Perl variable tutorial.
In Perl, a comment begins with a hash (#) character. Perl interpreter ignores comments at both compile time and runtime. You use the comment to document the logic of your code. The code tells you what it does however comment provides information on why the code does so.
Comment is very important and useful to you as a programmer in order to understand the code later on, as well as other developers who will maintain your code in the future.
Let’s take a look at the following example:
$salary = $salary * 1.05;
What the code does is to increase the value of the variable
$salary 5%. However why it does so was not documented. Therefore the following code with comment is much clearer.
# increase salary %5 for employee who achieves exceptional rating
$salary = $salary * 1.05;
Perl also allows you to put a comment on the same line as the statement. See the following example:
$counter = 0; # reset the counter
It is important to use comments properly to make your code easier to understand.
Whitespaces are spaces, tabs and new lines. Perl is very flexible in term of whitespaces usages. Consider the following example:
$x = 20;
Both lines of code work perfectly. We surrounded the assignment operator (=) with whitespace in the first statement, but not in the second one. Perl really doesn’t care about the whitespace, however it is a good practice to use whitespace to make the code more readable.
Perl has a set of keywords that have special meanings to its language. Perl keywords fall into some categories such as built-in function and control keywords. You should always avoid using keywords to name variables, functions, modules and other objects. Check it out the list of Perl keywords.
Sometimes, it is fine to use a variable name such as
print() function. However, it may lead to a confusion. In addition, if the program has issue, it is more difficult to troubleshoot.
In this tutorial, we have covered the basic Perl syntax including variables, expressions, statements, block, comments, whitespaces and keywords.