---
tags:
- devops
- l1
- flashcard-deck
- python-automation
---
<!-- wiki:breadcrumb:start -->
[Portal](../../../../library/portal/index.md) | **Level:** [L1: Foundations](../../../../library/portal/levels.md) | **Topics:** [Python Automation](../../../../library/portal/topics.md) | **Domain:** DevOps & Tooling
<!-- wiki:breadcrumb:end -->

id	category	difficulty	tags	question	answer	source_path
perl/02f49f6f474a	perl	easy	perl, modules, regex	What is a Perl package? And a module?	With a Perl package we are defining a namespace.\nA 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.	projects/knowledge/interview/perl/027-what-is-a-perl-package-and-a-module.txt
perl/113cb5c32ee3	perl	easy	perl, references, regex	What is the purpose of the bless function?	The function os the `bless` function is used to turning a plain data structure into an object.\n\nExample: perl -ne 'print if /ERROR/' access.log — prints lines containing ERROR. The -n flag wraps the code in a while(<>){...} loop.\n\nRemember: "perl -ne = line-by-line filter, perl -pe = line-by-line transform."	projects/knowledge/interview/perl/019-what-is-the-purpose-of-the-bless-function.txt
perl/1f78392b9860	perl	medium	perl, references, modules, text-processing	How can you call a method of an inherited class?	"```\n# Class `A` with `printA` method.\npackage A;\n\nsub new { return bless {}, shift; };\nsub printA { print ""A""; };\n\n# Class `B` that extends or use the parent class `A`.\npackage B;\n\nuse parent -norequire, 'A';\n\nsub new { return bless {}, shift; };\n\n# Instance class `B` allows call the inherited method\nmy $b = B->new();\n$b->printA();\n```"	projects/knowledge/interview/perl/023-how-can-you-call-a-method-of-an-inherited-class.txt
perl/25502d5487a3	perl	medium	perl, one-liner, cpan, modules	How can you install cpanm and a Perl module?	There are some different alternatives to install Perl modules. We will use `cpanm`.\n\n- Install `cpanm`:\n\n```\n$ cpan App::cpanminus\n```\n\n- Install the `Test` module with `cpanm`:\n\n```\ncpanm Test\n```\n\nNow we can test the `Test` installed module:\n\n```\n$ perl -M'Test::Simple tests => 1' -e 'ok( 1 + 1 == 2 );'\n1..1\nok 1\n```\n\n```\n$ perl -M'Test::Simple tests => 1' -e 'ok( 1 + 1 == 3 );'\n1..1\nnot ok 1\n#   Failed test at -e line 1.\n# Looks like you failed 1 test of 1.\n```	projects/knowledge/interview/perl/031-how-can-you-install-cpanm-and-a-perl-module.txt
perl/28838430e1e3	perl	medium	perl, regex, text-processing	How can we evaluate and capture an exception in Perl?	"From the official [eval docs](https://perldoc.perl.org/functions/eval):\n\n""`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."".\n\ne.g:\n\n```\neval {\n    die;\n};\nif ($@) {\n    print ""Error. Details: $@"";\n}\n```\n\nIf we execute this we get the next output:\n\n```\nError. Details: Died at eval.pl line 2.\n```\n\nThe `eval` (`try` in another programming languages) is trying to execute a code."	projects/knowledge/interview/perl/024-how-can-we-evaluate-and-capture-an-exception-in-pe.txt
perl/2a6486512573	perl	easy	perl, text-processing, regex	What is Perl and what are its common use cases in system administration?	"From the official [docs](https://perldoc.perl.org/):\n\n""Perl officially stands for Practical Extraction and Report Language, except when it doesn't.""\n\nIt'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."	projects/knowledge/interview/perl/001-what-is-perl.txt
perl/305fa8158616	perl	easy	perl, fileio, regex	What is Perl Open3?	"From the official [IPC::Open3 docs](https://perldoc.perl.org/IPC::Open3):\n\n""IPC::Open3 - open a process for reading, writing, and error handling using open3()"".\n\nWith `open3` we can have the full control of the STDIN, STDOUT, STDERR. It's usually used to execute commands."	projects/knowledge/interview/perl/025-what-is-perl-open3.txt
perl/35a35b535419	perl	easy	perl, modules, regex	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.	projects/knowledge/interview/perl/028-what-is-the-difference-between-pl-and-pm-extension.txt
perl/431a7f1e8a74	perl	hard	perl, references, modules, fileio	Does Perl have polymorphism? What is method overriding?	"Yes, it has polymorphism. In fact method overriding is a way to apply it in Perl.\n\nMethod overriding in simple words appears when we have a class with a method that already exist in a parent class.\n\nExample:\n\n```\npackage A;\n\nsub new { return bless {}, shift; };\nsub printMethod { print ""A\n""; };\n\npackage B;\n\nuse parent -norequire, 'A';\n\nsub new { return bless {}, shift; };\nsub printMethod { print ""B\n""; };\n\nmy $a = A->new();\nmy $b = B->new();\n\nA->new()->printMethod();\nB->new()->printMethod();\n\n# Output:\n# A\n# B\n```"	projects/knowledge/interview/perl/022-does-perl-have-polymorphism-what-is-method-overrid.txt
perl/45c58ce36ce5	perl	medium	perl, text-processing, regex	Extract all the elements that are numbers in an array	"```\nmy @array = ('a', 1, 'b', 2, 'c', 3);\nmy @numbers = grep (/\d/, @array);    # Note: \d involves more digits than 0-9\nmap {print $_ . ""\n"" } @numbers;\n```"\n\nRemember: In regex, $1 is the first capture group. Example: if ($line =~ /user=(\w+)/) { print $1; }	projects/knowledge/interview/perl/013-extract-all-the-elements-that-are-numbers-in-an-ar.txt
perl/45fc9f3c734c	perl	medium	perl, regex, text-processing	Check if the word `electroencefalografista` exists in a string	"```\nmy $string = ""The longest accepted word by RAE is: electroencefalografista"";\nif ($string =~ /electroencefalografista/) {                                                         \n    print ""Match!"";\n}\n```"\n\nExample: use strict; use warnings; catches undeclared variables and common mistakes. Always start scripts with these two lines.\n\nGotcha: Without strict, Perl silently creates global variables on first use — leading to typo-induced bugs.	projects/knowledge/interview/perl/009-check-if-the-word-electroencefalografista-exists-i.txt
perl/47028ef94658	perl	medium	perl, regex, text-processing	Check if the word `electroencefalografista` does not exists in a string	"```\nmy $string = ""The longest not accepted word by RAE is: Ciclopentanoperhidrofenantreno"";\nif ($string !~ /electroencefalografista/) {\n    print ""Does not match!"";\n}\n```"\n\nExample: my $count = 0; declares a lexically scoped variable. my @items = (1,2,3); for arrays, my %map = (a => 1); for hashes.\n\nRemember: "my = mine (local scope), our = shared (package scope)."	projects/knowledge/interview/perl/010-check-if-the-word-electroencefalografista-does-not.txt
perl/4749a3cf066c	perl	medium	perl, text-processing, regex	How can you access to a hash value, add and delete a key/value pair and modify a hash?	```\nmy %numbers = (\n  'First' => '1',\n  'Second' => '2',\n  'Third' => '3'\n);\n```\n\n- Access:\n\n```\nprint($numbers{'First'});\n```\n\n- Add:\n\n```\n$numbers{'Fourth'} = 4;\n```\n\n- Delete:\n\n```\ndelete $numbers{'Third'};\n```\n\n- Modify:\n\n```\n$numbers{'Fifth'} = 6;\n$numbers{'Fifth'} = 5;\n```\n\nExample: @colors = ("red", "green", "blue"); print $colors[0]; # "red". Use push/pop for stack ops, shift/unshift for queue ops.\n\nRemember: "@ = array (think 'at' multiple items), $ = scalar (single)."	projects/knowledge/interview/perl/003-how-can-you-access-to-a-hash-value-add-and-delete-.txt
perl/477c868e8927	perl	easy	perl, text-processing, regex	What is a Perl subroutine? How to define it?	"It's the perl model for user defined functions (this is also called function like other programming languages). We can define a subroutine with the keyword `sub`. \n\n```\nsub hello {\n  print ""hello"";\n}\n```"	projects/knowledge/interview/perl/005-what-is-a-perl-subroutine-how-to-define-it.txt
perl/4ea4451df614	perl	medium	perl, references, modules, fileio	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.\n\n```\npackage A;\n\nsub new { return bless {}, shift; };\nsub printMethod { print ""A\n""; };\n\n1;\n```"	projects/knowledge/interview/perl/029-why-a-perl-class-or-module-should-return-something.txt
perl/58825e6ba56d	perl	medium	perl, fileio, text-processing	How to write into a file?	"```\n# We can use:\n# '>' Write (it clears a previous content if exists).\n# '>>' Append.\nopen(my $fh, '>>', 'file_name.ext') or die ""Error: file can't be opened"";\nprint $fh ""writing text...\n"";\nclose($fh);\n```"\n\nRemember: "chomp eats the newline, chop eats any last char." Always chomp after reading input.	projects/knowledge/interview/perl/016-how-to-write-into-a-file.txt
perl/691751593d72	perl	easy	perl, modules, regex	What is Perl POD? Can you code an example?	"From the official [docs](https://perldoc.perl.org/perlpod):\n\n""Pod is a simple-to-use markup language used for writing documentation for Perl, Perl programs, and Perl modules.""\n\n```\n=item\n    This function returns the factorial of a number.\n    Input: $n (number you wanna calculate).\n    Output: number factorial.\n=cut\nsub factorial {\n    my ($i, $result, $n) = (1, 1, shift);\n    $result = $result *= $i && $i++ while $i <= $n;\n    return $result;\n}\n```"	projects/knowledge/interview/perl/008-what-is-perl-pod-can-you-code-an-example.txt
perl/6bcac3bb00d1	perl	medium	perl, references, modules, text-processing	How to create a Perl class? How can you call a method?	"- Let's create the package: `Example.pm`\n\n```\npackage Example;\n\nsub new {\n    my $class = shift;\n    my $self = {};\n    bless $self, $class;\n    return $self;\n}\n\nsub is_working {\n    print ""Working!"";\n}\n\n1;\n```\n\n- Now we can instance the `Example` class and call `is_working` method:\n\n```\nmy $e = new Example();\n$e->is_working();\n# Output: Working!\n```"	projects/knowledge/interview/perl/020-how-to-create-a-perl-class-how-can-you-call-a-meth.txt
perl/71cffdfa3eb5	perl	medium	perl, modules, fileio, text-processing	How can you read a file and print every line?	"```\nopen(my $fh, '<', 'file_to_read.ext') or die ""Error: file can't be opened"";\nmy @file = <$fh>;\nforeach my $line (@file) {\n    print $line;\n}\n```\n\nWe can use the file handle without assigning it to an array:\n\n```\nopen(my $fh, '<', 'file_to_read.ext') or die ""Error: file can't be opened"";\n\nforeach my $line (<$fh>) {\n    print $line;\n}\n```"	projects/knowledge/interview/perl/017-how-can-you-read-a-file-and-print-every-line.txt
perl/7a54001f3c96	perl	hard	perl, modules, fileio, text-processing	Using Open3: Create a file with the size of 15MB and check it's created successfully	"- Code:\n\n```\nuse IPC::Open3;\nuse Data::Dumper;\n\nsub execute_command {\n    my @command_to_execute = @_;\n    my ($stdin, $stdout, $stderr);\n    eval {\n        open3($stdin, $stdout, $stderr, @command_to_execute);\n    };\n    if ($@) {\n        print ""Error. Details: $@"";\n    }\n    close($stdin);\n    return $stdout;\n}\n\nmy $file_name = 'perl_open3_test';\n&execute_command('truncate', '-s', '15M', $file_name);\nmy $result = &execute_command('stat', '-c', '%s', $file_name);\nprint Dumper(<$result>);\n```\n\n- Result:\n\n```\n$ -> perl command.pl \n$VAR1 = '15728640\n';\n```"	projects/knowledge/interview/perl/026-using-open3-create-a-file-with-the-size-of-15mb-an.txt
perl/82edaa9fa7cf	perl	medium	perl, regex, text-processing	Extract `hh:mm:ss` with capturing group `()` in the following datetime	"```\nmy $date = ""Fri Nov 19 20:09:37 CET 2021"";\nmy @matches = $date =~ /(.*)(\d{2}:\d{2}:\d{2})(.*)/;\nprint $matches[1];\n# Output: 20:09:37\n```"\n\nRemember: "$_ is Perl's 'it' pronoun." Many functions default to $_ when no argument is given. Example: for (@items) { print; } prints each item because print defaults to $_.	projects/knowledge/interview/perl/012-extract-hhmmss-with-capturing-group-in-the-followi.txt
perl/8bc40e832f68	perl	hard	perl, modules, text-processing	How can you iterate an array? And a hash?	"- Array:\n\n```\nmy @numbers = qw/1 2 3 4 5/;\n\n# Using `$_` that represents the current iteration in a loop. It starts from index array 0 until the last index.\nforeach (@numbers) {\n    print($_);\n}\n# Output: 12345\n\n\n# ""$#"" returns the max index of an array. That's the reason because we can iterate accessing to the array from the index 0 to the max index.\nfor my $i (0..$#numbers) {\n    print($numbers[$i]);\n}\n# Output: 12345\n\n\n# Using the `map` keyword:\nprint map {$_} @numbers;\n# Output: 12345\n\n# Using `while`. We should take care with this option."	projects/knowledge/interview/perl/004-how-can-you-iterate-an-array-and-a-hash.txt
perl/ad1a9d9710d0	perl	medium	perl, regex, text-processing	Replace the word `amazing`	"```\nmy $string = ""Perl is amazing!"";\n$string =~ s/amazing/incredible/;\nprint $string;\n# Perl is incredible!\n```"\n\nRemember: "Perl hash = key-value store." Declared with %. Access with $hash{key}. Use keys/values/each to iterate.	projects/knowledge/interview/perl/011-replace-the-word-amazing.txt
perl/cdca5c5d4c43	perl	medium	perl, modules, fileio	Does Perl have inheritance? What is the `SUPER` keyword?	Yes, Perl supports inheritance. We can read about it in the official [docs](https://perldoc.perl.org/perlobj#Inheritance). \nWe 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.\n\nGotcha: Perl has no boolean type — 0, "", "0", and undef are all false. Everything else is true.	projects/knowledge/interview/perl/021-does-perl-have-inheritance-what-is-the-super-keywo.txt
perl/d1bd5226d672	perl	medium	perl, regex, one-liner	Does Perl have conventions?	You can check [perlstyle](https://perldoc.perl.org/perlstyle)\n\nExample: perl -i.bak -pe 's/foo/bar/g' *.txt — replaces foo with bar in all .txt files, creating .bak backups.\n\nGotcha: -i without extension on some systems destroys the original with no backup.	projects/knowledge/interview/perl/007-does-perl-have-conventions.txt
perl/d6a194442c79	perl	easy	perl, cpan, modules	What is cpan? And cpanm?	"CPAN is the Comprehensive Perl Archive Network. \n\nCPANM From the official [App::cpanminus](https://metacpan.org/pod/App::cpanminus):\n""App::cpanminus - get, unpack, build and install modules from CPAN"".\n\n[Find CPAN modules](https://metacpan.org/)"\n\nExample: my %ages = ("alice" => 30, "bob" => 25); print $ages{"alice"}; # 30. Use keys %hash to iterate.\n\nRemember: "% = hash (think percent = pairs of things)."	projects/knowledge/interview/perl/030-what-is-cpan-and-cpanm.txt
perl/e427948a2aab	perl	hard	perl, regex, one-liner, fileio	Print all the linux system users that starts with d or D	"- With a Perl one liner :D\n```\nopen(my $fh, '<', '/etc/passwd');\nmy @user_info = <$fh>;\nmap { print $& . ""\n"" if $_ =~ /^d([^:]*)/  } @user_info;\nclose $fh;\n```\n\n- Avoiding one-liners\n\n```\nforeach my $user_line (@user_info) {\n    if ($user_line =~ /^d([^:]*)/) {\n        print $& . ""\n"";\n    }\n}\n```"	projects/knowledge/interview/perl/014-print-all-the-linux-system-users-that-starts-with-.txt
perl/e7107d933647	perl	hard	perl, fileio, regex	Mention the different modes in File Handling	- Read only: `<`\n- Write mode. It creates the file if doesn't exist: `>`\n- Append mode. It creates the file if doesn't exist: `>>`\n- Read and write mode: `+<`\n- Read, clear and write mode. It creates the file if doesn't exist: `+>`\n- Read and append. It creates the file if doesn't exist: `+>>`	projects/knowledge/interview/perl/015-mention-the-different-modes-in-file-handling.txt
perl/f4faf379c7f5	perl	hard	perl, references, text-processing	What data types Perl has? And how can we define it?	- Scalar: This is a simple variable that stores single data items. It can be a string, number or reference.\n\n```\nmy $number = 5;\n```\n\n- Arrays: This is a list of scalars. \n\n```\nmy @numbers = (1, 2, 3, 4, 5);\n# or using the `qw` keyword (quote word):\nmy @numbers = qw/1 2 3 4 5/; \n# '/' can be another symbol, e.g qw@1 2 3 4 5@\n```\n\n- Hashes (or associative arrays): This is an unordered collection of key-value pairs. We can access to a hash using the keys.\n\n```\nmy %numbers = (\n  First => '1',\n  Second => '2',\n  Third => '3'\n);\n```	projects/knowledge/interview/perl/002-what-data-types-perl-has-and-how-can-we-define-it.txt
perl/f5397db5c324	perl	hard	perl, modules, regex	Does Perl have support for OOP?	"From the official [docs](https://perldoc.perl.org/perlootut):\n\n""By default, Perl's built-in OO system is very minimal, leaving you to do most of the work."""\n\nFun fact: Perl's motto is "There's More Than One Way To Do It" (TMTOWTDI, pronounced "tim-toady").	projects/knowledge/interview/perl/018-does-perl-have-support-for-oop.txt
perl/fcedabe6d5ac	perl	hard	perl, regex, one-liner	Describe the different ways to receive parameters in a subroutine	- List assignment: Using the `@_` array. It's a list with the elements that are being passed as parameters.\n\n```\nsub power {\n    my ($b, $e) = @_;\n    return $b ** $e; \n}\n\n&power(2, 3);\n```\n\n- Individual assignment: We should access to every element of the `@_` array. It starts from zero.\n\n```\nsub power {\n    my $b = $_[0];\n    my $e = $_[1];\n    return $b ** $e; \n}\n\n&power(2, 3);\n```\n\n- Using `shift` keyword: It's used to remove the first value of an array and it's returned.\n\n```\nsub power {\n    my $b = shift;\n    my $3 = shift;\n    return $b ** $e; \n}\n\	projects/knowledge/interview/perl/006-describe-the-different-ways-to-receive-parameters-.txt

<!-- wiki:related:start -->
---

## Wiki Navigation

### Related Content

- Python Algorithms Flashcards *(CLI)* (flashcard_deck, L2) — Python Automation
- [Python Async & Concurrency](../../../../library/topics/python-async-concurrency/index.md) (Topic Pack, L2) — Python Automation
- Python Concepts Flashcards *(CLI)* (flashcard_deck, L0) — Python Automation
- [Python Debugging](../../../../library/topics/python-debugging/index.md) (Topic Pack, L1) — Python Automation
- [Python Drills](../../../../library/drills/python_drills.md) (Drill, L0) — Python Automation
- Python Exercises (Quest Ladder) *(CLI)* (Exercise Set, L0) — Python Automation
- Python Flashcards *(CLI)* (flashcard_deck, L1) — Python Automation
- Python OOP Flashcards *(CLI)* (flashcard_deck, L2) — Python Automation
- [Python Packaging](../../../../library/topics/python-packaging/index.md) (Topic Pack, L2) — Python Automation
- [Python for Infrastructure](../../../../library/topics/python-infra/index.md) (Topic Pack, L1) — Python Automation

<!-- wiki:related:end -->
