
Before we can talk about variables, we have to understand the concept of data types.
Data types are an 'abstraction', which is just a way of being able to talk about something - we have to give everything a name, otherwise how could we discuss it?.
Other abstractions are 'file handles', 'directory handles', 'formats', 'subroutines', 'symbol tables', and 'symbol table entries'. But we're only concerned with data types for now.
First we need to understand what scalar means.
Context should be familiar to you, we use it in our everyday conversations. Perl has a couple of them, so pay attention, this is important.
As mentioned, scalars are usually strings or numbers. So we expect string results when dealing with strings, and number results when dealing with numbers.
If we have an expression
3*7we expect to get
21and we do.
But this
3x7gives us
3333333instead.
The first is numeric multiplication, but the second is string replication. Context.
The result we get depends on which context we use.
Perl has 2 contexts:
An operation in scalar context returns the number of elements in the array, but list context gives us the contents.
@flints=("Barney","Fred","Wilma","Betty");
print "\@flints: @flints";
@flints: Barney Fred Wilma Betty
my @friends=@flints;
print "\@friends: @friends";
@friends: Barney Fred Wilma Betty
my $friends=@friends;
print "\$friends: $friends";
$friends: 4
These are sometimes referred to as data structures, but either one works.
You can also create your own data type using the object-oriented features of Perl.
A useful way of way of classifying variables is by what kind of data they can hold.
As in many human languages, we have singular and plural pieces of data. Strings and numbers are singular, while lists of strings or numbers are plural.
A singular variable is a scalar. Plural variables are arrays.
Arrays and hashes are aggregate data types - they are made up of simple scalars.
This might be a good place to explain the sigils for various variable types.>