Some ActivePerl Predefined Subroutines

Basics of ActivePerl - Part 16

Chrys Forcha
Introduction
This is part 16 of my series, Basics of ActivePerl. In this part of the series, we look at some ActivePerl predefined subroutines. A predefined subroutine is a subroutine that has been defined for you in the ActivePerl interpreter. Most of the rules outline in this series are applicable to traditional Perl.

Parentheses
We have used some Perl functions with parentheses and others without parentheses. Perl functions do not need to have parentheses. In this tutorial I will use Perl functions without parentheses. Remember, another name for subroutine is function.

The print Function
In simple terms, the syntax of the print function is,

print LIST

where LIST is a list of arguments separated by commas.

Read and try the following code:

use strict;

print "Content-Type: text/html\n\n";

my $first = "one";
my $second = "two";
my $third = "three";

print $first, " ", $second, " ", $third;

The above code has two print statements. In order to send your output to the browser, you need the first print statement (print "Content-Type: text/html\n\n"). Here, I will talk about the second print statement. This statement begins with the print function, which is followed by 5 arguments. Two of the arguments are spaces, to provide spaces between the three arguments (variables), at the output.

Variables in Strings
If a string is in double quotes and it has as part of its content, a variable, the value of that variable will be displayed, in the case of the print function. For another function or in just a statement, the value will replace the variable. However, if the string is in single quotes, then the name of the variable will be displayed and not the value of the variable, in the case of the print function. For another function or statement, the value of the variable will still not replace the variable name; as long as you are using single quotes. Read and try the following code:

use strict;

print "Content-Type: text/html\n\n";

my $var = "good";

print "This is a $var man.", "
";
print 'This is a $var man.';

The first print statement displays "good" in place of $var, because it has double quotes. The second print statement displays $var and not the value, because it has single quotes.

The shift Function
The shift function removes the first element from the array and returns it, shortening the original array by one element. In simple terms its syntax is

shift ARRAY

If no argument is given, the @_ array is assumed. Remember, $_[0], $_[1], $_[2], etc. are the identifiers for the elements of the array, @_ .In the following function definition, the shift function is used against the @_ array, to print the first and second arguments sent to the function:

use strict;

print "Content-Type: text/html\n\n";

my $num1 = 4;
my $num2 = 5;

sub mySub
{
print shift;
print "
";
print shift;
}

mySub($num1, $num2);

Notice in the above function definition, that the shift function is argument to the print function. When the shift function does not take any argument, @_ is assumed, as in the above function definition. Recall that as soon as the function definition starts executing, @_ is in the function and it has the arguments sent. In the above function definition, each time the shift function is called, an element is removed from the top of the @_ array.

Concatenation of Strings
You can join strings using the dot operator. The following code illustrates this:

use strict;

print "Content-Type: text/html\n\n";

my $str = "the first part" . " and the second part";

my $left = "This is the left";
my $right = " and this is the right";

my $strn = $left . $right;
print $str, "
", $strn;

You can join strings in their literal forms or as variables, using the dot operator.

Well, there are many predefined functions (subroutines). I just wanted you to know that they exist. I will not discuss the rest of the predefined functions in this basic tutorial series. We end here and continue in the next part of the series.

Chrys

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.