Perl — 5
Link to Part 1: https://himashikarunathilake.medium.com/perl-1-5a5f4ec8c251
Link to Part 2: https://himashikarunathilake.medium.com/perl-2-12f31be96028
Link to Part 3: https://himashikarunathilake.medium.com/perl-3-daf8722151fa
Link to Part 4: https://himashikarunathilake.medium.com/perl-4-2215fa18efc3
Given below is the main.pl file that will be used to run all the sub files in this section:
#!/usr/bin/perl
# The fifth program in Perl.
use strict;
use warnings;
use feature "say";
say "";
say "*************** RUNNING THE MODULES.PL FILE ***************";
system("perl modules.pl");
say "__________________________________________________________________________________________";
say "";
say "*************** RUNNING THE ANONYMOUS_SUBROUTINES.PL FILE ***************";
system("perl anonymous_subroutines.pl");
say "__________________________________________________________________________________________";
say "";
say "*************** RUNNING THE FILE_HANDLING.PL FILE ***************";
system("perl file_handling.pl");
say "__________________________________________________________________________________________";
say "";
say "*************** RUNNING THE ERROR_HANDLING.PL FILE ***************";
system("perl error_handling.pl");
say "__________________________________________________________________________________________";
say "";
Modules in Perl
Both modules and packages in Perl are mechanisms used to organize code into separate files and namespaces, thereby helping to create modular, reusable, and maintainable code. A module is a collection of related functions, variables, and classes whereas a package is a namespace that encapsulates a set of variables and functions.
To create a module, a file with a “.pm” extension should be created with the same name as the module.
Given below is the Greet.pm module:
# Create the module Greet
package Greet;
# Define a subroutine - say_hello
sub say_hello {
my ($name) = @_;
print "Hello, $name!\n";
}
1;
Given below is the modules.pl script that will be using the module Greet created above:
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
# Add the current directory to @INC
use lib '.';
# Import the Greet module
use Greet;
say "=============== Welcome to the Greet Module! ===============";
print "Please provide your name: ";
my $name = <STDIN>;
chomp $name;
say "";
# Call a function from the module
Greet::say_hello($name);
Anonymous Subroutines
In Perl, an anonymous subroutine, also known as a “lambda” or “closure” is a subroutine without a name and are useful for creating ad-hoc code blocks. It can be stored in a scalar variable or passed as an argument to another subroutine.
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
# Create an anonymous subroutine and store it in a variable
my $add = sub {
my ($num1, $num2) = @_;
return $num1 + $num2;
};
say "=============== Performing Additions ===============";
print "Please provide the first number for the addition: ";
my $num1 = <STDIN>;
chomp $num1;
print "Please provide the second number for the addition: ";
my $num2 = <STDIN>;
chomp $num2;
# Call the anonymous subroutine
say "Calling the anonymous subroutine..........";
my $result = $add->($num1, $num2);
say "The addition of the two numbers is: $result";
File Handling in Perl
Reading from a File
Given below is the Perl code that can be used to read information from a file called input_file.txt:
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
say "=============== Reading from a File ===============";
print "Please provide the name of the input file: ";
my $inputFile = <STDIN>;
chomp $inputFile;
# Open the file in read mode
open(my $fh, '<', $inputFile) or die "ERROR! Could not open file: $!";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
# Close the file
close($fh);
Appending to a File
Given below is the Perl code that can be used to append information to the file called input_file.txt:
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
say "=============== Reading from a File ===============";
print "Please provide the name of the input file: ";
my $inputFile = <STDIN>;
chomp $inputFile;
# Open the file in read mode
open(my $fh, '<', $inputFile) or die "ERROR! Could not open file: $!";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
# Close the file
close($fh);
say "";
say "";
say "";
say "=============== Appending to a File ===============";
print "Please provide the name of the input file: ";
my $appendFile = <STDIN>;
chomp $appendFile;
# Open the file in append mode
open($fh, '>>', $appendFile) or die "ERROR! Could not open file: $!";
# Append to the file
print "Please provide the text to append to the file: ";
my $text = <STDIN>;
chomp $text;
say $fh $text;
# Close the file
close($fh);
say "";
say "SUCCESS! The text has been appended to the file. Please find the modified file contents below:";
say "";
say "";
# Open the file again in read mode
open($fh, '<', $appendFile) or die "ERROR! Could not open file: $!";
# Read and print the modified file contents
while (my $line = <$fh>) {
chomp $line;
say $line;
}
# Close the file
close($fh);
Writing to a File
Given below is the Perl code that can be used to write information to a file called output_file.txt:
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
say "=============== Reading from a File ===============";
print "Please provide the name of the input file: ";
my $inputFile = <STDIN>;
chomp $inputFile;
# Open the file in read mode
open(my $fh, '<', $inputFile) or die "ERROR! Could not open file: $!";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
# Close the file
close($fh);
say "";
say "";
say "";
say "=============== Appending to a File ===============";
print "Please provide the name of the input file: ";
my $appendFile = <STDIN>;
chomp $appendFile;
# Open the file in append mode
open($fh, '>>', $appendFile) or die "ERROR! Could not open file: $!";
# Append to the file
print "Please provide the text to append to the file: ";
my $text = <STDIN>;
chomp $text;
say $fh $text;
# Close the file
close($fh);
say "";
say "SUCCESS! The text has been appended to the file. Please find the modified file contents below:";
say "";
say "";
# Open the file again in read mode
open($fh, '<', $appendFile) or die "ERROR! Could not open file: $!";
# Read and print the modified file contents
while (my $line = <$fh>) {
chomp $line;
say $line;
}
# Close the file
close($fh);
say "";
say "";
say "";
say "=============== Writing to a File ===============";
print "Please provide the name of the output file: ";
my $outputFile = <STDIN>;
chomp $outputFile;
# Open the file in write mode
open($fh, '>', $outputFile) or die "ERROR! Could not open file: $!";
# Read the input from the user
print 'Please provide the text to write to the file (provide your input as a single line by using \n for line breaks): ';
$text = <STDIN>;
chomp $text;
# Replace "\n" with actual newline characters
$text =~ s/\\n/\n/g;
# Write to the file
say $fh $text;
# Close the file
close($fh);
say "";
say "SUCCESS! The text has been written to the file.";
Error Handling
die Function
In Perl, the “die” function is used to terminate the program and display an error message when a critical error occurs.
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
# die Function
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"die\" function, please provide the input file name as non_existent_file.txt.";
print "\nPlease provide the name of the input file: ";
my $dieInputFile= <STDIN>;
chomp $dieInputFile;
say "";
# Open the file in read mode
open(my $fh, '<', $dieInputFile) or die "ERROR! Could not open file: $!";
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
warn Function
The “warn” function is Perl, is used to display a warning message without terminating the program.
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
# die Function
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"die\" function, please provide the input file name as non_existent_file.txt.";
print "\nPlease provide the name of the input file: ";
my $dieInputFile = <STDIN>;
chomp $dieInputFile;
say "";
# Open the file in read mode
open(my $fh, '<', $dieInputFile) or die "ERROR! Could not open file: $!";
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
say "";
say "";
say "";
# warn Function
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"warn\" function, please provide the input file name as too_large_file.txt.";
print "\nPlease provide the name of the input file: ";
my $warnInputFile = <STDIN>;
chomp $warnInputFile;
say "";
# Open the file in read mode
open($fh, '<', $warnInputFile) or warn "WARNING! Could not open file: $!";
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
eval Block
The “eval” block in Perl is used for exception handling. It allows us to capture and handle exceptions, preventing the program from terminating abruptly.
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
# die Function
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"die\" function, please provide the input file name as non_existent_file.txt.";
print "\nPlease provide the name of the input file: ";
my $dieInputFile = <STDIN>;
chomp $dieInputFile;
say "";
# Open the file in read mode
open(my $fh, '<', $dieInputFile) or die "ERROR! Could not open file: $!";
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
say "";
say "";
say "";
# warn Function
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"warn\" function, please provide the input file name as too_large_file.txt.";
print "\nPlease provide the name of the input file: ";
my $warnInputFile = <STDIN>;
chomp $warnInputFile;
say "";
# Open the file in read mode
open($fh, '<', $warnInputFile) or warn "WARNING! Could not open file: $!";
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
say "";
say "";
say "";
# eval Block
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"eval\" block, please provide the input file name as corrupted_file.txt.";
print "\nPlease provide the name of the input file: ";
my $evalInputFile = <STDIN>;
chomp $evalInputFile;
say "";
# Open the file in read mode
eval {
open($fh, '<', $evalInputFile) or die "ERROR! Could not open file: $!";
};
if ($@) {
say "ERROR! Could not open file: $@";
}
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
Try::Tiny Module
The Try::Tiny module in Perl can be used for more advanced error handling. It provides a simple and flexible way to handle exceptions.
#!/usr/bin/perl
use strict;
use warnings;
use feature "say";
use Try::Tiny;
# die Function
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"die\" function, please provide the input file name as non_existent_file.txt.";
print "\nPlease provide the name of the input file: ";
my $dieInputFile = <STDIN>;
chomp $dieInputFile;
say "";
# Open the file in read mode
open(my $fh, '<', $dieInputFile) or die "ERROR! Could not open file: $!";
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
say "";
say "";
say "";
# warn Function
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"warn\" function, please provide the input file name as too_large_file.txt.";
print "\nPlease provide the name of the input file: ";
my $warnInputFile = <STDIN>;
chomp $warnInputFile;
say "";
# Open the file in read mode
open($fh, '<', $warnInputFile) or warn "WARNING! Could not open file: $!";
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
say "";
say "";
say "";
# eval Block
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"eval\" block, please provide the input file name as corrupted_file.txt.";
print "\nPlease provide the name of the input file: ";
my $evalInputFile = <STDIN>;
chomp $evalInputFile;
say "";
# Open the file in read mode
eval {
open($fh, '<', $evalInputFile) or die "ERROR! Could not open file: $!";
};
if ($@) {
say "ERROR! Could not open file: $@";
}
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
say "";
say "";
say "";
# Try::Tiny Module
say "To open an existing file, please provide the input file name as error_handling.txt.\nTo try out the \"Try::Tiny\" module, please provide the input file name as missing_file.txt.";
print "\nPlease provide the name of the input file: ";
my $tryTinyInputFile = <STDIN>;
chomp $tryTinyInputFile;
say "";
# Open the file in read mode
try {
open($fh, '<', $tryTinyInputFile) or die "ERROR! Could not open file: $!";
} catch {
warn "ERROR! Could not open file: $_\n";
};
say "Please find the file contents below: ";
say "";
# Read the file line by line
while (my $line = <$fh>) {
chomp $line;
say $line;
}
You can access the source code at: Perl/Perl-5 at master · Himashi-Karunathilake/Perl (github.com).
Link to Part 6: Perl — 6. In this article let’s look at using our… | by Himashi Karunathilake | Aug, 2023 | Medium