Perl/Basics/Variables and Data Types
In strict perl, variables must be declared using the "my" or "our" operators. "my" is used implicitly in non-shared memory, whereas "our" is used explicitly for shared memory to pass data between threads.
Contents
Scalars
Scalars in perl are prefixed with a $. A scalar may contain any string, integer, or floating point value. It may also contain a reference pointer. An example declaration:
<syntaxhighlight lang="perl">my $message = "Hello world!\n"; print $message;</syntaxhighlight> |
Arrays
Arrays (or lists) have elements. Typically an array in perl can contain anything - each element can be something different. An array element may be a hash, hash reference, scalar, or another array.
Arrays are prefixed by the @ character:
<syntaxhighlight lang="perl">my @messages = ("Hello world!\n","I like perl!\n"); print $messages[0]; print $messages[1]; print "Size of messages array: ". $#messages . "\n"; </syntaxhighlight> |
You can access and modify array elements directly:
<syntaxhighlight lang="perl"> $messages[0] = "Hello world!\n"; </syntaxhighlight> |
Helper Functions
join()
Join will compile an array into a scalar. Using the array example above, @messages, the following code will generate the string "Hello world!\n, I like perl!\n" as a scalar:
<syntaxhighlight lang="perl">my @messages = ("Hello world!\n","I like perl!\n"); my $joined_message = join(", ",@messages); print $joined_message;</syntaxhighlight> |
split()
Split takes a scalar and converts it to an array using a delimiter. Using our string from earlier:
<syntaxhighlight lang="perl">my $joined_message = "Hello world!\n, I like perl!\n"; my @messages = split('/, /',$joined_message); print $messages[0]; print $messages[1]; print "Size of messages array: ". $#messages . "\n";</syntaxhighlight> |
push()
The push() function is used to append an element or elements to the end of an array, similar to the push instruction in assembly and treats the array like a stack.
my @array; push(@array,'element one'); push(@array,('element two','element three')); |
$array[$#array] = "new element";
pop()
The pop() function is similar to the pop instruction in assembly and treats the array like a stack.
my @array; $array[$#array] = 1; $popped = pop(@array); |
The same affect can be acheived with:
$popped = $array[$#array--]; |
Executing pop() on an array will delete the highest order array element. |
unshift()
The unshift() function is like the inverse of the push() function and treats the array like a stack. In stead of pushing to the top of the stack, this function operates against the bottom of the stack.
my @array; $array[0] = 1; unshift(@array,0); # $array[0] now contains "0" and $array[1] now contains [1]. |
shift()
The shift() function is like the inverse of the pop() function and treats the array like a stack. In stead of popping from the top of the stack, this function operates against the bottom of the stack.
my @array = (0,1); my $first_element = shift(@array); # $array[0] now contains one, and @array only contains one element |
Hashes
A hash is very similar to a struct in C.
Introduction
Hashes are prefixed by the % character. Hash element values are prefixed by $. A hash element may contain another hash, an array, or a scalar.
- You can directly modify the key inside of a hash
<syntaxhighlight lang="perl">$hash{'key'} = 'value';</syntaxhighlight> |
- You can also create a key => value pair on declaration
<syntaxhighlight lang="perl">my %hash = ('key' => 'value', 'key2' => 'value2');</syntaxhighlight> |
- Example:
<syntaxhighlight lang="perl">my %user; $user{'username'} = "hatter"; $user{'network'} = "irc.blackhatacademy.org"; print "The user " . $user{'username'} . " is connected to " . $user{'network'} . "\n"; </syntaxhighlight> |
Helper Functions
each()
"while my each" can be used to isolate $key => $value pairs from a hash as follows with our %user hash:
<syntaxhighlight lang="perl">while(my($key,$value) = each(%user)) { print "Key: $key, Value: $value\n"; };</syntaxhighlight> |
keys
This uses a foreach() loop and casting. We can isolate $key=>$value pairs the same as above using keys in stead of each:
<syntaxhighlight lang="perl">foreach my $key (@{sort keys %user}) { print "Key: $key, Value: ". $user{$key} ."\n"; };</syntaxhighlight> |
References
A reference is very similar to a pointer in C.
Hash References
A hash reference is a scalar created using the \ operator as follows:
my %user; $user{'name'} = "hatter"; $user{'network'} = "irc.blackhatacademy.org"; my $hashref = \%user; |
Once you've created a hashref (hash reference) you must use pointers to access a key:
print $user->{'name'} . "\n"; print $user->{'network'} . "\n"; |
Callback References
This involves user-defined functions. User-defined functions are covered later in this article. A callback reference is a scalar that points to a function. To create a callback reference:
my $callback = \&function_name; |
To execute the callback function and pass it arguments:
$callback->($arg1, $arg2); |
Casting
Casting is the process of transitioning from one data type to another. This is typically done using curly brackets {} preceeded by a data type designator ($,%, or @).
- To cast a hash reference back to a hash:
my %hash; my $hashref = \%hash; #create the hash reference my %casted = %{$hashref}; #Cast back to a hash. |
- To cast a list of keys in a hash into an array:
my @casted = @{keys %hash}; |
- To cast a scalar value to an integer:
my $integer = int($scalar); |