SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
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!

Más contenido relacionado

La actualidad más candente

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

La actualidad más candente (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
 

Similar a 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
 

Similar a 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
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
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
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Perl Primer