Today we will try to grab some facts about useful
built-in functions used in PERL. Actually, built-in functions provide us useful
means of manipulating operations in PERL without writing some complex codes.
One such instance of built-in function is square-root
function. It is denoted in PERL by using keyword “sqrt”. To understand its use,
let us have a look at the program code fragment given below:
$NUM = 16;
print sqrt $NUM;
print sqrt $NUM;
It will print the digit 4 on the screen. Another
‘good-looking’ way of writing this code fragment is to include the $NUM in
braces. Although, it is not required in PERL, yet, it increases the readability
of the program to you.
Another built-in function of interest is “int”. It is
used to print only the part of number before the decimal point. For example, if
used with 56.678, it will return 56 to the program. Let us understand its
concept through a code fragment:
$NUM = 56.678;
print int ($NUM);
print int ($NUM);
In a similar manner, the function ‘print’ prints
everything given to it on screen, as we unknowingly used it in our almost all
programs.
There are a series of arithmetic in-built functions in
PERL, which we deal in our coming lectures. At this point, I would like to
introduce some functions which are used to be operate on strings. One such
function is “length”. It is used to give the number of characters used in the
string. Let us take an example.
$Test_String = “This is a test string.”;
print length($Test_String);
print length($Test_String);
Provided the spaces are also count in characters, the
output of the above code-fragment is 22.
Another in-built string functions are “lc” and “uc”.
These are used for converting the string to all-lower case and all-upper case
respectively, irrespective in which case they are initially in.
Let us take an example to understand it:
print uc(“convert me to upper case”);
The above code fragment will print the string in all
upper-case as: CONVERT ME TO UPPER CASE. Similar method applies with the
built-in function “lc”.
In addition to these basic string functions, there are a
variety of other string functions used in PERL, which we deal in our coming
blog learning sessions. In addition to the functions which takes arguments to
perform an operation, there are functions, which need no argument to operate.
Such a function, which is usually prove important in game programming is “rand”
function.
“rand” function is used to print a random number between
0 and 9. Let us understand it with the help of an example.
print rand();
It will print any random number between 0 and 9, say, it
be 7.0304934458. However, sometimes, we need random numbers between certain
upper limit, say between 0 and 7.009. In this condition, the upper limit can be
treated as the argument of the “rand” function. Taking an example:
print rand(7.009);
It will print a random number between 0 and 7.009, say it
to be, 5.373643441, but it will never be 7.9344344351.
PERL has many in-built functions which prove useful in
various operations. We will learn about some important of them in our coming
blog learning sessions.
Hope you are finding these blog learning sessions useful
and enjoyable!
With Warm Regards,
Yajur Kumar
(PERL Programming Expert)
Yajur Kumar
(PERL Programming Expert)