@@ -5279,7 +5279,82 @@ sub factorial {
5279
5279
### Perl Regex
5280
5280
5281
5281
<details >
5282
- <summary >How do you perform regular expresions in Perl?</summary ><br ><b >
5282
+ <summary >Check if the word `electroencefalografista` exists in a string</summary ><br ><b >
5283
+
5284
+ ```
5285
+ my $string = "The longest accepted word by RAE is: electroencefalografista";
5286
+ if ($string =~ /electroencefalografista/) {
5287
+ print "Match!";
5288
+ }
5289
+ ```
5290
+ </b ></details >
5291
+
5292
+ <details >
5293
+ <summary >Check if the word `electroencefalografista` does not exists in a string</summary ><br ><b >
5294
+
5295
+ ```
5296
+ my $string = "The longest not accepted word by RAE is: Ciclopentanoperhidrofenantreno";
5297
+ if ($string !~ /electroencefalografista/) {
5298
+ print "Does not match!";
5299
+ }
5300
+ ```
5301
+ </b ></details >
5302
+
5303
+
5304
+ <details >
5305
+ <summary >Replace the word `amazing`</summary ><br ><b >
5306
+
5307
+ ```
5308
+ my $string = "Perl is amazing!";
5309
+ $string =~ s/amazing/incredible/;
5310
+ print $string;
5311
+ # Perl is incredible!
5312
+ ```
5313
+ </b ></details >
5314
+
5315
+ <details >
5316
+ <summary >Extract `hh:mm:ss` with capturing group `()` in the following datetime</summary ><br ><b >
5317
+
5318
+ ```
5319
+ my $date = "Fri Nov 19 20:09:37 CET 2021";
5320
+ my @matches = $date =~ /(.*)(\d{2}:\d{2}:\d{2})(.*)/;
5321
+ print $matches[1];
5322
+ # Output: 20:09:37
5323
+ ```
5324
+ </b ></details >
5325
+
5326
+ <details >
5327
+ <summary >Extract all the elements that are numbers in an array</summary ><br ><b >
5328
+
5329
+ ```
5330
+ my @array = ('a', 1, 'b', 2, 'c', 3);
5331
+ my @numbers = grep (/\d/, @array); # Note: \d involves more digits than 0-9
5332
+ map {print $_ . "\n" } @numbers;
5333
+ ```
5334
+
5335
+ </b ></details >
5336
+
5337
+ <details >
5338
+ <summary >Print all the linux system users that starts with d or D</summary ><br ><b >
5339
+
5340
+ - With a Perl one liner : D
5341
+ ```
5342
+ open(my $fh, '<', '/etc/passwd');
5343
+ my @user_info = <$fh>;
5344
+ map { print $& . "\n" if $_ =~ /^d([^:]*)/ } @user_info;
5345
+ close $fh;
5346
+ ```
5347
+
5348
+ - Avoiding one-liners
5349
+
5350
+ ```
5351
+ foreach my $user_line (@user_info) {
5352
+ if ($user_line =~ /^d([^:]*)/) {
5353
+ print $& . "\n";
5354
+ }
5355
+ }
5356
+ ```
5357
+
5283
5358
</b ></details >
5284
5359
5285
5360
### Perl Files Handle
@@ -5303,7 +5378,7 @@ sub factorial {
5303
5378
# We can use:
5304
5379
# '>' Write (it clears a previous content if exists).
5305
5380
# '>>' Append.
5306
- open(my $fh, '>>', 'file_name.ext') or "Error: file can't be opened";
5381
+ open(my $fh, '>>', 'file_name.ext') or die "Error: file can't be opened";
5307
5382
print $fh "writing text...\n";
5308
5383
close($fh);
5309
5384
```
@@ -5313,7 +5388,7 @@ close($fh);
5313
5388
<summary >How can you read a file and print every line?</summary ><br ><b >
5314
5389
5315
5390
```
5316
- open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened";
5391
+ open(my $fh, '<', 'file_to_read.ext') or die "Error: file can't be opened";
5317
5392
my @file = <$fh>;
5318
5393
foreach my $line (@file) {
5319
5394
print $line;
@@ -5323,7 +5398,7 @@ foreach my $line (@file) {
5323
5398
We can use the file handle without assigning it to an array:
5324
5399
5325
5400
```
5326
- open(my $fh, '<', 'file_to_read.ext') or "Error: file can't be opened";
5401
+ open(my $fh, '<', 'file_to_read.ext') or die "Error: file can't be opened";
5327
5402
5328
5403
foreach my $line (<$fh>) {
5329
5404
print $line;
0 commit comments