This is part 3 of my series, Basics of ActivePerl. In this part of the series, I give you the basics of ActivePerl variables. ActivePerl has variables, similar to mathematical variables. In the strict sense, they do not behave like mathematical variables. In this part of the series, we look at the meaning of ActivePerl variables and some of the type of values that can be assigned to them. The variable name is the name, you, the programmer give.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Example
Consider the following statement;
my $str = "This is the third part";
In the above statement, the variable is, $str, meaning, a particular string. The value "This is the third part" is in quotes. It is a string. It is assigned to the variable, $str, using what is called the assignment operator, "=".
When you are writing a variable the first time in your code, you begin with the reserved word, my, as shown above.
Consider now the following two consecutive statements:
my $str = "This is the third part";
print $str;
The first statement assigns the value, "This is the third part" to the variable, $str. The second statement sends the value assigned to the variable to the browser. The second statement has the, print function (in ActivePerl a function is also called a subroutine). What goes next to the function, print, is called an argument to the print function. So $str is an argument to the print function. There are many situations in computing where you would use the variable instead of the value. Try the following code:
use strict;
print "Content-Type: text/html\n\n";
print "\n";
print "\n";
print "Hello\n";
print "\n";
print "\n";
my $str = "This is the third part";
print $str;
print "\n";
print "\n";
To try the above code, save it in the directory, cgi-bin, with a name such as var.pl; in the address bar of your browser, type, http://localhost:8000/cgi-bin/var.pl . You should see the string, "This is the third part", displayed on your browser.
Numbers as Values
You can use numbers as values. You can assign a number to a variable. Consider the following two statements:
my $num = 56.48;
print $num;
The number, 56.48 is assigned to the variable, $num. When you are assigning a number, you do not have to put the number in quotes. The second line sends the number to the browser. Here, the argument to the print function is, $num. Try the following code:
use strict;
print "Content-Type: text/html\n\n";
print "\n";
print "\n";
print "Hello\n";
print "\n";
print "\n";
my $num = 56.48;
print $num;
print "\n";
print "\n";
To try the code, save the program with whatever name you want in the cgi-bin directory. Let the file name extension be pl. Use the address bar of your browser to execute the program, as done above. You should try (test) any code sample in this tutorial series, this way. If you tried the above program you should see the number, 56.48, on the browser.
Assigning a Variable to another Variable
You can assign a variable to another variable. Consider the following two statements:
my $str1 = "test";
my $str2 = $str1;
The first statement assigns the string value, "test" to the variable, $str1. The second statement assigns the variable, $str1 to $str2. The value of $str2 is "test" copied from str1.
Changing the Value of a Variable
You can assign a value to a variable and then change it after that. Consider the following statements:
my $str = "test";
my $str = "good";
The first statement assigns the value, "test" to the variable, $str. The second statement assigns a new value to the same variable. The final value of, $str is "good".
Case Sensitivity in ActivePerl
ActivePerl is said to be case sensitive. This means that for variable names, $theVar is not the same as $TheVar or $thevar or $THEVAR, etc.
Creating a Variable
Before you can use a variable, you have to create it. To create a variable, you begin with the $ sign and then the name of the variable; just as we have been doing above. You do not have to assign a value to a variable when you create it (but end it with a semicolon). You can do the assignment later. The following code illustrates this:
use strict;
print "Content-Type: text/html\n\n";
print "\n";
print "\n";
print "Hello\n";
print "\n";
print "\n";
my $theVar;
my $theVar = "you";
print $theVar;
print "\n";
print "\n";
A valid name for a variable begins with a letter or underscore ( _ ). After that, it can have any number of underscores, letters or digits.
When you create a variable without assigning any number to it as follows, we say you have declared the variable:
my $theVar;
When to use Quotation Marks for Values
If your value is a number, you do not have to use quotation marks. If your value is a string, you must use quotations marks.
String
A string is a value in quotes. The quotes may be single (') or double (").
Integer
An integer is a whole number. It could be a negative number, zero or a positive number.
Floating Point Number
A floating-point number is a number with a decimal point. There are several ways of writing it.
Literals
When used as a value, a string, integer or floating-point number is called a literal.
Boolean Value
A Boolean value is either true or false. ActivePerl accepts 1 for true and 0 for false. We shall see more on this as we go alone.
We have seen much. Let us take a break here and continue in the next part of the series. Always bear in mind that all the principles outlined in these tutorials work with conventional Perl. For the conventional Perl interpreter, do not forget to begin your program (code) with something like, #!/usr/bin/perl .
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
- Basics of PHP VariablesIn this part of the series, I explain the meaning of PHP variables.
- Basics of JavaScript VariablesIn this part of the series, I explain the meaning of JavaScript variables.
- Regular Expressions in Perl for the NoviceIn this article I explain the meaning of Regular Expressions and I introduce you to the concept in Perl. You start learning what is called Matching.
The Print Gocco DebacleEver heard of Print Gocco? Even if you haven't, it's likely you've seen things made with Gocco if you shop at Etsy.com or read blogs like Craft:. Sadly, this art niche is way pa...
How to Make a Potato PrintThere are many ways to create a print and printing is quite fun especially when trying unusual materials, a creative way to print is to do a potato print.
- How to Throw a Hello Kitty Theme Birthday Party
- Hello Kitty Printables
- Sizzix Sizzlits "Hello Kitty Heart" Die Cutter
- Free Hello Kitty Coloring Pages
- How to Make Your Own Hello Kitty Wall Clock
- Best Hello Kitty Birthday Invitations
- Free Printable Hello Kitty Coloring Activities



