SlideShare a Scribd company logo
Introduction to Perl Day 1 An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
What We Will Cover What is Perl?
Creating and running a Perl program
Getting help
Input and Output
Perl variables
Operators and Functions
What We Will Cover Conditional Constructs
Subroutines
Regular Expressions
Smart Matching
Finding and using Modules
Schedule 09:30 – Begin
11:00 – Coffee break (30 mins)
13:00 – Lunch (90 mins)
14:30 – Begin
16:00 – Coffee break (30 mins)
18:00 – End
Resources Slides available on-line http://mag-sol.com/train/public/2009-02/yapc Also see Slideshare http://www.slideshare.net/davorg/slideshows Get Satisfaction http://getsatisfaction.com/magnum
What is Perl?
Perl's  Name Practical Extraction and Reporting Language
Pathologically Eclectic Rubbish Lister
“Perl” is the language
“perl” is the compiler
Never “PERL”
Typical Uses of Perl Text processing
System administration tasks
CGI and web programming
Database interaction
Other Internet programming
Less Typical Uses of Perl Human Genome Project
NASA
What is Perl Like? General purpose programming language
Free (open source)‏
Fast
Flexible
Secure
Fun
The Perl Philosophy There's more than one way to do it
Three virtues of a programmer Laziness
Impatience
Hubris Share and enjoy!
Creating and Running a Perl Program
Creating a Perl Program Our first Perl program
print "Hello world\n";
Put this in a file called hello.pl
Running a Perl Program Running a Perl program from the command line
$ perl hello.pl
Running a Perl Program The "shebang" line (Unix, not Perl)
#!/usr/bin/perl
Make program executable
$ chmod +x hello.pl
Run from command line
$ ./hello.pl
Perl Comments Add comments to your code
Start with a hash (#)‏
Continue to end of line
# This is a hello world program print "Hello, world!\n"; # print
Command Line Options Many options to control execution of the program
For example, -w turns on warnings
Use on command line
perl -w hello.pl
Or on shebang line
#!/usr/bin/perl -w
More usually  use warnings
Getting Help
Perl Documentation Perl comes with a huge amount of documentation
Accessed through the  perldoc  command
perldoc perl
perldoc perltoc  – table of contents
Also online at http://perldoc.perl.org/
Lots of references through the course
Some Useful Pages perlintro
perldata
perlsyn
perlfaq
perlstyle
perlcheat
Many many more
Perl Variables
What is a Variable? A place where we can store data
A variable needs a name To put new data in it
To retrieve the data stored in it
Variable Names Contain alphanumeric characters and underscores
User variable names may not start with numbers
Variable names are preceded by a punctuation mark indicating the type of data
Types of Perl Variable Different types of variables start with a different symbol Scalar variables start with $
Array variables start with @
Hash variables start with % More on these types soon
Declaring Variables You don't need to declare variables in Perl
But it's a very good idea typos
scoping Using the  strict  pragma
use strict; my $var;
Scalar Variables Store a single item of data
my $name = "Arthur";
my $whoami =   'Just Another Perl Hacker';
my $meaning_of_life = 42;
my $number_less_than_1 = 0.000001;
my $very_large_number = 3.27e17;  # 3.27 times 10 to the power of 17
Type Conversions Perl converts between strings and numbers whenever necessary
Add int to a floating point number
my $sum = $meaning_of_life +   $number_less_than_1;
Putting a number into a string
print "$name says, 'The meaning of life is $sum.'\n";
Quoting Strings Single quotes don't expand variables or escape sequences
my $price = '$9.95';
Double quotes do
my $invline =   "24 widgets @ $price each\n";
Use a backslash to escape special characters in double quoted strings
print "He said \"The price is  \$300\"";
Better Quotes This can look ugly
print "He said \"The price is  \$300\"";
This is a tidier alternative
print qq(He said "The price is \$300");
Also works for single quotes
print q(He said "That's too expensive");
Undefined Values A scalar variable that hasn't had data put into it will contain the special value “undef”
Test for it with  defined()  function
if (defined($my_var)) { ... }
Array Variables Arrays contain an ordered list of scalar values
my @fruit = ('apples', 'oranges',   'guavas', 'passionfruit',   'grapes');
my @magic_numbers = (23, 42, 69);
my @random_scalars = ('mumble', 123.45,   'dave cross',   -300, $name);
Array Elements Accessing individual elements of an array
print $fruits[0]; # prints "apples"
Note: Indexes start from zero
print $random_scalars[2]; # prints "dave cross"
Note use of $ as individual element of an array is a scalar
Array Slices Returns a list of elements from an array
print @fruits[0,2,4]; # prints "apples", "guavas", # "grapes"
print @fruits[1 .. 3]; # prints "oranges", "guavas", # "passionfruit"
Note use of @ as we are accessing more than one element of the array
Setting Array Values $array[4] = 'something';
$array[400] = 'something else';
Also with slices
@array[4, 7 .. 9] = ('four', 'seven',   'eight',   'nine');
@array[1, 2] = @array[2, 1];
Doesn't need to be an array ($x, $y) = ($y, $x);
Array Size $#array  is the index of the last element in  @array
Therefore  $#array + 1  is the number of elements
$count = @array;

More Related Content

What's hot (20)

PDF
Perl Scripting
Varadharajan Mukundan
 
PPT
JavaScript - An Introduction
Manvendra Singh
 
PPT
Introduction To Catalyst - Part 1
Dan Dascalescu
 
PPTX
PHP Form Validation Technique
Morshedul Arefin
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PPSX
Java String class
DrRajeshreeKhande
 
PPT
Chapter 07 php forms handling
Dhani Ahmad
 
PPT
Javascript
mussawir20
 
PPT
Php forms
Anne Lee
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PDF
Php array
Nikul Shah
 
PPTX
Dictionaries and Sets
Munazza-Mah-Jabeen
 
PPTX
Php.ppt
Nidhi mishra
 
PPTX
JSON: The Basics
Jeff Fox
 
PPT
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
PPTX
PHP
Steve Fort
 
PPTX
CSS - Text Properties
hstryk
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PDF
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 
Perl Scripting
Varadharajan Mukundan
 
JavaScript - An Introduction
Manvendra Singh
 
Introduction To Catalyst - Part 1
Dan Dascalescu
 
PHP Form Validation Technique
Morshedul Arefin
 
Java String class
DrRajeshreeKhande
 
Chapter 07 php forms handling
Dhani Ahmad
 
Javascript
mussawir20
 
Php forms
Anne Lee
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
Php array
Nikul Shah
 
Dictionaries and Sets
Munazza-Mah-Jabeen
 
Php.ppt
Nidhi mishra
 
JSON: The Basics
Jeff Fox
 
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
CSS - Text Properties
hstryk
 
Fundamental JavaScript [UTC, March 2014]
Aaron Gustafson
 

Viewers also liked (16)

PPT
Gccgdb
selva raj
 
PPTX
C compilation process
RajKumar Rampelli
 
PDF
GCC Compiler as a Performance Testing tool for C programs
Daniel Ilunga
 
ODP
Gcc opt
Mark Veltzer
 
ODP
GCC, GNU compiler collection
Alberto Bustamante Reyes
 
PPT
GEM - GNU C Compiler Extensions Framework
Alexey Smirnov
 
PPTX
MinGw Compiler
Avnish Patel
 
PDF
How it's made: C++ compilers (GCC)
Sławomir Zborowski
 
PDF
GNU Compiler Collection - August 2005
Saleem Ansari
 
PPTX
Compiling Under Linux
PierreMASURE
 
DOCX
HRM - PM in GCC
Muhammad Danish Azad
 
PPT
NetBeans para Java, C, C++
Manuel Antonio
 
PPT
Principles of compiler design
Janani Parthiban
 
PPTX
G++ & GCC
Beste Ekmen
 
PDF
Deep C
Olve Maudal
 
PPTX
GCC
Kir Chou
 
Gccgdb
selva raj
 
C compilation process
RajKumar Rampelli
 
GCC Compiler as a Performance Testing tool for C programs
Daniel Ilunga
 
Gcc opt
Mark Veltzer
 
GCC, GNU compiler collection
Alberto Bustamante Reyes
 
GEM - GNU C Compiler Extensions Framework
Alexey Smirnov
 
MinGw Compiler
Avnish Patel
 
How it's made: C++ compilers (GCC)
Sławomir Zborowski
 
GNU Compiler Collection - August 2005
Saleem Ansari
 
Compiling Under Linux
PierreMASURE
 
HRM - PM in GCC
Muhammad Danish Azad
 
NetBeans para Java, C, C++
Manuel Antonio
 
Principles of compiler design
Janani Parthiban
 
G++ & GCC
Beste Ekmen
 
Deep C
Olve Maudal
 
Ad

Similar to Introduction to Perl - Day 1 (20)

ODP
Introduction to Perl
Dave Cross
 
ODP
Beginning Perl
Dave Cross
 
ODP
Perl Introduction
Marcos Rebelo
 
PPT
PERL.ppt
Farmood Alam
 
ODP
Intermediate Perl
Dave Cross
 
PDF
Scripting3
Nao Dara
 
PPTX
PERL PROGRAMMING LANGUAGE Basic Introduction
johnboladevice
 
PPTX
Perl courseparti
ernlow
 
PPT
Introduction to Perl
NBACriteria2SICET
 
PDF
Tutorial perl programming basic eng ver
Qrembiezs Intruder
 
PDF
Practical approach to perl day1
Rakesh Mukundan
 
PPTX
Perl slid
pacatarpit
 
PDF
newperl5
tutorialsruby
 
PDF
newperl5
tutorialsruby
 
PPT
Perl tutorial
Manav Prasad
 
PPT
Perl Development (Sample Courseware)
Garth Gilmour
 
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
DOCX
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
PDF
Introduction to Perl and BioPerl
Bioinformatics and Computational Biosciences Branch
 
Introduction to Perl
Dave Cross
 
Beginning Perl
Dave Cross
 
Perl Introduction
Marcos Rebelo
 
PERL.ppt
Farmood Alam
 
Intermediate Perl
Dave Cross
 
Scripting3
Nao Dara
 
PERL PROGRAMMING LANGUAGE Basic Introduction
johnboladevice
 
Perl courseparti
ernlow
 
Introduction to Perl
NBACriteria2SICET
 
Tutorial perl programming basic eng ver
Qrembiezs Intruder
 
Practical approach to perl day1
Rakesh Mukundan
 
Perl slid
pacatarpit
 
newperl5
tutorialsruby
 
newperl5
tutorialsruby
 
Perl tutorial
Manav Prasad
 
Perl Development (Sample Courseware)
Garth Gilmour
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
Introduction to Perl and BioPerl
Bioinformatics and Computational Biosciences Branch
 
Ad

More from Dave Cross (20)

PDF
Measuring the Quality of Your Perl Code
Dave Cross
 
PDF
Apollo 11 at 50 - A Simple Twitter Bot
Dave Cross
 
PDF
Monoliths, Balls of Mud and Silver Bullets
Dave Cross
 
PPTX
The Professional Programmer
Dave Cross
 
PDF
I'm A Republic (Honest!)
Dave Cross
 
PDF
Web Site Tune-Up - Improve Your Googlejuice
Dave Cross
 
PDF
Modern Perl Web Development with Dancer
Dave Cross
 
PDF
Freeing Tower Bridge
Dave Cross
 
PDF
Modern Perl Catch-Up
Dave Cross
 
PDF
Error(s) Free Programming
Dave Cross
 
PDF
Medium Perl
Dave Cross
 
PDF
Modern Web Development with Perl
Dave Cross
 
PDF
Improving Dev Assistant
Dave Cross
 
PDF
Conference Driven Publishing
Dave Cross
 
PDF
Conference Driven Publishing
Dave Cross
 
PDF
TwittElection
Dave Cross
 
PDF
Perl in the Internet of Things
Dave Cross
 
PDF
Return to the Kingdom of the Blind
Dave Cross
 
PDF
Github, Travis-CI and Perl
Dave Cross
 
ODP
Object-Oriented Programming with Perl and Moose
Dave Cross
 
Measuring the Quality of Your Perl Code
Dave Cross
 
Apollo 11 at 50 - A Simple Twitter Bot
Dave Cross
 
Monoliths, Balls of Mud and Silver Bullets
Dave Cross
 
The Professional Programmer
Dave Cross
 
I'm A Republic (Honest!)
Dave Cross
 
Web Site Tune-Up - Improve Your Googlejuice
Dave Cross
 
Modern Perl Web Development with Dancer
Dave Cross
 
Freeing Tower Bridge
Dave Cross
 
Modern Perl Catch-Up
Dave Cross
 
Error(s) Free Programming
Dave Cross
 
Medium Perl
Dave Cross
 
Modern Web Development with Perl
Dave Cross
 
Improving Dev Assistant
Dave Cross
 
Conference Driven Publishing
Dave Cross
 
Conference Driven Publishing
Dave Cross
 
TwittElection
Dave Cross
 
Perl in the Internet of Things
Dave Cross
 
Return to the Kingdom of the Blind
Dave Cross
 
Github, Travis-CI and Perl
Dave Cross
 
Object-Oriented Programming with Perl and Moose
Dave Cross
 

Recently uploaded (20)

PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Digital Circuits, important subject in CS
contactparinay1
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 

Introduction to Perl - Day 1