SlideShare une entreprise Scribd logo
1  sur  160
Introduction to Perl Day 2 An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
What We Will Cover ,[object Object]
Strict and warnings
References
Sorting
Reusable Code
Object Orientation
What We Will Cover ,[object Object]
Dates and Times
Templates
Databases
Further Information
Schedule ,[object Object]
11:00 – Coffee break (30 mins)
13:00 – Lunch (90 mins)
14:30 – Begin
16:00 – Coffee break (30 mins)
18:00 – End
Resources ,[object Object]
Types of Variable
Types of Variable ,[object Object]
Important to know the difference
Lexical variables are created with  my
Package variables are created by  our
Lexical variables are associated with a code block
Package variables are associated with a package
Lexical Variables ,[object Object]
my ($doctor, @timelords,   %home_planets);
Live in a pad (associated with a block of code) ,[object Object]
Source file ,[object Object]
"Lexical" because the scope is defined purely by the text
Packages ,[object Object]
A new package is created with package ,[object Object],[object Object]
Used to avoid name clashes with libraries
Default package is called main
Package Variables ,[object Object]
Can be referred to using a fully qualified name ,[object Object]
@Gallifrey::timelords ,[object Object]
Can be seen from anywhere in the package (or anywhere at all when fully qualified)
Declaring Package Vars ,[object Object]
our ($doctor, @timelords,   %home_planet);
Or (in older Perls) with  use vars
use vars qw($doctor   @timelords   %home_planet);
Lexical or Package ,[object Object]
Simple answer ,[object Object],[object Object],[object Object]
Except for a tiny number of cases ,[object Object]
local ,[object Object]
local $variable;
This doesn't do what you think it does
Badly named function
Doesn't create local variables
Creates a local copy of a package variable
Can be useful ,[object Object]
local Example ,[object Object]
It defines the record separator
You might want to change it
Always localise changes
{   local $/ = “”;   while (<FILE> ) {   ...   } }
Strict and Warnings
Coding Safety Net ,[object Object]
Two features can minimise the dangers
use strict  /  use warnings
A good habit to get into
No serious Perl programmer codes without them
use strict ,[object Object]
use strict 'refs'  – no symbolic references
use strict 'subs'  – no barewords
use strict 'vars'  – no undeclared variables
use strict  – turn on all three at once
turn them off (carefully) with  no strict
use strict 'refs' ,[object Object]
aka &quot;using a variable as another variable's name&quot;
$what = 'dalek'; $$what = 'Karn'; # sets $dalek to 'Karn'
What if 'dalek' came from user input?
People often think this is a cool feature
It isn't
use strict 'refs' (cont) ,[object Object]
$what = 'dalek'; $alien{$what} = 'Karn';
Self contained namespace
Less chance of clashes
More information (e.g. all keys)
use strict 'subs' ,[object Object]
Bareword is a word with no other interpretation
e.g. word without $, @, %, &
Treated as a function call or a quoted string
$dalek = Karn;
May clash with future reserved words
use strict 'vars' ,[object Object]
Prevents typos
Less like BASIC - more like Ada
Thinking about scope is good
use warnings ,[object Object]
Some typical warnings ,[object Object]
Using undefined variables
Writing to read-only file handles
And many more...
Allowing Warnings ,[object Object]
Turn off use warnings locally
Turn off specific warnings
{   no warnings 'deprecated';   # dodgy code ... }
See perldoc perllexwarn
References
Introducing References ,[object Object]
A reference is a unique way to refer to a variable.
A reference can always fit into a scalar variable
A reference looks like  SCALAR(0x20026730)
Creating References ,[object Object]
$array_ref = array;
$hash_ref = hash; ,[object Object],[object Object]
$refs[0] = $array_ref;
$another_ref = $refs[0];
Creating References ,[object Object]
$aref = [ 'this', 'is', 'a', 'list']; $aref2 = [ @array ];
{ LIST }  creates anonymous hash and returns a reference
$href = { 1 => 'one', 2 => 'two' }; $href = { %hash };
Creating References ,[object Object]
Output ARRAY(0x20026800) ARRAY(0x2002bc00)
Second method creates a  copy  of the array
Using Array References ,[object Object]
Whole array
@array = @{$aref};
@rev = reverse @{$aref};
Single elements
$elem = ${$aref}[0];
${$aref}[0] = 'foo';
Using Hash References ,[object Object]
Whole hash
%hash = %{$href};
@keys = keys %{$href};
Single elements
$elem = ${$href}{key};
${$href}{key} = 'foo';
Using References ,[object Object]
Instead of  ${$aref}[0]  you can use $aref->[0]
Instead of  ${$href}{key}  you can use $href->{key}
Using References ,[object Object]
$aref = [ 1, 2, 3 ]; print ref $aref; # prints ARRAY
$href = { 1 => 'one',   2 => 'two' }; print ref $href; # prints HASH
Why Use References? ,[object Object]
Complex data structures
Parameter Passing ,[object Object]
@arr1 = (1, 2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size {   my (@a1, @a2) = @_;   print @a1 == @a2 ?   'Yes' : 'No'; }
Why Doesn't It Work? ,[object Object]
Arrays are combined in  @_
All elements end up in  @a1
How do we fix it?
Pass references to the arrays
Another Attempt ,[object Object]
Complex Data Structures ,[object Object]
Try to create a 2-D array
@arr_2d = ((1, 2, 3),   (4, 5, 6),   (7, 8, 9));
@arr_2d contains (1, 2, 3, 4, 5, 6, 7, 8, 9)
This is known a  array flattening
Complex Data Structures ,[object Object]
@arr_2d = ([1, 2, 3],   [4, 5, 6],   [7, 8, 9]);
But how do you access individual elements?
$arr_2d[1]  is ref to array (4, 5, 6)
$arr_2d[1]->[1]  is element 5
Complex Data Structures ,[object Object]
$arr_2d = [[1, 2, 3],   [4, 5, 6],   [7, 8, 9]];

Contenu connexe

Tendances

Introducing Modern Perl
Introducing Modern PerlIntroducing Modern Perl
Introducing Modern Perl
Dave Cross
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handling
mussawir20
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
 
PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2
Kumar
 

Tendances (20)

Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
Introducing Modern Perl
Introducing Modern PerlIntroducing Modern Perl
Introducing Modern Perl
 
Php array
Php arrayPhp array
Php array
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Php basics
Php basicsPhp basics
Php basics
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handling
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Subroutines in perl
Subroutines in perlSubroutines in perl
Subroutines in perl
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2
 

En vedette (7)

Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Perl hosting for beginners - Cluj.pm March 2013
Perl hosting for beginners - Cluj.pm March 2013Perl hosting for beginners - Cluj.pm March 2013
Perl hosting for beginners - Cluj.pm March 2013
 
Introduction to Web Programming with Perl
Introduction to Web Programming with PerlIntroduction to Web Programming with Perl
Introduction to Web Programming with Perl
 
DBI Advanced Tutorial 2007
DBI Advanced Tutorial 2007DBI Advanced Tutorial 2007
DBI Advanced Tutorial 2007
 
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
 
Lagrange’s interpolation formula
Lagrange’s interpolation formulaLagrange’s interpolation formula
Lagrange’s interpolation formula
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 

Similaire à Introduction to Perl - Day 2

Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 

Similaire à Introduction to Perl - Day 2 (20)

Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Introduction to Modern Perl
Introduction to Modern PerlIntroduction to Modern Perl
Introduction to Modern Perl
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3
 
Bioinformatica 10-11-2011-p6-bioperl
Bioinformatica 10-11-2011-p6-bioperlBioinformatica 10-11-2011-p6-bioperl
Bioinformatica 10-11-2011-p6-bioperl
 
Php2
Php2Php2
Php2
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Cleancode
CleancodeCleancode
Cleancode
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
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
 
PHP
PHP PHP
PHP
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng verTutorial perl programming basic eng ver
Tutorial perl programming basic eng ver
 
Hashes Master
Hashes MasterHashes Master
Hashes Master
 
PHP and Cassandra
PHP and CassandraPHP and Cassandra
PHP and Cassandra
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
 

Plus 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
 

Plus 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
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
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
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
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
 
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...
 
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
 
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...
 
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
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Introduction to Perl - Day 2