Perl Variables: A How to Guide on Making Variables in Perl

perl variables manAs a programming language, Perl is not easy to pick up. It’s a high level language that’s used in many professional spaces. Perl shares many similarities to other programming languages, and it even borrows elements from them. One similarity that Perl shares with practically all other programming languages is the use of variables.

Variables are a way for you to assign a value or identity things in your program. For example, say you wanted to represent the number 15 in your program, you would write a variable and assign the number value 15 to that variable. The reason why variables are so important is because throughout your program, you will more than likely manipulate and change a variable several times. That number 15 may change to 300 or 7,000 by the time your program is done.

Understanding the different types of variables in Perl, and learning how to use them properly will be your first step into learning how to use this highly versatile language, but it may not be simple. This is especially true if you don’t have any experience with programming. If you really want to learn about programming, then you should start with the basics. Check out the Udemy course, Programming for Non-Programmers: Fundamentals.

What are the Different Types of Variables? 

Perl consists primarily of three different variable types. These variables are scalars, lists, and hashes. Each variable type is primarily used for one particular task.

The scalar variables are probably the ones that you will use the most often. These variables only contain one piece of information, but that information can be anything. In many other languages, such as C based languages, you would have to identify a variable as being a string, integer or real, or something else specific, but in Perl that is not the case. In Perl, a scalar variable can be anything you want to assign to it. You can create a variable and set up its value as a string or setup its value as an integer or double.

The other types of variable you will commonly see are lists. These are very similar to arrays. In other programming languages, an array is something that holds in a list of information. In Perl however, a list holds in different types of information, but it doesn’t have to be the same. Within your Perl list, you can have strings, floating point numbers, integers, and whatever else you want to house inside of it. This can cut down on programming and give you a bit more versatility with what you’re creating.

The final types of variable you will use are the hashes. Hashes are two-dimensional arrays. The first dimension is considered to be a key value and its purpose is to look through the records of data. The second dimension is the data.

The way you create these variables is by using different symbols, such as the $ and % signs. Of course there are several different types of symbols you will be using as you program. It’s important to learn about all the symbols, especially the Perl operators in order to properly create more complex programs.

How to Create Perl Variables 

It’s very easy to create variables in Perl and assign values to them. Below you will see how to create variables of the three different types shown to you before, as well as how you can use them.

$name = Alexander;
$number = 3.123213;
$numbers = 7;

Above are three different scalar variables. Notice that they all start with the $ sign. Whenever you want to create a scalar variable, you must always start it off with the dollar sign ($). Another important thing to notice is that the scalar variable can be assigned anything. A scalar variable was used to represent both a name and two very different types of numbers. Scalar variables are versatile.

Another thing you can do with scalar variables are make additions to them. For example, look at the variable $numbers. Since Perl isn’t limited to assigning variable with specific data types, you can easily do something along the lines of

$numbers = “7.0” . “1331” . “9”;

When you print this out, you would get 7.0.1331.9 as your variable.

@monthsOftheYear = (“January”, “February”, “March”, “April”, “May”, “June”, “July”);

Above is an example of a list. There are several different ways to create and initialize a list, but for now, you should just focus on the basics. As you can see, you start off creating your list with the @ symbol. After that you just enter in the information for what you want the list to hold. Each item is separated by a comma. Remember that you can put anything in your list, and it doesn’t even have to be of the same type. This means you can add in numbers as well as strings.

%people = (“Teacher”, “Mrs. Johnson”, “Student”, “Alex”, ”Principal”, “Dr. Smith”);

Above is a hash. Remember that a hash is a two-dimensional array. The first item in the array is a part of the first dimension of the array, this is the key value. The second item is a part of the second dimension in the array. Hashes are created with the % symbol. The above is a type of hash that creates several items at one time, but you can also build on a hash individually.

My (%people);
$people{“teacher”} = “Mrs. Johnson”;

The above is a hash as well. What you do is first initialize the hash itself as people, and then create scalar variables as different parts of the hash. It may be a little complicated, but you just have to continue practicing with it. After all, it’s important to learn how to use these variables as you will use them in different programs you create.

Perl can be used for various types of software creation, and with mastery in Programming you can even make things, such as games and apps. Check out the two Udemy courses Android Programming for the Absolute Beginner and Game Development and Coding for Children to see what sort of things you can create with just a small amount of programming knowledge.

Programming as a Profession 

Many people take what they learn in programming and use it to help them jumpstart their career. There are many ways to become a professional programmer. You should try the Udemy course, Programming for Entrepreneurs – jQuery to see just one of the ways you can turn programming into a great way to make money.