Monday, January 30, 2012

Learning PERL | Day 6


Hi friends! In our last blog learning session, we grabbed some knowledge about the flow of control in PERL programming by using ‘if-else’ statements. Let us understand today these ‘if-else’ statements by means of some examples.

Consider you are asked to build a program which can classify the customers in a bank as premium, important and regular customer on the basis of the balance they have in their bank accounts. Classification is to be made like this: 

“those customers with amount deposited over $1,000,000 are to be treated as premium; those having amount deposited between $500,000 and $1,000,000 are to be treated as important customer; and finally, those left, i.e., customers with deposits below $500,000 are to be treated as regular customers.”

This problem can be converted into a PERL program which can be used to distinguish the various classes of customers. Let us see how it is done:

if ($balance > 1000000)
{
  print “\nPremium Customer.”;
}
elsif ($balance > 500000)
{
  print “\nImportant Customer.”;
}
else
{
  Print “\nRegular Customer.”;
}


In the program illustrated above, the flow is designed in such a way that there is the need of only one condition to be specified in the body of execution. ‘Body of Execution’ is the block below each condition, it is included in the curly-brackets ( { <body> } ).

One more thing to notice here is that we do not put terminator (;) after the if or elsif or else statement. It is because the statement as a whole does not terminates at the same line, if found correct, it’s body will be executed, which is in the next line starting with ‘{‘ and ending with ‘}’. It is a common mistake to put terminator after the flow statement.

One more that I want to discuss today is the use of comments in our programs. Suppose you are working on a big-big and complex program which uses various formulae and statements. It is a usual thing that you may not be able to grasp your own coding if viewed later. So, to keep our program meaningful to us and to other programmers, we use some statements which help us in grabbing the idea about our program. These statements are completely ignored by the PERL interpreter.

In PERL, comments begin with ‘#’ symbol. In other words, if you put ‘#’ in a line, all the characters after become invisible to interpreter. Let us re-write the above program using comments to make it more illustrative.

if ($balance > 1000000)          # first condition
{
  print “\nPremium Customer.”;   #  Body of first condition
}
elsif ($balance > 500000)        # second condition
{
  print “\nImportant Customer.”; #  Body of second condition
}
Else                             #  Final condition
{
  Print “\nRegular Customer.”;  #  Body of final condition
}


This concludes today’s learning session on PERL learning blog. Hope you are finding these learning sessions interesting and easy to understand.


With Warm Regards,
Yajur Kumar
(PERL Programming Expert)

12 comments:

  1. Thank you friend! Due to busy schedule I was unable to update my blog. But, now I've got some free-time, and promise to update my this learning blog very soon.

    Thank you for bookmarking!

    ReplyDelete
  2. perfect and excellent thoughts, thanks for sharing your knowledge
    Inplant Training In Chennai
    Inplant Course In Chennai

    ReplyDelete

  3. Thank you, This was the best and most interesting course. please continue to share your thoughts.

    RPA Course in Chennai
    RPA Online Course
    RPA Course In Bangalore

    ReplyDelete