Table of Contents
What is Perl?
Perl is a high-level programming language that is known for its flexibility and versatility. Initially developed by Larry Wall in 1987, Perl was created to make report processing more efficient. Over the years, It has evolved into a powerful scripting language used for a wide range of tasks, including web development, system administration, and text processing. I have covered Perl basics for new programmers who want to learn from the scratch.
It is often referred to as the “Swiss Army knife” of programming languages because of its extensive capabilities. It allows developers to write concise, efficient code that can handle complex tasks with ease. For beginners, It offers a gentle learning curve with straightforward syntax and a wealth of community resources.
The community is one of its greatest strengths. With countless tutorials, forums, and modules available, newcomers can easily find support and guidance. Whether you’re looking to automate simple tasks or build complex applications, It provides the tools and flexibility you need to succeed.
Downloading & Installing
First, you need to download Perl from the official website of Perl. In this blog we will consider Windows and only concept of Perl Basics. For that, all versions for Windows are available on the website.
Click on “Download Latest Stable Source (5.40.0)” button under Source in Windows Section.
Once installed, you can verify the installation by opening your terminal or command prompt and typing ‘perl -v
‘. This command will display the installed Perl version, confirming that everything is set up correctly.
Writing Your First Program
After installation, it’s time to write your first program. Open your preferred text editor (I prefer VS Code) and create a new file with a .pl
extension. This file extension is used for Perl program. In the file, type the following simple program:
#!/usr/bin/perl
print "Welcome to Perl World!\n";
- Save the file and open your terminal or command prompt. In case, you are using VS Code editor you can use in-built terminal.
- Navigate to the directory where your program is saved and type ‘
perl your_filen_ame.pl
‘. If everything is set up correctly, you’ll see “Welcome to Perl World!” printed in the terminal. - Congratulations, you’ve just written and executed your first program!
Understanding Basic Syntax
In learning of Perl basics, the syntax is an essential for writing effective code. Its syntax is straightforward, making it accessible for beginners.
The Perl language uses symbols and operators that are familiar to those who have experience with other programming languages, but it also introduces unique elements that make it stand out.

Variables and Data Types
The variables are used to store data that your program can manipulate. There are three main types of variables:
- Scalars:
- Scalars store single values
- Numbers or strings
- Symbol Used: $
- Example:
my $name = "Alice";
- Arrays
- Store ordered lists of scalars
- Symbol Used: @
- Example:
my @colors = ("red", "blue", "green");
- Hashes
- Store key-value pairs
- Symbol Used: %
- Example:
my %fruit = ("apple" => "red", "banana" => "yellow");
“my” Keyword: The listed variables are declared local (lexically) to the enclosing block, file, or eval by using the operator my.
This is a dynamically typed language, meaning you don’t need to declare the data type of a variable explicitly. The type is inferred based on the value assigned to the variable. This flexibility allows you to focus more on coding and less on managing data types.
Operators and Expressions
In Perl basics, the operators are symbols that tell the interpreter to perform specific operations on variables and values. It supports a variety of operators, including arithmetic, comparison, and logical operators. Understanding how these operators work is key to writing functional scripts.
- Arithmetic operators: Used to preform mathematical operations
- Addition (+)
- Example:
my $sum = 20 + 10;
- Example:
- Subtraction (-)
- Example:
my $sum = 20 - 10;
- Example:
- Multiplication (*)
- Example:
my $sum = 20 * 10;
- Example:
- Addition (+)
- Arithmetic operators: Used for comparing values
- Equality (==)
- Example:
my $is_equal = ($sum == 10);
- Example:
- Inequality (!=)
- Example:
my $is_not_equal = ($sum != 10);
- Example:
- Equality (==)
- Logical operators: Used to control the flow of program based on conditions
- and (&&)
- Example:
my $result = $is_equal && 1;
- Example:
- or (||)
- Example:
my $result = $is_equal || 1;
- Example:
- and (&&)
Control Structures

Conditional Statements ( if
, elsif
)
In Perl basics, the conditional statements enable your script to make decisions based on certain conditions. The most common conditional statement is the if
statement, which executes a block of code if the specified condition is true. You can also use else
and elsif
for alternative conditions.
Example:
my $age = 18;
if ($age >= 18) {
print "You are an adult.\n";
} elsif ($age >= 14) {
print "You are a teenager.\n";
} else {
print "You are a child.\n";
}
Explanation: In this example, the program checks the value of $age
and prints a message based on the condition that is met. Conditional statements are crucial for making your program dynamic and responsive to different inputs.
Loops and Iteration ( for
, while
, and foreach
)
In Perl Basics, the loops are used to repeat a block of code multiple times. Perl offers several types of loops, including for
, while
, and foreach
. These loops are useful when you need to perform repetitive tasks, such as iterating over a list of items.
Example: for Loop
for (my $i = 0; $i < 5; $i++) {
print "This is iteration number $i\n";
}
Explaination
- This part initializes a loop control variable,
$i
, and sets its initial value to0
. - The variable
$i
will be used to keep track of the number of iterations the loop has gone through. - Before each iteration of the loop, this condition is checked.
- If the condition is true, the loop continues and the code inside the loop is executed.
- If the condition is false, the loop stops, and execution continues with the code following the loop.
- In this example, the loop will continue as long as
$i
is less than5
. - After each iteration of the loop, the control variable
$i
is incremented by1
. - The
++
operator adds1
to$i
each time the loop runs. - This ensures that the loop eventually meets the condition to stop (
$i
will reach5
).
Example: while Loop
my $counter = 1;
while ($counter <= 5) {
print "This is iteration number $counter\n";
$counter++;
}
Explanation:
- The variable
$counter
is initialized to 1. - The
while
loop checks the condition$counter <= 5
. If the condition is true, the code block inside the loop is executed. - Inside the loop, the message “This is iteration number $counter” is printed, where
$counter
is replaced by its current value. - The
$counter
variable is then incremented by 1 ($counter++
). - The loop continues until the condition
$counter <= 5
becomes false. In this case, the loop will run 5 times.
Example: foreach Loop
my @colors = ("red", "blue", "green", "yellow");
foreach my $color (@colors) {
print "The color is $color\n";
}
Explanation:
- The array
@colors
contains a list of color names. - The
foreach
loop iterates over each element in the@colors
array. - During each iteration, the current element is assigned to the variable
$color
. - Inside the loop, the message “The color is $color” is printed, where
$color
is replaced by the current color name. - The loop continues until all elements in the
@colors
array have been processed.
Conclusion
Congratulations on taking your first steps in learning Perl Basics! By now, you should have a good understanding of the basics of Perl programming, including how to set up your environment, write simple scripts, and use basic syntax and control structures.
I have covered only Perl basics for beginners here. There are some other advanced topics are also essential for advance learning. Debugging and Error Handling, File Management, CPAN, OOP, Network Programming, Memory Management etc. If you have any question or doubt please feel free to contact us.