Perl Programming/Scalar variables
| A Wikibookian suggests that this book or chapter be merged with Perl Programming/Basic Variables.
Please discuss whether or not this merger should happen on the discussion page.
Introduction to Scalar Variables
Now that you understand how to use strings and numbers in Perl, you need to start learning how to use variables. The best way to learn about scalar variables - Perl talk for a single variable, as against a group or list of values - is to look at an example.
#!/usr/bin/perl use warnings; $my_scalar_variable = "Hello, Sir!\n"; print $my_scalar_variable;
Now let's break this program down:
- The first two lines you already know, #!/usr/bin/perl and use warnings;
- The third line is more interesting, it contains a scalar variable. There are a few important things to point out:
- In case you haven't figured this out, the scalar variable in this line is $my_scalar_variable
- Notice the $ before the name my_scalar_variable, in order to define a scalar variable, this sign must appear before the name.
- Now let's look at the last line. This is just the familiar print function being told to print the value of $my_scalar_variable.
Type in the program mentioned above and run it.
Assigning and Using Scalar Variables
In the course of writing a program, you will most likely use a variable. What is a variable? A variable is something that stores data. A scalar variable holds a single value.
- All scalar variables names must start with a $ symbol. You can remember this by thinking $scalar.
- Variable names can be comprised of alphanumeric characters and underscores.
- Numeric characters are allowed in names of variables, but not as the first character after the $.
Using Scalar Variables
Scalar Variables and Strings
You may recall that earlier in the book, I said that whether you use " or ' in strings makes a big difference in the interaction of strings and variables. Well now I am going to explain what I meant.
Now that you know what a variable is, what if you wanted to put a variable in a string? Here's the difference:
- With a double quoted string, this program:
#/usr/bin/perl use warnings; $variable = 4; print "I saw $variable lions!";
Would return "I saw 4 lions!"
- With a single quoted string, this program:
#/usr/bin/perl use warnings; $variable = 4; print 'I saw $variable lions!';
Would return "I saw $variable lions!"
Type in the programs mentioned above and run them.
This effect is because of what I said before, single quoted strings are interpreted literally.
- Main article: Perl Programming/Operators
There are operators that are used for comparing numbers and strings. This can be very useful when you get to more advanced programming. Both numbers and strings have their own set of operators which test for a condition such as equal or not equal and return either true or false.
Numeric Comparison Operators
Here is the list of numeric comparison operators:
- == - Equal to
- != - Not equal to
- < - Less than
- > - Greater than
- <= - Less than or equal to
- >= - Greater than or equal to
- <=> - Numeric Comparison
String Comparison Operators
Here is the list of string comparison operators:
- eq - Equal to
- ne - Not equal to
- lt - Less than
- gt - Greater than
- le - Less than or equal to
- ge - Greater than or equal to
- cmp - String Comparison
The two 'Comparison' operators <=> and cmp are slightly different from the rest. Rather than returning only true or false, these operators return 1 if the left argument is greater than the right argument, 0 if they are equal, and -1 if the right argument is greater than the left argument.
- Try writing a program like the Hello World program except elaborate it by storing "Hello, world!\n" in a variable and then printing the variable.
- Play around with all the things we have learned so far. Try to create a program that has an example of everything we have learned so far.