Skip to content

Commit 6aa674a

Browse files
author
abregman
committed
Merge branch 'master' of github.com:bregman-arie/devops-exercises into devel
2 parents 63a3b2e + 3f3f247 commit 6aa674a

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3253,6 +3253,7 @@ False
32533253

32543254
<details>
32553255
<summary>Explain what is GIL</summary><br><b>
3256+
Python Global Interpreter Lock (GIL) is a type of process lock which is used by python whenever it deals with processes. Generally, Python only uses only one thread to execute the set of written statements. This means that in python only one thread will be executed at a time
32563257
</b></details>
32573258

32583259
<details>

exercises/perl/README.md

+96
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)