SlideShare uma empresa Scribd logo
1 de 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]];

Mais conteúdo relacionado

Mais procurados

Perl programming language
Perl programming languagePerl programming language
Perl programming language
Elie Obeid
 
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
 
New Elements & Features in CSS3
New Elements & Features in CSS3New Elements & Features in CSS3
New Elements & Features in CSS3
Jamshid Hashimi
 

Mais procurados (20)

Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
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
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Java script
Java scriptJava script
Java script
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
An introduction to SQLAlchemy
An introduction to SQLAlchemyAn introduction to SQLAlchemy
An introduction to SQLAlchemy
 
JavaScript
JavaScriptJavaScript
JavaScript
 
JDBC
JDBCJDBC
JDBC
 
Es6 to es5
Es6 to es5Es6 to es5
Es6 to es5
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
New Elements & Features in CSS3
New Elements & Features in CSS3New Elements & Features in CSS3
New Elements & Features in CSS3
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
CSS Selectors
CSS SelectorsCSS Selectors
CSS Selectors
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 

Destaque (7)

Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
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
 
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
 

Semelhante a Introduction to Perl - Day 2

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

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

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
 

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

Último

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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
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
 
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
 

Introduction to Perl - Day 2