Perl Variables  «Prev  Next»
Lesson 2Scalars in Perl
Objective Familiarize yourself with the Basic Use of the Scalar Type in Perl.

Basic Use of Scalar Type in Perl

The scalar type is the basic building block of all Perl data types. It is used to store all one-dimensional data. In fact, the multi-dimensional data types that we will learn about later are built from scalar data.
Scalar data can be either
  1. numeric or
  2. textual.
Here are some examples of scalar variables:

$name = "Bill";  # a string 
$age  = 842;     # a number

All scalar values exists in some memory space, even if it doesn't have a name. In fact, the only difference between a scalar variable and any other scalar value is that the variable has a name associated with it.

Examples of Perl Scalar Types

In Perl, scalar types are used to represent single values. Scalars can hold numbers, strings, or references to other data types. A scalar variable is denoted with a dollar sign ($) prefix. Here are some basic uses of scalar types in Perl:
  1. Numbers: Scalars can store and manipulate numbers, including integers and floating-point values. Perl automatically handles the conversion between number types when needed.
    my $integer = 42;
    my $float = 3.14;
    
    my $sum = $integer + $float;
    print "Sum: $sum\n";  # Output: Sum: 45.14
    
  2. Strings: Scalars can store and manipulate strings. You can use single or double quotes to define strings. Double quotes allow for variable interpolation and escape sequences, while single quotes treat the content as literal text.
    my $name = "Alice";
    my $greeting = "Hello, $name!";
    print "$greeting\n";  # Output: Hello, Alice!
    

  3. References: Scalars can store references to other data types, such as arrays and hashes. References enable you to create complex data structures, pass arrays and hashes to subroutines, and work with object-oriented programming.
    my @array = (1, 2, 3);
    my %hash = (key1 => 'value1', key2 => 'value2');
    
    my $array_ref = \@array;
    my $hash_ref = \%hash;
    
    print "Array reference: @{$array_ref}\n";  # Output: Array reference: 1 2 3
    print "Hash reference: %{$hash_ref}\n";    # Output: Hash reference: key1 value1 key2 value2
    
In summary, scalar types in Perl are versatile and can be used to store and manipulate numbers, strings, and references to other data types. Scalars are the building blocks for more complex data structures and operations in Perl.

Scalars

Tied scalars are the easiest to implement, since scalars do not do much. I can either store or access scalar data. For my special scalar behavior, I have to create two methods: STORE, which Perl calls when I assign a value, and FETCH, which Perl calls when I access the value. Along with those, I provide TIESCALAR, which Perl calls when I use tie, and possibly the DESTROY or UNTIE methods if I need them. The TIESCALAR method works like any other constructor. It gets the class name as its first argument, then a list of the remaining arguments. Those come directly from tie.

A large part of the design of Perl is driven by the dictates of visual psychology. That is why Perl lets you structure your code with the condition on the left or on the right, depending on which part you want to look important. That is why the large nested structures like while loops require an explicit beginning and end, while the small ones like list operators do not.
That is why scalars start with $, arrays with @, and hashes with %. That is why file test operators look like -M, while numeric tests look like ==, and string tests look like eq. Perl is very much a What-You-See-Is-What-It-Does language. You can talk about readability all you like, but readability depends first and foremost on recognizability.
A Perl scalar variable holds a single value, an array holds an ordered list of scalars, a hash holds an unordered collection of scalars as values, keyed by strings. Although a scalar can be an arbitrary string, which lets us encode complex data in an array or hash, none of the three data types are well suited to complex data interrelationships. This is a job for the reference.