eWebProgrammer eweb  





Perl Variables  «Prev  Next»
Lesson 3 Introducing Perl Scalars
ObjectiveWhat is the syntax for a Perl Scalar Variable?

Perl Scalar Variable Syntax

All scalar variables are introduced by the $ symbol. The actual name of the variable may include any of the characters in the ASCII set,
alpha (a-z, A-Z), numeric (0-9), and the underscore ( _ ) with the exception that the first character may not be numeric.
The $ symbol is used whenever the variable is referenced:
Diagram of a scalar
A scalar is one-dimensional.


# initialize  a scalar to a string
  $food = "spaghetti";
# initialize an integer number
  $quan = 4;
# initialize a floating-point number
  $price = "2.49";
# initialize a mathematical result
  $total = $quan * $price
# pass a scalar to print
  print "$food\n";
# initialize one scalar from another
  $meal3 = $food;          

In the above example, the string spaghetti is a scalar value, as is the number 4. The result of the expression, $quan * $price, is also a scalar value.

Scalar Definition

Any value that represents only one thing (that is, a single-dimensional value) is a scalar.

Perl Scalar Type - Quiz

Click the Quiz link below to take a short multiple-choice quiz on scalars. Understanding the basic use of the scalar type will allow us to build on that knowledge in the next lesson by learning about the differences between numeric and string context.
Perl Scalar Type - Quiz