Csc410 Perl Programming

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

INTRODUCTION TO PERL

PROGRAMMING
CSC 410:E-TECH & GATEWAY PROGRAMMING
WHAT IS PERL
• Perl is a high-level, general-purpose programming language that
was originally developed for text manipulation and extraction of
information from text files. The name "Perl" originally stood for
"Practical Extraction and Reporting Language." Over time, Perl has
evolved into a versatile scripting language used for various
purposes, including system administration and web development
• A scripting language is a type of programming language that is
interpreted rather than compiled
• scripting languages are executed directly by an interpreter. This
means that the source code is read and executed line by line at
runtime.
FEATURES OF PERL
• Perl is a stable, cross platform programming language
• Perl is an Open Source software, licensed under its Artistic
License, or the GNU General Public License (GPL).
• Perl is an interpreted language, meaning that it is not compiled
into machine code but is executed directly by an interpreter.
• Perl is widely used for system administration tasks and scripting on
Unix and Linux systems.
• Perl has an active community, and the Comprehensive Perl Archive
Network (CPAN) is a vast repository of Perl modules and libraries
that can be easily integrated into Perl programs.
Visit the URL https://brew.sh/ for installation guide on
how to install the Perl Interpreter that is designed to ru
on macOS or Linus
Setting Up Perl Development Environment

• To develop Perl applications, you need a Perl distribution and a


code editor installed in your system
• If you’re using Windows, you can use Strawberry Perl.
• The Strawberry Perl is a Perl environment for Microsoft Windows.
It contains all you need to develop and execute Perl applications.
• To download Strawberry Perl, you navigate to the
https://strawberryperl.com and download the appropriate
version.
• Once the download completes, you launch the setup wizard by
double-clicking the installer file.
Setting Up Perl Development Environment
Setting Up Perl Development Environment
Second, click the Next button to continue to set up. In
this step, you need to accept the terms and license
agreement. After accepting the terms, click the Next
button.
Setting Up Perl Development Environment
Setting Up Perl Development Environment
Setting Up Perl Development Environment
Setting Up Perl Development Environment
Setting Up Perl Development Environment
Setting Up Perl Development Environment
PERL SYNTAX OVERVIEW
• A Perl program consists of a sequence of declarations and
statements, which run from the top to the bottom. Loops,
subroutines, and other control structures allow you to jump around
within the code. Every simple statement must end with a semicolon
(;).
• A Perl script can be created inside of any normal simple-text editor
program. As a Perl convention, a Perl file must be saved with a .pl
or .PL file extension in order to be recognized as a functioning Perl
script.
PERL SYNTAX OVERVIEW
• The first line of the every Perl program must begin with:

This line is called a shebang line, and it indicates the path to the Perl
interpreter. It tells the system that this script should be executed using
Perl.
The Second & Third lines of the every Perl program must begin with:
These statements are called
pragmas or directives to the
Perl Interpreter
PERL SYNTAX OVERVIEW

The use strict statement is called pragma and it forces you to code properly
to make your program less error-prone. For example: It forces you to
declare variables before you use them. You can declare variable using “my”
keyword. “my” keyword restricts the scope of the variable to local
Example
PERL SYNTAX OVERVIEW
• use warnings pragma helps you find typing mistakes, it warns you
whenever it sees something wrong with your program. It would
help you find mistakes in your program faster

Note: The most important point to note here is that “use strict” would
abort the execution of program if it finds errors. On the other hand “use
warnings” would only provide you the warnings, it wont abort the
execution. You should always use these two pragmas in your programs as
it is a good programming practice.
PERL SYNTAX OVERVIEW

• A Perl program does not care about whitespaces. if spaces are


inside the quoted strings, then they would be printed as is. For
example −
PERL SYNTAX OVERVIEW
• You can use double quotes or single quotes around literal strings
• There is an important difference in single and double quotes. Only
double quotes interpolate variables and special characters such as
newlines \n, whereas single quote does not interpolate any variable
or special character
• To Interpolate variables in Perl allow you to include the values of
variables directly within the string. In the example where we are
using $a as a variable to store a value and later printing that value

PERL DATA TYPES
• Perl has three basic data types: scalars, arrays of scalars, and
hashes of scalars, also known as associative arrays
• Scalars are simple variables. They are preceded by a dollar sign
($). A scalar is either a number, a string, or a reference. A
reference is actually an address of a variable
• Arrays are ordered lists of scalars that you access with a numeric
index, which starts with 0. They are preceded by an "at" sign (@)
• Hashes are unordered sets of key/value pairs that you access using
the keys as subscripts. They are preceded by a percent sign (%)
NUMERIC LITERALS
• In Perl, numeric literals are used to represent constant numeric
values. Perl supports various formats for numeric literals,
including integers, floating-point numbers, and scientific notation
• Integers:

• Floating-Point Numbers:
NUMERIC LITERALS
• Numeric Operators
Math Functions
PERL INTEGERS
• Remember when the integer number is big, you often use a comma
as a separator to make it easier to read e.g., 123,763,213.
• However, Perl already uses a comma (,) as a separator in the list so
for integer numbers Perl uses an underscore character
• instead. In this case, 123,763,213 is written in Perl as 123_763_213
• Perl also supports other formats including binary, octal, and
hexadecimal.
• The following table shows you prefixes for formatting with binary,
octal, and hexadecimal integers.
PERL INTEGERS
Classwork

1) Write a Perl Program to convert a decimal number to binary


2) Write a Perl Program to convert an octal no to hexadecimal
Explanation
• Line 1 is called a shebang line, specifying the path to the Perl
interpreter.
• Line 2 & Line 3 are called pragmas enforce stricter coding
standards and enable warnings to help catch potential errors
• Line 5 is a comment
• Line 6 Prints a message prompting the user to enter a binary
number.
• Line 7 Reads the binary number entered by the user from the
standard input.
• Line 8 Removes the newline character at the end of the input.
Explanation
• Line 10 is a comment
• Line 11 Checks if the input contains only 0s and 1s using a regular
expression. And encounters an if…else statement
• Line 12 is a Comment
• Line 13 Uses the oct () function to convert the binary number to
decimal. The “0b” prefix indicates that the number is in binary format.
• Line 15 is a comment
• Line 16 Displays the result, the decimal equivalent of the entered binary
number.
• Line 19 If the input is not a valid binary number, an error message is
displayed

You might also like