SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
PerlA Primer
Anuradha Weeraman
anu@taprobane.org
http://www.linux.lk/~anu/
21 December 2005
 
History & Roadmap
●  Created by Larry Wall
●  Powerful text handling
●  Evolved with the Internet
●  CGI / mod_perl
●  OOP
●  Matured as a serious platform for development
●  Perl 6 / Parrot
Key Features
●  Simple, intuitive, versatile syntax
●  Cross platform
●  Regular expression support
●  Supports OOP semantics
●  Open source – dual licensed
●  Thriving community
●  CPAN
Supported Platforms
Linux, AIX, IRIX, HP/UX, BSD, Solaris, Tru64, 
DOS, Windows 3.1, Acorn Risc OS, 
Windows 95/98/NT/2000/XP, Apple Macintosh, 
Amiga, BeOS, OS/2, AS/400, OS390, VMS, 
OpenVMS, Stratus, Tandem, EPOC, QNX, Plan 
9, Atari ST, GNU HURD, Cygwin, HP 3000 
MPE etc.
Practical
Extraction &
Reporting
Language
Pathologically
Eclectic
Rubbish
Lister
 There is More Than 
One Way To Do It
TMTOWDI
(tim­towdi)
Hello World
print “Hello World!n”;
Hello World
perl ­e 'print “Hello World!n”;'
Hello World
echo 'print “Hello World!n”' | perl ­
Running the First Script
File: HelloWorld.pl
#!/usr/bin/perl
print “Greetings, Earth People!n”;
$ perl HelloWorld.pl
   or
$ perl < HelloWorld.pl
   or
$ chmod a+x HelloWorld.pl
$ ./HelloWorld.pl
Program Structure
● Lenient structure.
● No indentation, whitespace rules.
● Every simple statement ends in a semi­colon.
● All user created objects, except subroutines, are 
automatically created with a null or zero value.
● Comments are prefixed with a #
● Lines starting with = are interpreted as the start of 
a section of embedded documentation
Data Types
● Scalars
● Arrays
● Hashes
Scalars
● Simple variables
● Preceded by a $
● Loosely typed
$var = “random string”;
$var = “with n special t characters”;
$var = 23;
$var = 15.3439;
$var = 0xff; # Hexadecimal
$var = 033; # Octal
Arrays
● Ordered list of scalars
● Preceded by @
● Elements are accessed by subscript
● Elements are numbered starting from 0.
● Negative indexes count backwards
@array = (1, 1, 2, 3, 5, 8, 13);
print $array[4]; # Prints 5
Hashes
● Unordered set of key/value pairs
● Preceded by %
● Keys are used as subscripts to access elements
%lives = ( “cat” => 9, “dog” => 1.5);
%lives = (“cat”, 9, “dog”, 1.5);
$lives{'cat'} # Returns 9
$lives{'dog'} = 2;
Conditionals
if (expression) {block} else {block}
unless (expression) {block} else {block}
if (expression1) {block}
elsif (expression2) {block}
elsif (lastexpression) {block}
else {block}
Loops
while (<INFILE>) {
    print OUTFILE, "$_n";
}
for ($i = 1; $i < 10; $i++) {
    ... # Do something
}
Loops
foreach var (list) {
    ...
}
● Can be used to iterate through hashes and arrays
foreach $element (@array) {
print “$elementn”;
}
Modifiers
statement if EXPR;
statement unless EXPR;
statement while EXPR;
statement until EXPR;
$i = $num if ($num < 50); # $i will be less than 50
$j = $cnt unless ($cnt < 100); # $j will be >= 100
$lines++ while <FILE>;
print "$_n" until /The end/;
Modifiers
do {
    $line = <STDIN>;
    ...
} until $line eq ".n";
do {
    $line = <STDIN>;
    ...
} while length ($line) > 1;
Loop Control
LINE: while (<SCRIPT>) {
    print;
    next LINE if /^#/;      # discard comments
}
last label
next label
redo label
Special Variables
● Perl has many special variables
● $_ ­ implicit assignment
foreach ('hickory','dickory','doc') {
print; # Prints $_
}
Special Arrays and Hashes
● @ARGV – Command line arguments passed into 
the script
● @INC – List of places to look for scripts and 
modules
● %ENV – Hash containing the environment the 
script is running in
Special Filehandles
● STDIN
● STDOUT
● STDERR
Subroutines
sub name {block}
sub name (prototype) {block}
name(args); # & is optional with parentheses
name args; # Parens optional if predeclared
&name; # Passes current @_ to subroutine
● The implicit return value is the result of the last 
evaluated statement
Switches
●  perl ­n assumes a
while (<>) {
... # your script goes here
}
around the command line arguments
● perl ­p does the same while printing the line
Example: perl ­ne “print if /PATTERN/” filename
More Switches
● To search and replace text in a file:
 perl ­i.bak ­p ­e 's/match/replace/g' filename
● ­i switch backs up modified files with a .bak 
extension.
● See perl –help for more switches
Installing Modules
Download foo­module.tar.gz
$ tar zxvf foo­module.tar.gz
$ cd foo­module
$ perl Configure.PL
$ make
$ make test
# make install
           OR
use CPAN.
CPAN
●  CPAN.org
●  Comprehensive Perl Archive Network
●  Mirrors all over the world
●  Command line shell
●  Bundled with standard Perl distribution
●  Intuitive module management
CPAN
perl ­MCPAN ­e shell
cpan> install Term::ReadKey
cpan> install Term::ReadLine
cpan> install Bundle::CPAN
cpan> h or ?
 
Thank You!

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Linux OS
Introduction to Linux OSIntroduction to Linux OS
Introduction to Linux OS
Mohammed Safwat
 

Was ist angesagt? (10)

Adding Extended Attribute Support to NFS
Adding Extended Attribute Support to NFSAdding Extended Attribute Support to NFS
Adding Extended Attribute Support to NFS
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linux
 
ZendCon - Linux 101
ZendCon - Linux 101ZendCon - Linux 101
ZendCon - Linux 101
 
Before begining linux
Before begining linuxBefore begining linux
Before begining linux
 
Easy Installation and Setup of PostgreSQL on Linux, OSX, & Windows
Easy Installation and Setup of PostgreSQL on Linux, OSX, & WindowsEasy Installation and Setup of PostgreSQL on Linux, OSX, & Windows
Easy Installation and Setup of PostgreSQL on Linux, OSX, & Windows
 
Gentoo on a 486
Gentoo on a 486Gentoo on a 486
Gentoo on a 486
 
Mini-Training: Docker
Mini-Training: DockerMini-Training: Docker
Mini-Training: Docker
 
FreeBSD Portscamp, Kuala Lumpur 2016
FreeBSD Portscamp, Kuala Lumpur 2016FreeBSD Portscamp, Kuala Lumpur 2016
FreeBSD Portscamp, Kuala Lumpur 2016
 
Introduction to Linux OS
Introduction to Linux OSIntroduction to Linux OS
Introduction to Linux OS
 
BSidesKnoxville 2019 - Unix: The Other White Meat
BSidesKnoxville 2019 - Unix: The Other White MeatBSidesKnoxville 2019 - Unix: The Other White Meat
BSidesKnoxville 2019 - Unix: The Other White Meat
 

Ähnlich wie Perl Primer

What is open source
What is open sourceWhat is open source
What is open source
Kumar
 
From Silicon to Software - IIT Madras
From Silicon to Software - IIT MadrasFrom Silicon to Software - IIT Madras
From Silicon to Software - IIT Madras
Aanjhan Ranganathan
 

Ähnlich wie Perl Primer (20)

An Introduction to Linux
An Introduction to LinuxAn Introduction to Linux
An Introduction to Linux
 
Todd Vatalaro, The power of x
Todd Vatalaro, The power of xTodd Vatalaro, The power of x
Todd Vatalaro, The power of x
 
What is open source
What is open sourceWhat is open source
What is open source
 
From Silicon to Software - IIT Madras
From Silicon to Software - IIT MadrasFrom Silicon to Software - IIT Madras
From Silicon to Software - IIT Madras
 
perl lauange
perl lauangeperl lauange
perl lauange
 
Pearl
PearlPearl
Pearl
 
Perl
PerlPerl
Perl
 
Intro
IntroIntro
Intro
 
Intro
IntroIntro
Intro
 
Linux Introduction
Linux IntroductionLinux Introduction
Linux Introduction
 
Apples and Oranges-- Introductory Comparison between PHP and Python
Apples and Oranges-- Introductory Comparison between PHP and PythonApples and Oranges-- Introductory Comparison between PHP and Python
Apples and Oranges-- Introductory Comparison between PHP and Python
 
Why Python
Why PythonWhy Python
Why Python
 
Internet all the things - curl everywhere!
Internet all the things - curl everywhere!Internet all the things - curl everywhere!
Internet all the things - curl everywhere!
 
Linux Knowledge Transfer
Linux Knowledge TransferLinux Knowledge Transfer
Linux Knowledge Transfer
 
Linux para iniciantes
Linux para iniciantesLinux para iniciantes
Linux para iniciantes
 
Learn PERL at ASIT
Learn PERL at ASITLearn PERL at ASIT
Learn PERL at ASIT
 
Linux introduction (eng)
Linux introduction (eng)Linux introduction (eng)
Linux introduction (eng)
 
Cross platform php
Cross platform phpCross platform php
Cross platform php
 
Nethemba metasploit
Nethemba metasploitNethemba metasploit
Nethemba metasploit
 
ICIT2013-Keynote-Speech-In-Bali
ICIT2013-Keynote-Speech-In-BaliICIT2013-Keynote-Speech-In-Bali
ICIT2013-Keynote-Speech-In-Bali
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Kürzlich hochgeladen (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Perl Primer