Perl — 1

Himashi Karunathilake
4 min readAug 9, 2023
Image from https://www.vectorlogo.zone/logos/perl/perl-ar21.svg

Introduction

Perl is a versatile and dynamic scripting language that is known for its expressive syntax and powerful text manipulation capabilities. Perl offers developers a flexible tool for a wide range of applications.

For official website for Perl provides a wealth of resources for anyone willing to learn: The Perl Programming Language — www.perl.org.

If you want to install Perl in your own PC, you can download it from its website at: Perl Download — www.perl.org.

To code in Perl, you can download any supported IDE (Visual Studio Code in this case with the Perl extension enabled: Visual Studio Code — Code Editing. Redefined) and save the file with the extension “.pl” (e.g., main.pl).

The First Program in Perl

As the first step, as always, let’s try printing the “Hello World!” statement in Perl.

Comments in Perl

Perl does not have separate syntaxes for single line and multi-line comments. However, all comments in Perl start with a “#”.

# This is a comment in Perl.

Shebang / Hashbang (#!)

A shebang is the character sequence “#!” that appears at the very beginning of a script file. It is a special directive that is primarily used in Unix-like operating systems. When a script with a shebang is executed, the operating system uses the interpreter specified in the shebang to process and execute the script.

#!/usr/bin/perl

Pragmas in Perl

Pragmas are special directives or instructions that provide additional information to the compiler or interpreter about how the code should be treated or processed. They are not a part of the core language syntax, but Pragmas can influence how the compiler or interpreter behaves during compilation or execution.

use <pragma> <arguments>;

Given below are some example Pragmas.

PRAGMA                           USE

use strict; Enforce strict rules for variable declaration
and usage.
use warnings; Enables runtime warnings that alert you to
potential issues in your code.
use feature 'feature_name'; Enables specific language features introduced
in newer versions of Perl.
use constant NAME => value; Define constants that are fixed values that
cannot be changed.
use utf8; Allows you to use Unicode characters in your
program.
#!/usr/bin/perl

# The first program in Perl.

# Pragmas
use strict;
use warnings;

The Output Statement in Perl

When coding in Perl, it is important to note that each line of code will be followed by a semicolon “;” at its end.

#!/usr/bin/perl

# The first program in Perl.

# Pragmas
use strict;
use warnings;

print "Hello World!";

Run the File

  1. To run the file, open a command prompt and navigate to the file location.
  2. Type the following command into the command prompt:
perl main.pl (perl file_name)
Output of the print statement

Declaring Variables

A variable can be used to store a value. In Perl, variables can be declared using the keyword “my”. It is important to note that variable names should start with the dollar mark “$”.

#!/usr/bin/perl

# The first program in Perl.

# Pragmas
use strict;
use warnings;

# Print a single line
print "Hello World!";

# Print an empty line
print "\n\n";

# Declare variables to store a string and a number
my $name = "Dwight";
my $age = 30;

Printing Variables

Once declared, Perl variables can be printed as follows:

#!/usr/bin/perl

# The first program in Perl.

# Pragmas
use strict;
use warnings;

# Print a single line
print "Hello World!";

# Print an empty line
print "\n\n";

# Declare variables to store a string and a number
my $name = "Dwight";
my $age = 30;

# Print the variables
print "Name of the programmer: $name\n";
print "Age of the programmer: $age\n";

“say” Function

In Perl, the say function is used to print text followed by a newline character as opposed to the print function which would require you to append a newline character “\n” at the end of every statement.

#!/usr/bin/perl

# The first program in Perl.

# Pragmas
use strict;
use warnings;
use feature 'say';

# Print a single line
print "Hello World!";

# Print an empty line using the print function
print "\n\n";

# Declare variables to store a string and a number
my $name = "Dwight";
my $age = 30;
my $module = "The Office";

# Print the variables using the print function
print "Name of the programmer: $name\n";
print "Age of the programmer: $age\n";

# Print an empty line using the say function
say "";

# Print the variables using the say function
say "Name of the module: $module";
Output of the say function

--

--

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!