Basics of Perl Variables
Perl Basics – Part 3
Perl Course
Foreword: In this part of the series, I explain to you the basics of Perl variables.
By: Chrysanthus Date Published: 29 Mar 2015
Introduction
Example
Consider the following statement;
my $str = "This is the third part";
In this 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 displays the value assigned to the variable at the console. The second statement has the, print function (in Perl a function is also called a subroutine). What goes next to the function, is called an argument to the 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 (type, save and execute):
use strict;
my $str = "This is the third part";
print $str;
To try this code, type it in a blank text editor; save it in the root directory, with a name such as, temp.pl; at the C:\> prompt, type the name, temp.pl, and you should see the result at the console. You should see the string, "This is the third part", printed at the console.
Note: in Perl a variable begins with a $ character.
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 console. Here, the argument to the print function is, $num. Try the following code:
use strict;
my $num = 56.48;
print $num;
To try the code as done above, save the program (script) with whatever name of your choice (e.g. temp.pl), in the root (C:\) directory. Let the filename extension be pl. At the C:\> prompt at the console, type the name of the file (including the extension) and press Enter. You should try (test) any code sample in this tutorial series, this way. If you tried the above script, you should see the number, 56.48, displayed in a new line at the console.
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. Consider the following statements:
my $str = "test";
$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”. Note that for the second statement, “my” was not retyped. For the same variable, you type “my” only once (without any quotes), the first time.
Perl 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 you have seen 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;
my $theVar;
my $theVar = "a plate";
print $theVar;
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 quotation marks.
String
A string is a value in quotes. The quotes may be single (') or double ("). If the first quote is single, the second closing quote must be single; if the first quote is double, the second quote must be double.
Integer
An integer is a whole number. It can be a negative number, zero or a positive number, e.g. –5, 0 or +5 . A positive number can be written without the + sign.
Floating Point Number
A floating-point number is a number with a decimal point, e.g. 2.5 . There are several ways of writing it – see later.
Literals
When used as a value, a string, integer or floating-point number is called a literal. An example of a string literal is, "I love you.". an example of an integer is, 25. An example of a float number is 32.369 .
A Boolean value is either true or false. Perl accepts 1 for true and 0 for false. You will see more on this as we go alone.
The undef Value
When a variable is declared as in the statement,
my $var;
the value assigned to it by Perl (interpreter) is the constant value, undef. The assignment is done during execution of the program, unknown to you. So, the following two statements are the same:
my $var;
my $var = undef;
undef for undefined, must be typed in lowercase.
Concatenation of Strings
“Concatenation Strings” means joining strings. You can join strings using the dot, called, the dot operator. The following code illustrates this:
use strict;
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 . "\n" . $strn;
In the first statement after, “use strict;”, the string, " and the second part" is joined to the end of the string, "the first part" using the dot operator. You can join strings in their literal forms or as variables, using the dot operator. Down in the code, the expression, “$left . $right”, which has variables, joins the values of the variables, with the dot operator. The argument of the print function this time, is the concatenation of the value of $str, the literal "\n" and the value of $strn. Remember, "\n" is not printed; its purpose is to make sure that what is printed after it, goes to the next line, at the console.
Try the code. If you try the code, you will have the following as output:
the first part and the second part
This is the left and this is the right
Now, with your knowledge of concatenating strings, instead of coding,
print "Hello World! \n";
you should code,
print "Hello World!" . "\n";
This second statement is preferable because it separates the output of interest from \n, which is an instruction to send the cursor to the next line.
We have done 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 different Perls. For the non-windows’ Perl interpreter, do not forget to begin your script (code) with something like, #!/usr/bin/perl .
Chrys
Related Links
Perl BasicsPerl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT