@@ -5081,22 +5081,38 @@ my %numbers = (
50815081<details >
50825082<summary >How can you access to a hash value, add and delete a key/value pair and modify a hash?</summary ><br ><b >
50835083
5084+ ```
50845085my %numbers = (
50855086 'First' => '1',
50865087 'Second' => '2',
50875088 'Third' => '3'
50885089);
5090+ ```
50895091
50905092- Access:
5093+
5094+ ```
50915095print($numbers{'First'});
5096+ ```
5097+
50925098- Add:
5099+
5100+ ```
50935101$numbers{'Fourth'} = 4;
5102+ ```
5103+
50945104- Delete:
5105+
5106+ ```
50955107delete $numbers{'Third'};
5108+ ```
5109+
50965110- Modify:
5097- $numbers{'Fifth'} = 6;
50985111
5112+ ```
5113+ $numbers{'Fifth'} = 6;
50995114$numbers{'Fifth'} = 5;
5115+ ```
51005116
51015117</b ></details >
51025118
@@ -5268,8 +5284,52 @@ sub factorial {
52685284
52695285### Perl Files Handle
52705286
5287+ <details >
5288+ <summary >Mention the different modes in File Handling</summary ><br ><b >
5289+
5290+ - Read only: ` < `
5291+ - Write mode. It creates the file if doesn't exist: ` > `
5292+ - Append mode. It creates the file if doesn't exist: ` >> `
5293+ - Read and write mode: ` +< `
5294+ - Read, clear and write mode. It creates the file if doesn't exist: ` +> `
5295+ - Read and append. It creates the file if doesn't exist: ` +>> `
5296+
5297+ </b ></details >
5298+
52715299<details >
52725300<summary >How to write into a file?</summary ><br ><b >
5301+
5302+ ```
5303+ # We can use:
5304+ # '>' Write (it clears a previous content if exists).
5305+ # '>>' Append.
5306+ open(my $fh, '>>', 'file_name.ext') or "Error: file can't be opened";
5307+ print $fh "writing text...\n";
5308+ close($fh);
5309+ ```
5310+ </b ></details >
5311+
5312+ <details >
5313+ <summary >How can you read a file and print every line?</summary ><br ><b >
5314+
5315+ ```
5316+ open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened";
5317+ my @file = <$fh>;
5318+ foreach my $line (@file) {
5319+ print $line;
5320+ }
5321+ ```
5322+
5323+ We can use the file handle without assigning it to an array:
5324+
5325+ ```
5326+ open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened";
5327+
5328+ foreach my $line (<$fh>) {
5329+ print $line;
5330+ }
5331+ ```
5332+
52735333</b ></details >
52745334
52755335### Perl OOP
@@ -5291,11 +5351,100 @@ The function os the `bless` function is used to turning a plain data structure i
52915351</b ></details >
52925352
52935353<details >
5294- <summary >Does Perl have inheritance?</summary ><br ><b >
5354+ <summary >How to create a Perl class? How can you call a method?</summary ><br ><b >
5355+
5356+ - Let's create the package: ` Example.pm `
5357+
5358+ ```
5359+ package Example;
5360+
5361+ sub new {
5362+ my $class = shift;
5363+ my $self = {};
5364+ bless $self, $class;
5365+ return $self;
5366+ }
5367+
5368+ sub is_working {
5369+ print "Working!";
5370+ }
5371+
5372+ 1;
5373+ ```
5374+
5375+ - Now we can instance the ` Example ` class and call ` is_working ` method:
5376+
5377+ ```
5378+ my $e = new Example();
5379+ $e->is_working();
5380+ # Output: Working!
5381+ ```
5382+
5383+ </b ></details >
5384+
5385+ <details >
5386+ <summary >Does Perl have inheritance? What is the `SUPER` keyword?</summary ><br ><b >
5387+
5388+ Yes, Perl supports inheritance. We can read about it in the official [ docs] ( https://perldoc.perl.org/perlobj#Inheritance ) .
5389+ 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.
52955390</b ></details >
52965391
52975392<details >
52985393<summary >Does Perl have polymorphism? What is method overriding?</summary ><br ><b >
5394+
5395+ Yes, it has polymorphism. In fact method overriding is a way to apply it in Perl.
5396+
5397+ Method overriding in simple words appears when we have a class with a method that already exist in a parent class.
5398+
5399+ Example:
5400+
5401+ ```
5402+ package A;
5403+
5404+ sub new { return bless {}, shift; };
5405+ sub printMethod { print "A\n"; };
5406+
5407+ package B;
5408+
5409+ use parent -norequire, 'A';
5410+
5411+ sub new { return bless {}, shift; };
5412+ sub printMethod { print "B\n"; };
5413+
5414+ my $a = A->new();
5415+ my $b = B->new();
5416+
5417+ A->new()->printMethod();
5418+ B->new()->printMethod();
5419+
5420+ # Output:
5421+ # A
5422+ # B
5423+ ```
5424+
5425+ </b ></details >
5426+
5427+ <details >
5428+ <summary >How can you call a method of an inherited class?</summary ><br ><b >
5429+
5430+ ```
5431+ # Class `A` with `printA` method.
5432+ package A;
5433+
5434+ sub new { return bless {}, shift; };
5435+ sub printA { print "A"; };
5436+
5437+ # Class `B` that extends or use the parent class `A`.
5438+ package B;
5439+
5440+ use parent -norequire, 'A';
5441+
5442+ sub new { return bless {}, shift; };
5443+
5444+ # Instance class `B` allows call the inherated method
5445+ my $b = B->new();
5446+ $b->printA();
5447+ ```
52995448</b ></details >
53005449
53015450### Perl OS
0 commit comments