Perl Programming/Scalar variables

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.

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.

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.

Naming Conventions

 * 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 $.

Scalar Variables and Strings
You may recall that earlier in the book, I said that whether you use <tt>"</tt> or <tt>'</tt> 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:

Would return "I saw 4 lions!"
 * With a double quoted string, this program:

Would return "I saw $variable lions!"
 * With a single quoted string, this program:

This effect is because of what I said before, single quoted strings are interpreted literally.

Comparison Operators

 * 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:


 * <tt>==</tt> - Equal to
 * <tt>!=</tt> - Not equal to
 * <tt><</tt> - Less than
 * <tt>></tt> - Greater than
 * <tt><=</tt> - Less than or equal to
 * <tt>>=</tt> - Greater than or equal to
 * <tt><=></tt> - Numeric Comparison

String Comparison Operators
Here is the list of string comparison operators:


 * <tt>eq</tt> - Equal to
 * <tt>ne</tt> - Not equal to
 * <tt>lt</tt> - Less than
 * <tt>gt</tt> - Greater than
 * <tt>le</tt> - Less than or equal to
 * <tt>ge</tt> - Greater than or equal to
 * <tt>cmp</tt> - String Comparison

Exercises

 * Try writing a program like the Hello World program except elaborate it by storing <tt>"Hello, world!\n"</tt> 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.