Feature/new perl questions and answers (#185)

* New questions and spell check (#181)

Added new questions related with KVM, Libvirt and DNF

* New perl questions and answers

Added some questions and answers in the filehandle and OOP topics.
This commit is contained in:
Adrian 2021-11-19 09:39:20 +01:00 committed by GitHub
parent 1604ce467b
commit 14f99e2ef9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

165
README.md
View File

@ -5081,22 +5081,38 @@ my %numbers = (
<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> <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 = ( my %numbers = (
'First' => '1', 'First' => '1',
'Second' => '2', 'Second' => '2',
'Third' => '3' 'Third' => '3'
); );
```
- Access: - Access:
print($numbers{'First'});
- Add:
$numbers{'Fourth'} = 4;
- Delete:
delete $numbers{'Third'};
- Modify:
$numbers{'Fifth'} = 6;
```
print($numbers{'First'});
```
- Add:
```
$numbers{'Fourth'} = 4;
```
- Delete:
```
delete $numbers{'Third'};
```
- Modify:
```
$numbers{'Fifth'} = 6;
$numbers{'Fifth'} = 5; $numbers{'Fifth'} = 5;
```
</b></details> </b></details>
@ -5268,8 +5284,52 @@ sub factorial {
### Perl Files Handle ### Perl Files Handle
<details>
<summary>Mention the different modes in File Handling</summary><br><b>
- Read only: `<`
- Write mode. It creates the file if doesn't exist: `>`
- Append mode. It creates the file if doesn't exist: `>>`
- Read and write mode: `+<`
- Read, clear and write mode. It creates the file if doesn't exist: `+>`
- Read and append. It creates the file if doesn't exist: `+>>`
</b></details>
<details> <details>
<summary>How to write into a file?</summary><br><b> <summary>How to write into a file?</summary><br><b>
```
# We can use:
# '>' Write (it clears a previous content if exists).
# '>>' Append.
open(my $fh, '>>', 'file_name.ext') or "Error: file can't be opened";
print $fh "writing text...\n";
close($fh);
```
</b></details>
<details>
<summary>How can you read a file and print every line?</summary><br><b>
```
open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened";
my @file = <$fh>;
foreach my $line (@file) {
print $line;
}
```
We can use the file handle without assigning it to an array:
```
open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened";
foreach my $line (<$fh>) {
print $line;
}
```
</b></details> </b></details>
### Perl OOP ### Perl OOP
@ -5291,11 +5351,100 @@ The function os the `bless` function is used to turning a plain data structure i
</b></details> </b></details>
<details> <details>
<summary>Does Perl have inheritance?</summary><br><b> <summary>How to create a Perl class? How can you call a method?</summary><br><b>
- Let's create the package: `Example.pm`
```
package Example;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
sub is_working {
print "Working!";
}
1;
```
- Now we can instance the `Example` class and call `is_working` method:
```
my $e = new Example();
$e->is_working();
# Output: Working!
```
</b></details>
<details>
<summary>Does Perl have inheritance? What is the `SUPER` keyword?</summary><br><b>
Yes, Perl supports inheritance. We can read about it in the official [docs](https://perldoc.perl.org/perlobj#Inheritance).
We also can read about `SUPER` keyword that is used to call a method from the parent class. It gives an example about how we can apply inheritance.
</b></details> </b></details>
<details> <details>
<summary>Does Perl have polymorphism? What is method overriding?</summary><br><b> <summary>Does Perl have polymorphism? What is method overriding?</summary><br><b>
Yes, it has polymorphism. In fact method overriding is a way to apply it in Perl.
Method overriding in simple words appears when we have a class with a method that already exist in a parent class.
Example:
```
package A;
sub new { return bless {}, shift; };
sub printMethod { print "A\n"; };
package B;
use parent -norequire, 'A';
sub new { return bless {}, shift; };
sub printMethod { print "B\n"; };
my $a = A->new();
my $b = B->new();
A->new()->printMethod();
B->new()->printMethod();
# Output:
# A
# B
```
</b></details>
<details>
<summary>How can you call a method of an inherited class?</summary><br><b>
```
# Class `A` with `printA` method.
package A;
sub new { return bless {}, shift; };
sub printA { print "A"; };
# Class `B` that extends or use the parent class `A`.
package B;
use parent -norequire, 'A';
sub new { return bless {}, shift; };
# Instance class `B` allows call the inherated method
my $b = B->new();
$b->printA();
```
</b></details> </b></details>
### Perl OS ### Perl OS