Perl — 4

Himashi Karunathilake
4 min readAug 16, 2023
Image from https://www.vectorlogo.zone/logos/perl/perl-ar21.svg
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

Given below is the main.pl file that will be used to run all the sub files in this section:

#!/usr/bin/perl

# The fourth program in Perl.

use strict;
use warnings;
use feature "say";

say "";

say "*************** RUNNING THE SUBROUTINES.PL FILE ***************";
system("perl subroutines.pl");
say "__________________________________________________________________________________________";
say "";

say "*************** RUNNING THE CLASSES.PL FILE ***************";
system("perl classes.pl");
say "__________________________________________________________________________________________";
say "";

Subroutines in Perl

In Perl, functions are referred to as “subroutines”. They can be used to reuse code segments at various times by simply calling the subroutine name when and where necessary. These subroutines prevent repetition of code blocks.

#!/usr/bin/perl

use strict;
use warnings;
use feature "say";

sub multiply {
my ($num1, $num2) = @_;
return $num1 * $num2;
}

say "Performing multiplications..........";
print "Please provide the first number for the multiplication: ";
my $num1 = <STDIN>;
chomp $num1;
print "Please provide the second number for the multiplication: ";
my $num2 = <STDIN>;
chomp $num2;

my $result = multiply($num1, $num2);

# Call the multiply function
say "The multiplication of the two numbers is: $result";
A subroutine that calculated the multiplication of two values

Classes in Perl

In Perl, classes are defined as “packages”. Packages are essentially namespaces that contains methods and variables associated with the class. In Perl, classes should be saved as Perl modules with a “.pm” file extension before accessing them through Perl script files (“.pl” file extension). The file name of the Perl module should be the name of our class.

Given below is the Student.pm module:

# Define a class
package Student;

# Create a constructor to create new objects
sub new_student {
my ($class, %args) = @_; # Capture the class name and arguments
my $self = { # Create a hash reference to store the object's attributes
name => $args{name},
age => $args{age},
country => $args{country},
};

bless $self, $class; # Bless the reference to the class name to create an object

return $self; # Return the object
}

sub get_name {
my ($self) = @_; # Capture the object
return $self->{name}; # Return the name attribute
}

sub get_age {
my ($self) = @_; # Capture the object
return $self->{age}; # Return the age attribute
}

sub get_country {
my ($self) = @_; # Capture the object
return $self->{country}; # Return the country attribute
}

return 1; # Return true to indicate that the module has been successfully loaded

Given below is the classes.pl script:

#!/usr/bin/perl

use strict;
use warnings;
use feature "say";

# Add the current directory to @INC
use lib '.';

# Import the Student module
use Student;

say "=============== Welcome to the Student Management System! ===============";

print "Would you like to add a new student record (y/n)? ";
my $createRecord = <STDIN>;
chomp $createRecord;

say "";

if ($createRecord eq "y") {
print "Please provide the student's name: ";
my $name = <STDIN>;
chomp $name;
print "Please provide the student's age: ";
my $age = <STDIN>;
chomp $age;
print "Please provide the student's country: ";
my $country = <STDIN>;
chomp $country;

# Create a new student record
my $student = Student->new_student(
name => $name,
age => $age,
country => $country,
);

# Retrieve student data
my $studentName = $student->get_name();
my $studentAge = $student->get_age();
my $studentCountry = $student->get_country();

say "";

say "SUCCESS! A new student record has been created for $studentName.\n[AGE: $studentAge, COUNTRY: $studentCountry]";
} elsif ($createRecord eq "n") {
say "EXITING... Thank you for using the Student Management System. Goodbye!";
} else {
say "ERROR! Invalid input. Please try again.";
}
Creating an object using a Perl class
Successfully exiting the program
Error message when providing an incorrect input

--

--

Himashi Karunathilake

I am a cybersecurity enthusiast and writer with a passion for demystifying complex topics. Join me as I explore the ever-evolving world of cybersecurity!