A Wikibookian suggests that this book or chapter bemerged withPerl_Programming/Scalar_Variables. Please discuss whether or not this merger should happen on thediscussion page. |
| Previous: First programs | Index | Next: Strings |
In Perl, there are five different types of variables, each indicated by its own sigil character: scalars ($), arrays (@), hashes (%), subroutines (&), and typeglobals (*). We first introduce scalars, as they are easiest to understand.
Although they have a complicated name, a scalar variable is just a way of storing a value for later use. For example, consider:
#!/usr/bin/perlprint"Hello, John.\n";
If we wanted to change the name of the user, we can just change the string. However, if this program were hundreds of lines long and the name was used several times, trying to find the strings throughout the program would be more fuss than it was worth. Instead, it is common to use variables for any values that might change.
#!/usr/bin/perlmy$firstname="John";print"Hello, $firstname.\n";
This new program gives exactly the same output as the previous one. There are several new things about this program, the most obvious being the line
While using variables to contain information that you have programmed is all very well, variables are used to store data that can change while your program runs, as their name implies.
#!/usr/bin/perlmy$firstname="Jonathan";print"Hello, $firstname.\n";$firstname="John";print"Goodbye, $firstname.\n";
You should notice that when we change the value of$firstname we do not need to use themy operator. This is because we have already told Perl to give us the variable, and it is now ours to do what we wish with.
Now, while strings are very useful, there is a place in computer programs for numbers as well. A scalar variable can contain either a number or a string.
#!/usr/bin/perlmy$age=17;print"Hello, $age year old.\n";
Perl does not provide any easy way of telling the type of information stored in a scalar variable. Indeed, if you give Perl a string and ask it to do number operation, Perl will automatically convert the string into the number; and vice versa. Here is an example:
#!/usr/bin/perlmy$x=10;my$y=$x+1;print"Using a number $x + 1 = $y.\n";$x="10";$y=$x+1;print"Using a string $x + 1 = $y.\n";$x="ten";$y=$x+1;print"Using an English word, $x + 1 = $y.\n";$x="2ten";$y=$x+1;print"Using a funny string, $x + 1 = $y.\n";
If you run this program, you can see that both the first two examples result in 11 being stored to $y. Perl does not convert English words to numbers, though, and if you give a string like"ten" it will not turn it to 10, but instead just think it is 0. The last example shows that it actually tries to understand the string as a number, and stops whenever it finds something it doesn't understand.
It is of course possible to output variables without including them in a string.
#!/usr/bin/perlmy$forename="John";my$message="Hello ";print$message;print", ";print$forename;print"\n";
|
| Previous: First programs | Index | Next: Strings |