SlideShare uma empresa Scribd logo
1 de 169
Introduction to Perl An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
What We Will Cover ,[object Object]
Creating and running a Perl program
Input and Output
Perl variables
Operators and Functions
Conditional Constructs
What We Will Cover ,[object Object]
Regular Expressions
References
Smart Matching
Finding and using Modules
Further Information
Schedule ,[object Object]
11:15 – Coffee break
13:00 – Lunch
14:00 – Begin
15:30 – Coffee break
17:00 – End
Resources ,[object Object]
What is Perl?
Perl's Name ,[object Object]
Pathologically Eclectic Rubbish Lister
"Perl" is the language
"perl" is the compiler
Never "PERL"
Typical uses of Perl ,[object Object]
System administration tasks
CGI and web programming
Database interaction
Other Internet programming
Less typical uses of Perl ,[object Object]
NASA
What is Perl Like? ,[object Object]
Free (open source)‏
Fast
Flexible
Secure
Fun
The Perl Philosophy ,[object Object]
Three virtues of a programmer ,[object Object]
Impatience
Hubris ,[object Object]
Creating and Running a Perl Program
Creating a Perl Program ,[object Object]
Put this in a file called hello.pl
Running a Perl Program ,[object Object]
$ perl hello.pl
Running a Perl Program ,[object Object]
Make program executable $ chmod +x hello.pl
Run from command line $ ./hello.pl
Perl Comments ,[object Object]
Start with a hash ( # )‏
Continue to end of line
# This is a hello world program print "Hello, world!"; # print
Command Line Options ,[object Object]
For example,  -w  turns on warnings
Use on command line perl -w hello.pl
Or on shebang line #!/usr/bin/perl -w
Perl variables
What is a Variable? ,[object Object]
A variable needs a name to ,[object Object]
put new data in it
Variable Names ,[object Object]
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 ,[object Object],[object Object]
Array variables start with @
Hash variables start with % ,[object Object]
Declaring Variables ,[object Object]
But it's a very good idea ,[object Object]
scoping ,[object Object]
Scalar Variables ,[object Object]
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 ,[object Object]
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.'";
Quoting Strings ,[object Object]
Double quotes do my $invline =   "24 widgets @ $price each";
Use a backslash to escape special characters in double quoted strings print "He said amp;quot;The price is 300amp;quot;";
Better Quotes ,[object Object]
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 ,[object Object]
Test for it with “defined()” function
if (defined($my_var)) { ... }
Array Variables ,[object Object]
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 ,[object Object]
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 ,[object Object]
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 ,[object Object]
Also with slices
@array[4, 7 .. 9] = ('four', 'seven',    'eight', 'nine');
@array[1, 2] = @array[2, 1];
Doesn't need to be an array! ,[object Object]
Array Size ,[object Object]
Therefore  $#array + 1  is the number of elements
$count = @array;  does the same thing and is easier to understand
Hash Variables ,[object Object]
Initialised with a list %french = ('one', 'un', 'two', 'deux',   'three', 'trois');
"fat comma" (=>) is easier to understand %german = (one  => 'ein',   two  => 'zwei',    three => 'drei');
Accessing Hash Values ,[object Object]
print $german{two};
As with arrays, notice the use of $ to indicate that we're accessing a single value
Hash Slices ,[object Object]
Returns a list of elements from a hash print @french{'one','two','three'}; # prints "un", "deux" & "trois"
Again, note use of @ as we are accessing more than one value from the hash
Setting Hash Values ,[object Object]
$hash{bar} = 'something else';
Also with slices
@hash{'foo', 'bar'} =  ('something', 'else');
@hash{'foo', 'bar'} =  @hash{'bar', 'foo'};
More About Hashes ,[object Object]
There is no equivalent to  $#array
print %hash  is unhelpful
We'll see ways round these restrictions later
Special Perl Variables ,[object Object]
Many of them have punctuation marks as names
Others have names in ALL_CAPS
They are documented in perlvar
The Default Variable ,[object Object]
print; # prints the value of $_
If a piece of Perl code seems to be missing a variable, then it's probably using  $_
Think of “it” or “that” in English
Using $_ ,[object Object]
Three uses of $_
A Special Array ,[object Object]
Contains your programs command line arguments
perl printargs.pl foo bar baz
my $num = @ARGV; print "$num arguments: @ARGV";
A Special Hash ,[object Object]
Contains the  environment variables  that your script has access to.
Keys are the variable names Values are the… well… values!
print $ENV{PATH};
Input and Output
Input and Output ,[object Object]
We'll see more input and output methods later in the day
But here are some simple methods
Output ,[object Object]
print “Hello world”;
Input ,[object Object]
$input = <STDIN>;
< ... >  is the “read from filehandle” operator
STDIN  is the standard input filehandle
A Complete Example ,[object Object]
Operators and Functions
Operators and Functions ,[object Object]
&quot;Things&quot; that do &quot;stuff&quot;
Routines built into Perl to manipulate data
Other languages have a strong distinction between operators and functions ,[object Object],[object Object]
Arithmetic Operators ,[object Object]
Less standard operations modulus (%), exponentiation (**)‏
$speed = $distance / $time; $vol = $length * $breadth * $height; $area = $pi * ($radius ** 2); $odd = $number % 2;
Shortcut Operators ,[object Object]
Can be abbreviated to $total += $amount;
Even shorter $x++; # same as $x += 1 or $x = $x + 1 $y--; # same as $y -= 1 or $y = $y - 1

Mais conteúdo relacionado

Mais procurados

P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
phelios
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
Core Lee
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
mussawir20
 

Mais procurados (20)

Php Basic
Php BasicPhp Basic
Php Basic
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Php2
Php2Php2
Php2
 
Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
Introduction to Modern Perl
Introduction to Modern PerlIntroduction to Modern Perl
Introduction to Modern Perl
 
Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
Intro to Perl and Bioperl
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl style
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 
slidesharenew1
slidesharenew1slidesharenew1
slidesharenew1
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 

Destaque

Destaque (17)

Idiotic Perl
Idiotic PerlIdiotic Perl
Idiotic Perl
 
Perl Training
Perl TrainingPerl Training
Perl Training
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
 
Proud To Use Perl
Proud To Use PerlProud To Use Perl
Proud To Use Perl
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)
 
Saint Perl 2009: CGI::Ajax demo
Saint Perl 2009: CGI::Ajax demoSaint Perl 2009: CGI::Ajax demo
Saint Perl 2009: CGI::Ajax demo
 
Practical approach to perl day2
Practical approach to perl day2Practical approach to perl day2
Practical approach to perl day2
 
Achieving the Impossible with Perl
Achieving the Impossible with PerlAchieving the Impossible with Perl
Achieving the Impossible with Perl
 
Beginning Kindle Hackery
Beginning Kindle HackeryBeginning Kindle Hackery
Beginning Kindle Hackery
 
The Essential Perl Hacker's Toolkit
The Essential Perl Hacker's ToolkitThe Essential Perl Hacker's Toolkit
The Essential Perl Hacker's Toolkit
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Functional perl
Functional perlFunctional perl
Functional perl
 
Perl University: Getting Started with Perl
Perl University: Getting Started with PerlPerl University: Getting Started with Perl
Perl University: Getting Started with Perl
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
Perl Programming - 01 Basic Perl
Perl Programming - 01 Basic PerlPerl Programming - 01 Basic Perl
Perl Programming - 01 Basic Perl
 

Semelhante a Beginning Perl

Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
mussawir20
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone
 

Semelhante a Beginning Perl (20)

Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
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
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented Perl
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
 
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng verTutorial perl programming basic eng ver
Tutorial perl programming basic eng ver
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Cleancode
CleancodeCleancode
Cleancode
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Perl slid
Perl slidPerl slid
Perl slid
 
Basic PHP
Basic PHPBasic PHP
Basic PHP
 
PHP MySQL
PHP MySQLPHP MySQL
PHP MySQL
 
Javascript
JavascriptJavascript
Javascript
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
perltut
perltutperltut
perltut
 
perltut
perltutperltut
perltut
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
 

Mais de Dave Cross

Object-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and MooseObject-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and Moose
Dave Cross
 
Database Programming with Perl and DBIx::Class
Database Programming with Perl and DBIx::ClassDatabase Programming with Perl and DBIx::Class
Database Programming with Perl and DBIx::Class
Dave Cross
 

Mais de Dave Cross (20)

Measuring the Quality of Your Perl Code
Measuring the Quality of Your Perl CodeMeasuring the Quality of Your Perl Code
Measuring the Quality of Your Perl Code
 
Apollo 11 at 50 - A Simple Twitter Bot
Apollo 11 at 50 - A Simple Twitter BotApollo 11 at 50 - A Simple Twitter Bot
Apollo 11 at 50 - A Simple Twitter Bot
 
Monoliths, Balls of Mud and Silver Bullets
Monoliths, Balls of Mud and Silver BulletsMonoliths, Balls of Mud and Silver Bullets
Monoliths, Balls of Mud and Silver Bullets
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 
I'm A Republic (Honest!)
I'm A Republic (Honest!)I'm A Republic (Honest!)
I'm A Republic (Honest!)
 
Web Site Tune-Up - Improve Your Googlejuice
Web Site Tune-Up - Improve Your GooglejuiceWeb Site Tune-Up - Improve Your Googlejuice
Web Site Tune-Up - Improve Your Googlejuice
 
Modern Perl Web Development with Dancer
Modern Perl Web Development with DancerModern Perl Web Development with Dancer
Modern Perl Web Development with Dancer
 
Freeing Tower Bridge
Freeing Tower BridgeFreeing Tower Bridge
Freeing Tower Bridge
 
Modern Perl Catch-Up
Modern Perl Catch-UpModern Perl Catch-Up
Modern Perl Catch-Up
 
Error(s) Free Programming
Error(s) Free ProgrammingError(s) Free Programming
Error(s) Free Programming
 
Medium Perl
Medium PerlMedium Perl
Medium Perl
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
 
Conference Driven Publishing
Conference Driven PublishingConference Driven Publishing
Conference Driven Publishing
 
Conference Driven Publishing
Conference Driven PublishingConference Driven Publishing
Conference Driven Publishing
 
TwittElection
TwittElectionTwittElection
TwittElection
 
Perl in the Internet of Things
Perl in the Internet of ThingsPerl in the Internet of Things
Perl in the Internet of Things
 
Return to the Kingdom of the Blind
Return to the Kingdom of the BlindReturn to the Kingdom of the Blind
Return to the Kingdom of the Blind
 
Github, Travis-CI and Perl
Github, Travis-CI and PerlGithub, Travis-CI and Perl
Github, Travis-CI and Perl
 
Object-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and MooseObject-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and Moose
 
Database Programming with Perl and DBIx::Class
Database Programming with Perl and DBIx::ClassDatabase Programming with Perl and DBIx::Class
Database Programming with Perl and DBIx::Class
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Último (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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...
 
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...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
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
 

Beginning Perl