Perl Variables - What They Are and How to Use

In this Perl tutorial, we will learn what variables are, what they are for, how they work, and how to use them. Also, let's meet the vigil, $ symbol, and assignment operator =

Storing Information in Perl

When you select a piece of text in some editor, you can paste it later as many times as you like. If the computer stays on for 10 years, you can still paste after 10 years.

When you play a game and lose when you failed some stage, you return to the same level with the same and items you have collected.

When you listen to music on your phone, stop listening, and then listen again (no matter how long later), you come back to the exact same point as before.

So, does the editor guess what you copied? Does the music app guess which song and where you stopped listening? Obviously not, if they know this information is because it was stored, saved somewhere.

Storing and using saved information is one of the cornerstones of programming, and we will now learn, in Perl programming, how to store information to use throughout our scripts.

In the future we will learn how to store values ​​in text files, for example.

This saves you information that will be permanently written to your machine even after your script has finished executing.

Perl variable: How to declare and initialize

Let's first learn how to store information temporarily, that is, during the duration of our Perl scripts execution, and only 'scalar' type information (we will study other types of information later, such as Hashes and Arrays).

This information storage is because the variables.
Variable is nothing more than that: a thing that stores data.

Every scalar type variable (such as numbers and strings) has a name and begins with the $ (vigil) symbol. Unlike other languages, in Perl you don't have to declare variables beforehand, you can quickly use them without wasting time, and you don't have to say what kind of information you want to store.

For example, let's store the number 2112 in the name variable number:

  • $number = 2112;


That's it. The $ symbol, a name, the assignment operator (=), and the information you want to initially store.

But what is the use of this? What is the purpose of using variables in Perl?
Let's find out.

Variables: What are they for?

When we declare a variable, Perl goes there and reserves a block in your computer's memory.
For example, it might reserve block number: 0x34671FF

But hey, programming is supposed to be easy and simple ... keep using this ugly number is not cool, do you agree? And if instead of calling 0x34671FF we could call by an easier name, what do we choose?

How about calling $number?
Every time we use the  number variable, Perl understands that you are referring to the value stored at position 0x34671FF in your memory.

Perl Variable: How to Use

Declare and initialize an age variable with the value of its age.
For example:

  • $age = 20;

Now let's make a script that prints the text on the screen: "You're [your age] years old."
In Perl, it looks like this:
#!/usr/bin/perl

$age=20;
print "You're $age years old\n";
Note that the "$age" did not appear on the screen, but a number.

That is, Perl interpreted, he thinks:
- "Oops! Here is a thing starting with the $symbol, so it's a variable ... instead of printing the variable name, I'll print the value it stores ... let me search here what is there at that address from memory".


And bang, it prints the number you initially stored in the variable:
How to programming in Perl - Tutorials

We're working with strings and variables, we can do that using the concatenation operator as well:
#!/usr/bin/perl

$age=20;
print "Your age is: ".$age."\n";
Note that we do not need to put quotation marks around the variable name.

Now let's store a string in a variable.
Enter your name in the $ name variable, for example:

  • $ name = 'Neil Peart';

Now a script that gives you good morning:
#!/usr/bin/perl

$name = 'Neil Peart';
print "Good morning, $name\n";

We can do this too:
#!/usr/bin/perl

$name = 'Neil Peart';
print "Good morning, ", $name;

And until then:
#!/usr/bin/perl

$name = 'Good morning, Neil Peart';
print $name;
This Perl is really very flexible, don't you think?

Perl Variable Exercises

"Write a script that has a variable that stores any numeric value and displays the twice."

Let's store the number 2112 in the $num variable.
In the $doub variable, we store twice the $num (just multiply this by two).

See how our script looks:
#!/usr/bin/perl

$num = 2112;
$doub = 2 * $num;
print "Twice of ", $num, " is ", $doub;

"Make a script that stores the numbers 21 and 12 in two different variables and displays their sum"

Let's store in the variables $num1 and $num2, each value.
In the $sum variable we will store the sum ($num1 + $num2), see:
#!/usr/bin/perl

$num1 = 21;
$num2 = 12;
$soma = $num1 + $num2;
print "Sum: $soma";

You could also do it directly in print:
#!/usr/bin/perl

$num1 = 21;
$num2 = 12;
print "Sum: ", ($num1+$num2);

The above way is more efficient as it declares only two variable.
We can also use just one variable, already starting with the sum directly:
#!/usr/bin/perl

$num = 21+12;
print "Soma: $num";

Do you understand now why Perl's motto is "There are always several ways to do the same thing"?

"Write a script that initializes with two variables with two values. Then show the sum, subtraction, multiplication, division, and remainder of the division of the first by the second number."

This we leave with you, write your script in the comments.

No comments:

Post a Comment