From the official [docs](https://perldoc.perl.org/):
"Perl officially stands for Practical Extraction and Report Language, except when it doesn't."
It's a general purpose programming language developed for manipulating texts mainly. It has been used to perform system administration tasks, networking, building websites and more.
</b></details>
<details>
<summary>What data types Perl has? And how can we define it?</summary><br><b>
- Scalar: This is a simple variable that stores single data items. It can be a string, number or reference.
```
my $number = 5;
```
- Arrays: This is a list of scalars.
```
my @numbers = (1, 2, 3, 4, 5);
# or using the `qw` keyword (quote word):
my @numbers = qw/1 2 3 4 5/;
# '/' can be another symbol, e.g qw@1 2 3 4 5@
```
- Hashes (or associative arrays): This is an unordered collection of key-value pairs. We can access to a hash using the keys.
```
my %numbers = (
First => '1',
Second => '2',
Third => '3'
);
```
</b></details>
<details>
<summary>How can you access to a hash value, add and delete a key/value pair and modify a hash?</summary><br><b>
```
my %numbers = (
'First' => '1',
'Second' => '2',
'Third' => '3'
);
```
- Access:
```
print($numbers{'First'});
```
- Add:
```
$numbers{'Fourth'} = 4;
```
- Delete:
```
delete $numbers{'Third'};
```
- Modify:
```
$numbers{'Fifth'} = 6;
$numbers{'Fifth'} = 5;
```
</b></details>
<details>
<summary>How can you iterate an array? And a hash?</summary><br><b>
- Array:
```
my @numbers = qw/1 2 3 4 5/;
# Using `$_` that represents the current iteration in a loop. It starts from index array 0 until the last index.
foreach (@numbers) {
print($_);
}
# Output: 12345
# "$#" returns the max index of an array. That's the reason because we can iterate accesing to the array from the index 0 to the max index.
for my $i (0..$#numbers) {
print($numbers[$i]);
}
# Output: 12345
# Using the `map` keyword:
print map {$_} @numbers;
# Output: 12345
# Using `while`. We should take care with this option. When we use `shift` we're deleting the first element of the array and assigning it to the `element` variable.
# After this `loop` the `numbers` array will not have elements.
while (my $element = shift(@numbers)) {
print($element);
}
# Output: 12345
```
- Hashes:
```
my %capital_cities = (
'Madrid' => 'Spain',
'Rome' => 'Italy',
'Berlin' => 'Germany'
);
# Iterate and get the `keys`:
foreach my $city (keys %capital_cities) {
print($city . "\n");
}
# Iterate and get the `values`:
foreach my $country (values %capital_cities) {
print($country . "\n");
}
# Iterate and get the values and keys (first option):
<summary>What is a Perl subroutine? How to define it?</summary><br><b>
It's the perl model for user defined functions (this is also called function like other programming languages). We can define a subroute with the keyword `sub`.
```
sub hello {
print "hello";
}
```
</b></details>
<details>
<summary>Describe the different ways to receive parameters in a subroutine</summary><br><b>
- List assignment: Using the `@_` array. It's a list with the elements that are being passed as parameters.
```
sub power {
my ($b, $e) = @_;
return $b ** $e;
}
&power(2, 3);
```
- Individual assigment: We should access to every element of the `@_` array. It starts from zero.
```
sub power {
my $b = $_[0];
my $e = $_[1];
return $b ** $e;
}
&power(2, 3);
```
- Using `shift` keyword: It's used to remove the first value of an array and it's returned.
<summary>How can we evaluate and capture an exception in Perl?</summary><br><b>
From the official [eval docs](https://perldoc.perl.org/functions/eval):
"`eval` in all its forms is used to execute a little Perl program, trapping any errors encountered so they don't crash the calling program.".
e.g:
```
eval {
die;
};
if ($@) {
print "Error. Details: $@";
}
```
If we execute this we get the next output:
```
Error. Details: Died at eval.pl line 2.
```
The `eval` (`try` in another programming languages) is trying to execute a code. This code fails (it's a die), and then the code continues into the `if` condition that evaluates `$@` error variable have something stored. This is like a `catch` in another programming languages. At this way we can handle errors.
A Perl module in one simple word can be defined as a `class`. When we create a `class` in Perl we use the `package` keyword. A module can be used with the `use` keyword.
There's no a real difference between a `.pm` and `.pl` extensions. Perl use `.pm` extensions just to difference it as a perl module (a class). `.pl` extensions are usually named for perl scripts without OOP classes.
</b></details>
<details>
<summary>Why a Perl class (or module) should return something at the end of the file? Check the example.</summary><br><b>
If we want to `use` a Perl module (`import` a class), this module should end in a value different than 0. This is necessary because if we try to import the class and it has a false value, we will not be able to use it.