Sunday, January 22, 2012

Learning PERL | Day 2

Welcome back friends!

Yesterday we learn about writing a simple addition program in PERL. To recall you, we coded a program for simple addition in which we used two variables, “$x” & “&y”:
$x = 100;
$y = $x + $x;
print $y;

I stated in my previous blog that variables are used for storing ‘varying data’ that we need or generated in the program. So, you may have noticed one thing about the variable names e.g., in “$x” and “$y”, why we are using this “$” sign before any variable? The answer is related to the identification of these variables as ‘variables’ by the PERL interpreter. The “$” sign tells the PERL interpreter that it is dealing with a variable.

A variable name, which starts from “$” sign, may contain letters(a-z, A-Z), numbers(0-9), a special character “_”, known as underscore or a mixture of these. Example of variable names are $value1, $value2, $max_value, $min_value etc. There is a limit on number of letters you can use in variable names, but, don’t worry, I’m sure you will never need to jump over it.

The variable names should be descriptive and handy to you. Such as if you are writing a program to compute EMI on loan amount, and you use variable names as “$x” and “$y”, and so on, your program may become uninformative after a few steps, that’s you waste most of your time in remembering what x stands for, what y stands for etc. I suggest using mixed type variable names; however, you can omit vowels in words. For example, a good variable for loan amount can be “$amt_loan”.

For those who have used to code in ‘C’ before, they may have familiar with the ‘Datatypes’. For beginners, datatypes simply define what type of data you are entering or what type of information you want in return from the machine. 
For example, in some cases you may need integer values like 1423, 88383 etc., and in some other cases you may need to input contact names as “John Smith”, “Yajur Kumar” etc. So, datatypes simply tells what type of data the program is dealing with.

PERL uses three types of datatypes, namely, scalars, arrays and hashes. Today we only deal with them in short and will learn about them in detail in our next learning sessions in this blog.

Scalars can have any kind of value, viz. a character, a whole number, an integer, or a string. Scalars variable names starts with “$” symbol, as we learned in our first program. Further, for now just remember that array variables initialize with “@” symbol and hash variables starts with “%” symbol.

Also, I like one thing of PERL that it does not need to declare any variable before using it like other programming languages e.g., C and C++. We can use the variables directly without bothering about their declaration in the program.

So, this concludes your second day at PERL learning blog. Tomorrow we will go through some basics in scalar data types.

Hope you find today’s blogpost interesting.

With Warm Regards,
Yajur Kumar
(PERL Programming Expert)

No comments:

Post a Comment