From 1dfe544c6c29c1212e438e68c7c4cd4250aab18c Mon Sep 17 00:00:00 2001 From: Adrian <59370927+adrianfusco@users.noreply.github.com> Date: Mon, 22 Nov 2021 07:35:15 +0100 Subject: [PATCH 1/2] Update/add perl questions and answers (#188) * New questions and spell check (#181) Added new questions related with KVM, Libvirt and DNF * New perl questions and answers. Handle errors. Open3. Packages. --- exercises/perl/README.md | 96 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/exercises/perl/README.md b/exercises/perl/README.md index 546e435..b663d1c 100644 --- a/exercises/perl/README.md +++ b/exercises/perl/README.md @@ -486,20 +486,116 @@ $b->printA(); ``` +### Perl Exception Handling + +
+How can we evaluate and capture an exception in Perl?
+ +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. + +
+ ### Perl OS
What is Perl Open3?
+ +From the official [IPC::Open3 docs](https://perldoc.perl.org/IPC::Open3): + +"IPC::Open3 - open a process for reading, writing, and error handling using open3()". + +With `open3` we can have the full control of the STDIN, STDOUT, STDERR. It's usually used to execute commands. +
+ +
+Using Open3: Create a file with the size of 15MB and check it's created successfully
+ +- Code: + +``` +use IPC::Open3; +use Data::Dumper; + +sub execute_command { + my @command_to_execute = @_; + my ($stdin, $stdout, $stderr); + eval { + open3($stdin, $stdout, $stderr, @command_to_execute); + }; + if ($@) { + print "Error. Details: $@"; + } + close($stdin); + return $stdout; +} + +my $file_name = 'perl_open3_test'; +&execute_command('truncate', '-s', '15M', $file_name); +my $result = &execute_command('stat', '-c', '%s', $file_name); +print Dumper(<$result>); +``` + +- Result: + +``` +$ -> perl command.pl +$VAR1 = '15728640 +'; +``` +
### Perl Packages & Modules
What is a Perl package? And a module?
+ +With a Perl package we are defining a namespace. +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.
What is the difference between .pl and .pm extensions?
+ +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. + +
+ +
+Why a Perl class (or module) should return something at the end of the file? Check the example.
+ +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. + +``` +package A; + +sub new { return bless {}, shift; }; +sub printMethod { print "A\n"; }; + +1; +``` +
From 3f3f247b2608a04c26bccbf5174cd0f24e12ac5b Mon Sep 17 00:00:00 2001 From: Sahil Tripathi <55251741+sahil2128@users.noreply.github.com> Date: Mon, 22 Nov 2021 18:23:19 +0530 Subject: [PATCH 2/2] Update README.md (#189) --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6d33889..abae334 100644 --- a/README.md +++ b/README.md @@ -3257,6 +3257,7 @@ False
Explain what is GIL
+ 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