Overview of the fundamentals of Perl

Appblee
0


Hello, dear readers, We are thrilled to have you back in our Perl article series. Whether you are a seasoned coder or a curious-comer, we have an exciting topic to dive into today. So be comfy and let's embark on a fascinating journey through Perl fundamentals.


Guilty as charged. Perl is happily ugly, and happily derivative.

                                                                                                 -Larry Wall


Before we dive into deep let's take a moment to understand the fundamentals of Perl. Perl is a language loved by both beginners and experienced programmers. Let's explore some essential fundamental elements that make Perl such a fascinating language. 


Syntax Overview

  • Perl is a case-sensitive language.

    "#" is used for the comment

  • But the first line, #!/usr/local/bin/perl, tells where to find the Perl compiler on your system.
    
  • Perl statements end with the semi-colon ';'

example for comment in Perl


 Data Types

Perl has 3 basic data types.
       
1. Scalar

This is a single value. The single data unit can be an integer number, a floating point, a character, a string, a v-string, a paragraph or an entire web page.


examples for scalar types in Perl

If you are using use strict statements in a program, then you have to declare your variable before using it. It is mandatory. Otherwise, you'll get an error.


2. Arrays

This is an ordered list of scalars. It stores the values of the same data types in the form of a list. To declare an array we use the '@' sign in front of the variable. To access a particular value, we use the '$' symbol which is followed by the index in brackets.

        
Example for array in Perl


3. Hash

This is also known as an associative array because it is an unordered collection of scalars.
Hash is a set of key-value pairs. To declare hash in Perl we use the '%' sign. To access a particular value, we use the '$' symbol which is followed by the index in braces.




Name Convention

1. All scalar names should begin with $.

2. After the first character $ .alphanumeric characters,0 to 9 and underscore are allowed. But the first character cannot be a number.
 

Conditional Statements

if Statement:

The if statement allows running a block of code only if the given condition is true.

example for if statement in Perl





if-else Statement

The if-else statement extends the if statement to provide an alternative code block to execute if the condition is false.

Example for if_else statement in Perl


Loops

for  loop

The for loop is used to iterate over a list of values or the given range. It executes a block of code inside the loop in the given range or the specific number of elements in the list.

Example for for loop in Perl

The size of an array is determined with scalar context on the array



while  loop

while the loop runs until the condition is true. The block of code inside the while loop will execute until the given condition is true.

Example for while loop in Perl




Function

Perl Subroutine

This is similar to functions in other programming languages. Functions are defined using a sub. Functions in Perl accept arguments, process them, and return a value if needed.




Access Modifiers


There 3 types of access modifiers.

1. my

This can be used to declare any variable that is specific within the block.


2. local 

Using this we can change the same variable values to different values without changing the original value of the variable.

3. our

This is used to declare global variables and can be used inside any function or any block created within the program. This can be directly used and is accessed from every part of the program.

Packages

• A package is a collection of code that lives in its own namespace. 
• A namespace is a named collection of unique variable names.
• Namespaces prevent variable name collisions between packages.

for example, let's assume we have a package named "Appblee" and we define a variable $counter_value  and subroutine "appblee_calculator" within it. In this case, their fully qualified names would be $Appblee::counter_value and $Appblee::appblee_calculator


Example for package in Perl



Congratulations! You've just reacquainted yourself with some essential fundamentals of Perl. Through this article, we gave an overview idea about the fundamentals of Perl such as syntax, data types, conditional statements, loops, functions, access modifiers, and packages. We hope that you gain a clear idea about the fundamentals of Perl through this. Stay tuned for more exciting Perl content in our upcoming blog posts. Happy coding!🍏🐝



Tags:

Post a Comment

0Comments

Post a Comment (0)