All numbers are in double precision floating point format.
There are no integer values in Perl although integer literals may be used in decimal, octal and hexadecimal format.
There is an int function that returns the integer portion of a number and a use integer compiler directive that forces all arithmetic operations to be integer.
There is even a package Math::BigInt that alows arbitrary precision integer mathematics but this uses strings to represent the integers :-)
Strings are sequences of characters. Each character is an 8 bit value from the entire 256 character set.
This means that raw binary data can be created, scanned and manipulated as a string.
The shortest string has no characters ("").
The longest string fills all available memory (no unecessary limits).
Strings are delimited with single or double quotes.
Variable values are interpolated in double quoted strings.
A period "." is the string concatenation operator.
In addition to a wide range of numeric and string operators boolean operations are available with false being the null or empty string ("") or the number 0.
Three built in data structures are available in Perl:
You can define your own fancy dynamic data types using Perl's object oriented features.
A scalar is a single data item, either a number or a string, identified by a $ prefix.
$foo = "bar"; # assigns "bar" to scalar $foo $x = 123.4; # assigns 123.4 to scalar $x $foo = $x; # type juggling print "$foo\n"; # prints 123.4
A list is an ordered array of scalar data, identified by a @ prefix. Each element of the array is a separate scalar value. The shortest list has no elements. The largest list fills all available memory. Note the use of a $ prefix to extract scalar values from the list.
@mylist = ("rubble", 23, 17.5, $x); # assignment to @mylist $VAT = $mylist[2]; # assigns the value 17.5 to $VAT print "$mylist[3]\n"; # prints 123.4 @mylist = ("jack", @mylist, "jill"); print "$mylist[3]\n"; # prints 17.5
A hash is a collection of scalar data in which each element is referenced by some index value. A hash is identified by a % prefix, note the use of curly parentheses. Unlike a list the index values of a hash are not small positive numbers, they are arbitrary scalars called keys. So a hash is a set of key-value pairs.
%fred = ('aa', 'bb', 12.3, "dog"); # assign with key - value pairs $fred{23} = 45.6; # creates key 23, value 45.6 $fred{"bar"} = "17.5"; # creates key "bar", value "17.5" $fred{23} += 3; # makes key 23 take value 48.6 @fredlist = %fred; # @fredlist becomes ("aa", "bb", 12.3, "dog", 23, 48.6, "bar", "17.5") # although not necessarily in that order!
To see how this works print some values.
print "$fred{12.3}\n"; # prints dog print '$fred{12.3}\n'; # prints $fred{12.3}\n without a newline print '$fred{12.3}'."\n"; # prints $fred{12.3} with a newline
%fred = ( %fred, @mylist );
print '$fred{12.3} = "'. $fred{12.3} . "\"\n";
Another way of writing to a hash is to use the => 'corresponds to' operator. This is essentially a synonym for a comma but is visually distinctive and quotes any bare identifiers to the left of it.
%fred = ( aa => 'zz', 12.3 => 'dog' );
Overwriting existing data or adding more data to an existing hash.
%fred = ( %fred, bar => '17.5', 23 => 48.6, aa => 'bb' );
Is this %fred the same as the %fred that we saw previously?
(The corresponds to operator is also used with Perl records known as anonymous hash references and with the infix operator -> for call-by-named-parameter use of complicated functions. While this is beyond the scope of this lecture we will see it in action soon)
shift ARRAY
removes and returns the first item from the array
unshift ARRAY, LIST
prepends LIST to the fron ofthe array, returns the new length of the array
push ARRAY, LIST
adds the list to the end of the array, returns the new length of the array
pop ARRAY
removes and returns the last value from the end of the array
join EXPR, LIST
joins list items into a single string separated by the value of the expression
qw/STRING/
places quotes around the scalar values in the string
reverse LIST
returns the list values in reverse order
sort LIST
returns the sorted list
splice ARRAY, OFFSET, LENGTH, LIST
replaces length items from array starting from offset with list
keys HASH
returns a list of the keys from the hash
values HASH
returns a list of the values from the hash
delete EXPR
deletes the specified key and value from the specified hash
each HASH
returns a two element list containing the key value pair of the next item in a hash
exists EXPR
returns true if the hash expression exists