Skip to content

Commit 1dfe544

Browse files
authored
Update/add perl questions and answers (bregman-arie#188)
* New questions and spell check (bregman-arie#181) Added new questions related with KVM, Libvirt and DNF * New perl questions and answers. Handle errors. Open3. Packages.
1 parent 764c742 commit 1dfe544

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

exercises/perl/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,20 +486,116 @@ $b->printA();
486486
```
487487
</b></details>
488488

489+
### Perl Exception Handling
490+
491+
<details>
492+
<summary>How can we evaluate and capture an exception in Perl?</summary><br><b>
493+
494+
From the official [eval docs](https://perldoc.perl.org/functions/eval):
495+
496+
"`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.".
497+
498+
e.g:
499+
500+
```
501+
eval {
502+
die;
503+
};
504+
if ($@) {
505+
print "Error. Details: $@";
506+
}
507+
```
508+
509+
If we execute this we get the next output:
510+
511+
```
512+
Error. Details: Died at eval.pl line 2.
513+
```
514+
515+
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.
516+
517+
</b></details>
518+
489519
### Perl OS
490520

491521
<details>
492522
<summary>What is Perl Open3?</summary><br><b>
523+
524+
From the official [IPC::Open3 docs](https://perldoc.perl.org/IPC::Open3):
525+
526+
"IPC::Open3 - open a process for reading, writing, and error handling using open3()".
527+
528+
With `open3` we can have the full control of the STDIN, STDOUT, STDERR. It's usually used to execute commands.
529+
</b></details>
530+
531+
<details>
532+
<summary>Using Open3: Create a file with the size of 15MB and check it's created successfully</summary><br><b>
533+
534+
- Code:
535+
536+
```
537+
use IPC::Open3;
538+
use Data::Dumper;
539+
540+
sub execute_command {
541+
my @command_to_execute = @_;
542+
my ($stdin, $stdout, $stderr);
543+
eval {
544+
open3($stdin, $stdout, $stderr, @command_to_execute);
545+
};
546+
if ($@) {
547+
print "Error. Details: $@";
548+
}
549+
close($stdin);
550+
return $stdout;
551+
}
552+
553+
my $file_name = 'perl_open3_test';
554+
&execute_command('truncate', '-s', '15M', $file_name);
555+
my $result = &execute_command('stat', '-c', '%s', $file_name);
556+
print Dumper(<$result>);
557+
```
558+
559+
- Result:
560+
561+
```
562+
$ -> perl command.pl
563+
$VAR1 = '15728640
564+
';
565+
```
566+
493567
</b></details>
494568

495569
### Perl Packages & Modules
496570

497571
<details>
498572
<summary>What is a Perl package? And a module?</summary><br><b>
573+
574+
With a Perl package we are defining a namespace.
575+
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.
499576
</b></details>
500577

501578
<details>
502579
<summary>What is the difference between .pl and .pm extensions?</summary><br><b>
580+
581+
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.
582+
583+
</b></details>
584+
585+
<details>
586+
<summary>Why a Perl class (or module) should return something at the end of the file? Check the example.</summary><br><b>
587+
588+
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.
589+
590+
```
591+
package A;
592+
593+
sub new { return bless {}, shift; };
594+
sub printMethod { print "A\n"; };
595+
596+
1;
597+
```
598+
503599
</b></details>
504600

505601
<details>

0 commit comments

Comments
 (0)