Understanding Scalar Values

A Scalar Value is one unit of data, the data can either by a number or a chunk of text. All scalar variable have an initial value of the null string if not assigned, this means that you do not need to define.

In scalar values, a value that was assigned as a string can be used as a integer whenever it makes sense to do so and vice versa.

Scalar Values
Integer

Can contain one or more digits, optionally proceeded by either a plus or minus sign and optionally containing underscores. There is a limit of how many digits can be contained in the number, this depends on the type of machine you use.

To use octal number you start the number with a 0 (zero)

Floating-Point

A floating point number consists of the following

  • An optional minus sign (-)
  • A sequence of digits, optionally containing a decimal point
  • An optional exponent

The exponent represent the value by which to multiply or divide the scalar value by a power of ten. the exponent e+01 means multiply by ten, e+02 means multiply by 100 and e+03 means multiply by 1000, etc. The exponent e-01 means divide by ten, e-02 means divide by 100 and so on.

Remember that round-off will occur when perl meets it maximum limit, this depend on what machine you use.

Octal and Hexadecimal

Perl also enables you to use two other notations to represent integer scalar values

  • Base 8 (octal)
  • Base 16 (hexadecimal)
Character Strings

A character string is a sequence of one or more letters, digits, spaces or special characters. When you assign a string variable you can either use double quotes (variable substitution) or single quotes (no substitution).

Variable substitution means that when Perl sees a variable ( starts with a dollar sign($) ) inside the double quotes it knows that this is a variable and substitutes it for the variable's value.

There are lots of escapes sequences for character strings and need to be in double quotes to work, see Perl Cheat sheet for more details

Scalar Value Examples
Integer

$salary = 10000;
$pay_reduction = -100;

Floating-Point

$mars_bar = 0.50;
$breakages = -1.50;
$bonus = .1;

$a_large_number = 1.2e+05;          # remember that e+05 means multiply by 100000
$a_tiny_number = 1.2e-05;           # remember that e-05 means divide by 100000

Octal and Hexadecimal $result = 047;                      # this assign octal 47 or decimal 39 ( notice the leading 0 (zero) )
$result = 0x1f;                     # this assign hexadecimal 1F or decimal 31 ( notice the leading 0x)
Character Strings

$string = "Hello World";

## Substitution (double and single quotes)
$string = "You have $balance in your bank";   # using double quotes $balance will be substituted with the value of $balance
$string = 'You have $balance in your bank';   # will not substitute $balance because single quotes means take literary        

## Using escape sequences
$string = "Hello World!\n";         # notice the new line feed escape sequence
$string = "Hello World!\a";         # beep when you print this
$string = "\uhello World!\n";       # \u forces the h to become upper case

## Using a string as a integer
$string = "43";
$number = 57;
$result = $string + $number;        # the value of string is converted into a number and then added