Reputation: 4588
In perl, you have three main data types, "scalars, arrays of scalars, and associative arrays of scalars". What exactly is perl trying to convey with the name "scalar"? What's the metaphor, the mental image that should be forming?
I can infer an analogy: single value, a list of values, a list of named values. But this doesn't help me understand why the word "scalar" is used. Perl's scalars surely do not resemble a ladder.
Upvotes: 5
Views: 1389
Reputation: 1
how about string "data" or number "data", a variable representing "data".
Upvotes: 0
Reputation: 93676
A scalar is a single distinct value, like 47 or "Bob Smith". Scalar variables use the $
sigil.
my $score = 47;
my $name = "Bob Smith";
An array is an ordered list of 0 or more scalars, indexable by a nonnegative integer. Array variables use the @
sigil.
my @scores = ( 47, 52, 34 );
my @names = ( "Bob Smith", "Susan Richardson", "Heather Jones" );
my $first_score = $scores[0]; # 47
my $last_score = $scores[-1]; # 34, indexed from the end
A hash or associative array is an unordered list of strings that map to scalars. Hash variables use the %
sigil.
my %scores = (
"Bob Smith" => 47,
"Susan Richardson" => 52,
"Heather Jones" => 34,
);
my $score_for_susan = $scores{"Susan Richardson"}; # Has the value 52
my $score_for_doug = $scores{"Doug Thompson"}; # Has the value undef
# because the key "Doug Thompson" does not exist in %scores.
It can be confusing that a hash like %scores
is expressed as $scores
when looking up a value.
Upvotes: 2
Reputation: 13942
This concept is described in principle in Variable (Computer Science) wherein it says:
In computer programming, a variable or scalar is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context.
In Perl, a scalar variable holds a single value. While a Perl scalar is typically associated with a symbolic name, it doesn't have to be; you can have a scalar that is only referred to by a scalar reference, and not paired with a symbol. The usage of the word "scalar" in computer programming probably derives from a similar usage in mathematics, as described in Scalar (Mathematics):
A scalar is an element of a field which is used to define a vector space. A quantity described by multiple scalars, such as having both direction and magnitude, is called a vector.
As applied to Perl, scalars are described in perldoc perldata:
A scalar may contain one single value in any of three different flavors: a number, a string, or a reference. In general, conversion from one form to another is transparent. Although a scalar may not directly hold multiple values, it may contain a reference to an array or hash which in turn contains multiple values.
Perl scalars, internally, store a few things; an IV (integer value), an NV (floating point numeric value), PV (pointer to string value), SV (a reference), a reference count, some various flags to keep track of if the scalar has Unicode semantics, other "magic", and which of the various storage types are in use. Not all of these buckets are in use at any given time, and which ones are populated depends both on how the scalar is defined, and on how it is used (a scalar holding a string may later populate the IV or NV fields, internally, if it is treated as a number). This is described in perlguts, but you usually don't need to worry about it.
You can informally consider a scalar to be a single unit of storage which may contain a number, a string, or a reference. And you may consider a scalar to be either a standalone variable, a unit of storage referred to by a scalar reference, or a single element of an array or hash.
Upvotes: 4