SlideShare ist ein Scribd-Unternehmen logo
1 von 23
PERLPERL
Practical Extraction & Reporting LanguagePractical Extraction & Reporting Language
History
Developed by Larry Wall in
1987.
A combination of C and Unix
Shell programming language.
Originally developed for UNIX,
but now runs under all OS.
“A good perl program is one that gets the job
done before your boss fires you.”
Larry Wall, the creator of Perl.
Contents
Introduction
Variables
Operators
Conditional Statements and Loops
Regular Expressions
Subroutines
File Handling
Many more....
File Management Contd..
binmode(HANDLE): change file mode from text to binary
unlink("myfile"): delete file myfile
rename("file1","file2"): change name of file file1 to file2
mkdir("mydir"): create directory mydir
rmdir("mydir"): delete directory mydir
chdir("mydir"): change the current directory to mydir
system("command"): execute command command
die("message"): exit program with message message
warn("message"): warn user about problem message
Introduction
Interpreted language, optimized for string manipulation, I/O and
system tasks.
Speed of development
Easy to use, portable
Freely available
Extension is .pl
Execution : perl file.pl
All perl program goes through 2 phases :
a compile phase where the syntax is checked and the source code,
including any modules used, is converted into bytecode.
a run-time phase where the bytecode is processed into machine
instructions and executed.
Basic Syntax
All statements should end with ';'
# is used for commenting a line
Perl is case sensitive
Basic options : -c, -v, -w
Variables
Three basic data types, scalars, arrays and hashes
Scalars : Holds a single value of any type.
$var1 = 123
$var2 = “Hello, how r u”
$var3 = 1.23
$1var = 123 # Error
Built-in Functions
Chomp() : The chomp() function will remove (usually) any newline
character from the end of a string.
Chop() : The chop() function will remove the last character of a string
(or group of strings) regardless of what that character is.
Control Flow
Syntax same as C
If Construct :
if (expr) {
stmt block;
}
If Else Construct :
Loops
Loops in Perl are written in the same way as in C.
Only remember that the loop variable is a scalar variable and must be
preceded by a $ sign.
while (expr) {
stmt block;
}
do {
stmt block;
} while (expr);
for (expr1 ; expr2 ; expr3) {
stmt block;
}
foreach var (listexpr) {
Foreach
Behaves same as for loop.
foreach $person (@names) {
print "$person";
}
$_ : Default Input and Pattern Searching Variable
foreach (@names) {
print "$_";
}
Lists
List is a group of scalars used to initialize and array or hash.
Elements can be any type of scalar data.
List functions :
Join : Joins list values
Split : A string is splited
Map : Evaluates expression or blocks
Grep : Returns a sublist of list for each a specific criteria is true
Arrays
In Perl an array does not have to be declared before it is used.
The size of an array increases dynamically.
An array variable has an @ before it.
@num = (1,2,3,4,5);
@num = (1..5);
@mix_arr = ("Tom", 5, "Cruise", 90, "Jane", 35.67 34, 78, "I am Bond");
You can assign an array element to any index - even one that is
beyond the current range of index numbers.
The in-between elements are assigned null values.
Push, Pop and Splice
The push function pushes an element onto an array. If the last element in
an array has index 5, pushing an element onto the array makes it the
element with index 6.
The syntax is:
push(@arrayname, value);
The pop function pops off the last element off the array. So, if the last
element in the array @myarray is 99, then the statement
$num = pop(@myarray);
assigns the value 99 to $num.
The splice function is used to pop more than one element.
(splice(@myarray, -3)
Unshift and Shift
These work like the push and pop operators, but add and subtract from
the beginning of an array rather than the end.
The syntax is
unshift(@arrayname, value);
shift(@arrayname);
Associative Array or Hash
Hashes are a list of scalars, but instead of being accessed by index
number, they are accessed by a key.
Syntax : %myhash = ('Key1', 'Val1', 'Key2', 'Val2', 'Key3', 'Val3')
Index No Value
0 Spain
1 Belgium
2 Germany
3 Netherlan
ds
Key Value
SP Spain
BL Belgium
GE Germany
NL Netherland
s
Perl Command Line
Read from command prompt.
Go to example.
Hash Functions
Assigning : $countries{PT} = 'Portugal';
Deleting : delete $countries{NL};
Print all the keys : print keys %countries;
Print all the values : print values %countries;
A slice of hash : print @countries{'NL', 'BL'};
How many elements : print scalar(keys %countries);
Does the key exist : print “I exist n” if exists $countries{'NL'};
Subroutine
Syntax for subroutines :
Sub subname {
Stmt block;
}
Value of the last expression evaluated by the subroutine is automatically
considered to be subroutine's return value.
Foreach $var (&subname) {
Stmt block;
}
Perl defines 3 special subroutines that are executed at specific times.
BEGIN : Called when our program starts running.
END : Called when the program terminates.
AUTOLOAD : Called when the program cannot find a subroutine it is
File Management
open(INFILE,"myfile"): reading
open(OUTFILE,">myfile"): writing
open(OUTFILE,">>myfile"): appending
open(INFILE,"someprogram |"): reading from program
open(OUTFILE,"| someprogram"): writing to program
opendir(DIR,"mydirectory"): open directo
Operations on an open file handle
$a = <INFILE>: read a line from INFILE into $a
@a = <INFILE>: read all lines from INFILE into @a
$a = readdir(DIR): read a filename from DIR into $a
@a = readdir(DIR): read all filenames from DIR into @a
read(INFILE,$a,$length): read $length characters from INFILE into $a
print OUTFILE "text": write some text in OUTFILE
Close files / directories
Perl Debugger
Perl -d myprogram.pl
Debugger commands :
l : lists the next few statements.
l 10 : specifies the line number
l 10-15 : display a range of lines
l subroutine : display subroutine
- : display the immediately preceding the last displayed line
/search'/ : Search for a line containing the pattern
?backsearch? : To search backword for a pattern
S : lists all the subroutines in the file
s : execute single statement and displays next statement to execute
n : same as s command, but n command does not enter subroutine,
directly execute it
r : if we are inside a subroutine and do not want to execute further, this
command finishes subroutine execution and returns to the last
statement called the subroutine
X : print the value of a particular variable
Resources
Perl.com
Perl.org
Lists.perl.org
Perlmonks
Stackoverflow
Perl mongers
Search.cpan.org
Continued...

Weitere ähnliche Inhalte

Was ist angesagt?

Perl names values and variables
Perl names values and variablesPerl names values and variables
Perl names values and variablessana mateen
 
JavaScript Classes and Inheritance
JavaScript Classes and InheritanceJavaScript Classes and Inheritance
JavaScript Classes and Inheritancemarcheiligers
 
Perl programming language
Perl programming languagePerl programming language
Perl programming languageElie Obeid
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingHenri Tremblay
 
Programming in Computational Biology
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational BiologyAtreyiB
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsMark Baker
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1Nihar Ranjan Paital
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2Nihar Ranjan Paital
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsNihar Ranjan Paital
 
The Ring programming language version 1.5.3 book - Part 32 of 184
The Ring programming language version 1.5.3 book - Part 32 of 184The Ring programming language version 1.5.3 book - Part 32 of 184
The Ring programming language version 1.5.3 book - Part 32 of 184Mahmoud Samir Fayed
 
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsanePerl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsaneRicardo Signes
 

Was ist angesagt? (20)

Perl names values and variables
Perl names values and variablesPerl names values and variables
Perl names values and variables
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
JavaScript Classes and Inheritance
JavaScript Classes and InheritanceJavaScript Classes and Inheritance
JavaScript Classes and Inheritance
 
Linq introduction
Linq introductionLinq introduction
Linq introduction
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
 
Programming in Computational Biology
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational Biology
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
Perl
PerlPerl
Perl
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
 
Arrays in linux
Arrays in linuxArrays in linux
Arrays in linux
 
The Ring programming language version 1.5.3 book - Part 32 of 184
The Ring programming language version 1.5.3 book - Part 32 of 184The Ring programming language version 1.5.3 book - Part 32 of 184
The Ring programming language version 1.5.3 book - Part 32 of 184
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsanePerl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
 
Subroutines
SubroutinesSubroutines
Subroutines
 

Ähnlich wie Perl

WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTHBhavsingh Maloth
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasGanesh Samarthyam
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Wildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell VariablesWildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell VariablesGaurav Bisht
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,filesShankar D
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsRoy Zimmer
 
Using Unix
Using UnixUsing Unix
Using UnixDr.Ravi
 
Stata cheatsheet programming
Stata cheatsheet programmingStata cheatsheet programming
Stata cheatsheet programmingTim Essam
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionProf. Wim Van Criekinge
 
Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line conceptsArtem Nagornyi
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge O T
 

Ähnlich wie Perl (20)

Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT VI BY BHAVSINGH MALOTH
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Wildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell VariablesWildcards, Simple Shell Programs and Shell Variables
Wildcards, Simple Shell Programs and Shell Variables
 
Perl_Part6
Perl_Part6Perl_Part6
Perl_Part6
 
Shell programming
Shell programmingShell programming
Shell programming
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
 
Perl tutorial final
Perl tutorial finalPerl tutorial final
Perl tutorial final
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
 
Using Unix
Using UnixUsing Unix
Using Unix
 
Stata cheatsheet programming
Stata cheatsheet programmingStata cheatsheet programming
Stata cheatsheet programming
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Unix command line concepts
Unix command line conceptsUnix command line concepts
Unix command line concepts
 
1. python
1. python1. python
1. python
 
Python cheatsheet for beginners
Python cheatsheet for beginnersPython cheatsheet for beginners
Python cheatsheet for beginners
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
 

Kürzlich hochgeladen

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 

Kürzlich hochgeladen (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 

Perl

  • 1. PERLPERL Practical Extraction & Reporting LanguagePractical Extraction & Reporting Language
  • 2. History Developed by Larry Wall in 1987. A combination of C and Unix Shell programming language. Originally developed for UNIX, but now runs under all OS. “A good perl program is one that gets the job done before your boss fires you.” Larry Wall, the creator of Perl.
  • 3. Contents Introduction Variables Operators Conditional Statements and Loops Regular Expressions Subroutines File Handling Many more....
  • 4. File Management Contd.. binmode(HANDLE): change file mode from text to binary unlink("myfile"): delete file myfile rename("file1","file2"): change name of file file1 to file2 mkdir("mydir"): create directory mydir rmdir("mydir"): delete directory mydir chdir("mydir"): change the current directory to mydir system("command"): execute command command die("message"): exit program with message message warn("message"): warn user about problem message
  • 5. Introduction Interpreted language, optimized for string manipulation, I/O and system tasks. Speed of development Easy to use, portable Freely available Extension is .pl Execution : perl file.pl All perl program goes through 2 phases : a compile phase where the syntax is checked and the source code, including any modules used, is converted into bytecode. a run-time phase where the bytecode is processed into machine instructions and executed.
  • 6. Basic Syntax All statements should end with ';' # is used for commenting a line Perl is case sensitive Basic options : -c, -v, -w
  • 7. Variables Three basic data types, scalars, arrays and hashes Scalars : Holds a single value of any type. $var1 = 123 $var2 = “Hello, how r u” $var3 = 1.23 $1var = 123 # Error
  • 8. Built-in Functions Chomp() : The chomp() function will remove (usually) any newline character from the end of a string. Chop() : The chop() function will remove the last character of a string (or group of strings) regardless of what that character is.
  • 9. Control Flow Syntax same as C If Construct : if (expr) { stmt block; } If Else Construct :
  • 10. Loops Loops in Perl are written in the same way as in C. Only remember that the loop variable is a scalar variable and must be preceded by a $ sign. while (expr) { stmt block; } do { stmt block; } while (expr); for (expr1 ; expr2 ; expr3) { stmt block; } foreach var (listexpr) {
  • 11. Foreach Behaves same as for loop. foreach $person (@names) { print "$person"; } $_ : Default Input and Pattern Searching Variable foreach (@names) { print "$_"; }
  • 12. Lists List is a group of scalars used to initialize and array or hash. Elements can be any type of scalar data. List functions : Join : Joins list values Split : A string is splited Map : Evaluates expression or blocks Grep : Returns a sublist of list for each a specific criteria is true
  • 13. Arrays In Perl an array does not have to be declared before it is used. The size of an array increases dynamically. An array variable has an @ before it. @num = (1,2,3,4,5); @num = (1..5); @mix_arr = ("Tom", 5, "Cruise", 90, "Jane", 35.67 34, 78, "I am Bond"); You can assign an array element to any index - even one that is beyond the current range of index numbers. The in-between elements are assigned null values.
  • 14. Push, Pop and Splice The push function pushes an element onto an array. If the last element in an array has index 5, pushing an element onto the array makes it the element with index 6. The syntax is: push(@arrayname, value); The pop function pops off the last element off the array. So, if the last element in the array @myarray is 99, then the statement $num = pop(@myarray); assigns the value 99 to $num. The splice function is used to pop more than one element. (splice(@myarray, -3)
  • 15. Unshift and Shift These work like the push and pop operators, but add and subtract from the beginning of an array rather than the end. The syntax is unshift(@arrayname, value); shift(@arrayname);
  • 16. Associative Array or Hash Hashes are a list of scalars, but instead of being accessed by index number, they are accessed by a key. Syntax : %myhash = ('Key1', 'Val1', 'Key2', 'Val2', 'Key3', 'Val3') Index No Value 0 Spain 1 Belgium 2 Germany 3 Netherlan ds Key Value SP Spain BL Belgium GE Germany NL Netherland s
  • 17. Perl Command Line Read from command prompt. Go to example.
  • 18. Hash Functions Assigning : $countries{PT} = 'Portugal'; Deleting : delete $countries{NL}; Print all the keys : print keys %countries; Print all the values : print values %countries; A slice of hash : print @countries{'NL', 'BL'}; How many elements : print scalar(keys %countries); Does the key exist : print “I exist n” if exists $countries{'NL'};
  • 19. Subroutine Syntax for subroutines : Sub subname { Stmt block; } Value of the last expression evaluated by the subroutine is automatically considered to be subroutine's return value. Foreach $var (&subname) { Stmt block; } Perl defines 3 special subroutines that are executed at specific times. BEGIN : Called when our program starts running. END : Called when the program terminates. AUTOLOAD : Called when the program cannot find a subroutine it is
  • 20. File Management open(INFILE,"myfile"): reading open(OUTFILE,">myfile"): writing open(OUTFILE,">>myfile"): appending open(INFILE,"someprogram |"): reading from program open(OUTFILE,"| someprogram"): writing to program opendir(DIR,"mydirectory"): open directo Operations on an open file handle $a = <INFILE>: read a line from INFILE into $a @a = <INFILE>: read all lines from INFILE into @a $a = readdir(DIR): read a filename from DIR into $a @a = readdir(DIR): read all filenames from DIR into @a read(INFILE,$a,$length): read $length characters from INFILE into $a print OUTFILE "text": write some text in OUTFILE Close files / directories
  • 21. Perl Debugger Perl -d myprogram.pl Debugger commands : l : lists the next few statements. l 10 : specifies the line number l 10-15 : display a range of lines l subroutine : display subroutine - : display the immediately preceding the last displayed line /search'/ : Search for a line containing the pattern ?backsearch? : To search backword for a pattern S : lists all the subroutines in the file s : execute single statement and displays next statement to execute n : same as s command, but n command does not enter subroutine, directly execute it r : if we are inside a subroutine and do not want to execute further, this command finishes subroutine execution and returns to the last statement called the subroutine X : print the value of a particular variable