# Scalars are simple variables that hold one value
$myNum = 100; # Number
$decimal = -5.742; # Floating point number
$sampleText = "Hello World!"; # String
# Scalars can be references, which hold the location of data
$myRef = \$sampleText; # myRef holds the location of sampleText
# Dereference (get value at reference's location)
print ${$myRef}; # The first $ may change based on data type
@scores = (95, 86, 92);
@subjects = ("Math", "History", "Writing");
$myNum = $scores[0];
$faveSubject = $subjects[2];
# Hashes are key/value pairs
%ages; # declare an empty hash
$ages{'Bob'} = 35; # 35 is the value for the key 'Bob'
$ages{'Jane'} = 20;
$ageOfJane = $ages{'Jane'}; # Gets value 20 from the hash
$myNum = 0;
$myNum = $myNum + 1;
# + : Addition - : Subtraction
# * : Multiplication / : Division
# % : Modulus Division **: Exponent
if ($myNum == 0) {
$myNum = $myNum + 1;
}
# == : Equal to != : Not Equal to
# > : Greater than < : Less than
# >= : Greater than or equal to <= : Less than or equal to
if ($myNum > 1) {
$result = "myNum is greater than 1";
} elsif ($myNum < 1) {
$result = "myNum is less than 1";
} else {
$result = "myNum is equal to 1";
}
# While Loop
while ($myNum < 50) {
$myNum = $myNum + 1;
}
# For Loop
# initial value; condition; increment
for ( $i = 0; $i < 10; $i++ ) {
$myNum = $myNum + 1;
}
# Subroutines are very similar to functions from other languages
sub addValues {
my ($val1, $val2) = @_; # @_ is the array of arguments
return $val1 + $val2;
}
$sum = addValues(15, 42);