Enviar búsqueda
Cargar
Introduction to Perl - Day 2
•
Descargar como ODP, PDF
•
30 recomendaciones
•
19,026 vistas
Dave Cross
Seguir
Tecnología
Educación
Denunciar
Compartir
Denunciar
Compartir
1 de 160
Descargar ahora
Recomendados
Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
Introduction To Catalyst - Part 1
Introduction To Catalyst - Part 1
Dan Dascalescu
LPW: Beginners Perl
LPW: Beginners Perl
Dave Cross
DBIx::Class beginners
DBIx::Class beginners
leo lapworth
Introduction to Perl
Introduction to Perl
Dave Cross
Control Structures In Php 2
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
Introducing Modern Perl
Introducing Modern Perl
Dave Cross
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
Ricardo Signes
Recomendados
Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
Introduction To Catalyst - Part 1
Introduction To Catalyst - Part 1
Dan Dascalescu
LPW: Beginners Perl
LPW: Beginners Perl
Dave Cross
DBIx::Class beginners
DBIx::Class beginners
leo lapworth
Introduction to Perl
Introduction to Perl
Dave Cross
Control Structures In Php 2
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
Introducing Modern Perl
Introducing Modern Perl
Dave Cross
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
Ricardo Signes
Perl tutorial
Perl tutorial
Manav Prasad
Introduction to PHP
Introduction to PHP
Kengatharaiyer Sarveswaran
jQuery
jQuery
Dileep Mishra
jQuery
jQuery
Vishwa Mohan
Clean code
Clean code
Arturo Herrero
jQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
1 03 - CSS Introduction
1 03 - CSS Introduction
apnwebdev
Introduction to php
Introduction to php
Anjan Banda
JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
Javascript
Javascript
guest03a6e6
Javascript operators
Javascript operators
Mohit Rana
HTML 5 Complete Reference
HTML 5 Complete Reference
EPAM Systems
Javascript Basic
Javascript Basic
Kang-min Liu
Php mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
Class 3 - PHP Functions
Class 3 - PHP Functions
Ahmed Swilam
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
Ismail Mukiibi
MYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
Css padding
Css padding
AbhishekMondal42
Javascript arrays
Javascript arrays
Hassan Dar
Php Lecture Notes
Php Lecture Notes
Santhiya Grace
Perl Introduction
Perl Introduction
Marcos Rebelo
Perl programming language
Perl programming language
Elie Obeid
Más contenido relacionado
La actualidad más candente
Perl tutorial
Perl tutorial
Manav Prasad
Introduction to PHP
Introduction to PHP
Kengatharaiyer Sarveswaran
jQuery
jQuery
Dileep Mishra
jQuery
jQuery
Vishwa Mohan
Clean code
Clean code
Arturo Herrero
jQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
1 03 - CSS Introduction
1 03 - CSS Introduction
apnwebdev
Introduction to php
Introduction to php
Anjan Banda
JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
Javascript
Javascript
guest03a6e6
Javascript operators
Javascript operators
Mohit Rana
HTML 5 Complete Reference
HTML 5 Complete Reference
EPAM Systems
Javascript Basic
Javascript Basic
Kang-min Liu
Php mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
Class 3 - PHP Functions
Class 3 - PHP Functions
Ahmed Swilam
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
Ismail Mukiibi
MYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
Css padding
Css padding
AbhishekMondal42
Javascript arrays
Javascript arrays
Hassan Dar
Php Lecture Notes
Php Lecture Notes
Santhiya Grace
La actualidad más candente
(20)
Perl tutorial
Perl tutorial
Introduction to PHP
Introduction to PHP
jQuery
jQuery
jQuery
jQuery
Clean code
Clean code
jQuery from the very beginning
jQuery from the very beginning
1 03 - CSS Introduction
1 03 - CSS Introduction
Introduction to php
Introduction to php
JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
Javascript
Javascript
Javascript operators
Javascript operators
HTML 5 Complete Reference
HTML 5 Complete Reference
Javascript Basic
Javascript Basic
Php mysql ppt
Php mysql ppt
Class 3 - PHP Functions
Class 3 - PHP Functions
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
MYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
Css padding
Css padding
Javascript arrays
Javascript arrays
Php Lecture Notes
Php Lecture Notes
Destacado
Perl Introduction
Perl Introduction
Marcos Rebelo
Perl programming language
Perl programming language
Elie Obeid
Perl Scripting
Perl Scripting
Varadharajan Mukundan
Perl hosting for beginners - Cluj.pm March 2013
Perl hosting for beginners - Cluj.pm March 2013
Arpad Szasz
Introduction to Web Programming with Perl
Introduction to Web Programming with Perl
Dave Cross
DBI Advanced Tutorial 2007
DBI Advanced Tutorial 2007
Tim Bunce
Database Programming with Perl and DBIx::Class
Database Programming with Perl and DBIx::Class
Dave Cross
Lagrange’s interpolation formula
Lagrange’s interpolation formula
Mukunda Madhav Changmai
Cookie and session
Cookie and session
Aashish Ghale
Destacado
(9)
Perl Introduction
Perl Introduction
Perl programming language
Perl programming language
Perl Scripting
Perl Scripting
Perl 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 Perl
DBI Advanced Tutorial 2007
DBI Advanced Tutorial 2007
Database Programming with Perl and DBIx::Class
Database Programming with Perl and DBIx::Class
Lagrange’s interpolation formula
Lagrange’s interpolation formula
Cookie and session
Cookie and session
Similar a Introduction to Perl - Day 2
Intermediate Perl
Intermediate Perl
Dave Cross
Beginning Perl
Beginning Perl
Dave Cross
Perl Presentation
Perl Presentation
Sopan Shewale
Introduction to Modern Perl
Introduction to Modern Perl
Dave Cross
Advanced Perl Techniques
Advanced Perl Techniques
Dave Cross
What's new, what's hot in PHP 5.3
What's new, what's hot in PHP 5.3
Jeremy Coates
Bioinformatica 10-11-2011-p6-bioperl
Bioinformatica 10-11-2011-p6-bioperl
Prof. Wim Van Criekinge
Php2
Php2
Keennary Pungyera
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
Cleancode
Cleancode
hendrikvb
Introduction to Perl
Introduction to Perl
worr1244
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
PHP
PHP
webhostingguy
Advanced Perl Techniques
Advanced Perl Techniques
Dave Cross
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng ver
Qrembiezs Intruder
Hashes Master
Hashes Master
Paolo Marcatili
PHP and Cassandra
PHP and Cassandra
Dave Gardner
Introduction to perl_lists
Introduction to perl_lists
Vamshi Santhapuri
Php Using Arrays
Php Using Arrays
mussawir20
Learning Perl 6
Learning Perl 6
brian d foy
Similar a Introduction to Perl - Day 2
(20)
Intermediate Perl
Intermediate Perl
Beginning Perl
Beginning Perl
Perl Presentation
Perl Presentation
Introduction to Modern Perl
Introduction to Modern Perl
Advanced Perl Techniques
Advanced Perl Techniques
What'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-bioperl
Php2
Php2
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Cleancode
Cleancode
Introduction 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 Needs
PHP
PHP
Advanced Perl Techniques
Advanced Perl Techniques
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng ver
Hashes Master
Hashes Master
PHP and Cassandra
PHP and Cassandra
Introduction to perl_lists
Introduction to perl_lists
Php Using Arrays
Php Using Arrays
Learning Perl 6
Learning Perl 6
Más de Dave Cross
Measuring the Quality of Your Perl Code
Measuring the Quality of Your Perl Code
Dave Cross
Apollo 11 at 50 - A Simple Twitter Bot
Apollo 11 at 50 - A Simple Twitter Bot
Dave Cross
Monoliths, Balls of Mud and Silver Bullets
Monoliths, Balls of Mud and Silver Bullets
Dave Cross
The Professional Programmer
The Professional Programmer
Dave Cross
I'm A Republic (Honest!)
I'm A Republic (Honest!)
Dave Cross
Web Site Tune-Up - Improve Your Googlejuice
Web Site Tune-Up - Improve Your Googlejuice
Dave Cross
Modern Perl Web Development with Dancer
Modern Perl Web Development with Dancer
Dave Cross
Freeing Tower Bridge
Freeing Tower Bridge
Dave Cross
Modern Perl Catch-Up
Modern Perl Catch-Up
Dave Cross
Error(s) Free Programming
Error(s) Free Programming
Dave Cross
Medium Perl
Medium Perl
Dave Cross
Modern Web Development with Perl
Modern Web Development with Perl
Dave Cross
Improving Dev Assistant
Improving Dev Assistant
Dave Cross
Conference Driven Publishing
Conference Driven Publishing
Dave Cross
Conference Driven Publishing
Conference Driven Publishing
Dave Cross
TwittElection
TwittElection
Dave Cross
Perl in the Internet of Things
Perl in the Internet of Things
Dave Cross
Return to the Kingdom of the Blind
Return to the Kingdom of the Blind
Dave Cross
Github, Travis-CI and Perl
Github, Travis-CI and Perl
Dave Cross
Object-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and Moose
Dave Cross
Más de Dave Cross
(20)
Measuring 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 Bot
Monoliths, Balls of Mud and Silver Bullets
Monoliths, Balls of Mud and Silver Bullets
The Professional Programmer
The Professional Programmer
I'm A Republic (Honest!)
I'm A Republic (Honest!)
Web Site Tune-Up - Improve Your Googlejuice
Web Site Tune-Up - Improve Your Googlejuice
Modern Perl Web Development with Dancer
Modern Perl Web Development with Dancer
Freeing Tower Bridge
Freeing Tower Bridge
Modern Perl Catch-Up
Modern Perl Catch-Up
Error(s) Free Programming
Error(s) Free Programming
Medium Perl
Medium Perl
Modern Web Development with Perl
Modern Web Development with Perl
Improving Dev Assistant
Improving Dev Assistant
Conference Driven Publishing
Conference Driven Publishing
Conference Driven Publishing
Conference Driven Publishing
TwittElection
TwittElection
Perl in the Internet of Things
Perl in the Internet of Things
Return to the Kingdom of the Blind
Return to the Kingdom of the Blind
Github, Travis-CI and Perl
Github, Travis-CI and Perl
Object-Oriented Programming with Perl and Moose
Object-Oriented Programming with Perl and Moose
Último
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
The Digital Insurer
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
Delhi Call girls
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
hans926745
presentation ICT roal in 21st century education
presentation ICT roal in 21st century education
jfdjdjcjdnsjd
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
wesley chun
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
debabhi2
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Anna Loughnan Colquhoun
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Enterprise Knowledge
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
Delhi Call girls
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Miguel Araújo
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
Gabriella Davis
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Martijn de Jong
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
sammart93
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
sudhanshuwaghmare1
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
The Digital Insurer
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Delhi Call girls
Último
(20)
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
presentation ICT roal in 21st century education
presentation ICT roal in 21st century education
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Introduction to Perl - Day 2
1.
Introduction to Perl
Day 2 An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
2.
3.
Strict and warnings
4.
References
5.
Sorting
6.
Reusable Code
7.
Object Orientation
8.
9.
Dates and Times
10.
Templates
11.
Databases
12.
Further Information
13.
14.
11:00 – Coffee
break (30 mins)
15.
13:00 – Lunch
(90 mins)
16.
14:30 – Begin
17.
16:00 – Coffee
break (30 mins)
18.
18:00 – End
19.
20.
Types of Variable
21.
22.
Important to know
the difference
23.
Lexical variables are
created with my
24.
Package variables are
created by our
25.
Lexical variables are
associated with a code block
26.
Package variables are
associated with a package
27.
28.
my ($doctor, @timelords,
%home_planets);
29.
30.
31.
"Lexical" because the
scope is defined purely by the text
32.
33.
34.
Used to avoid
name clashes with libraries
35.
Default package is
called main
36.
37.
38.
39.
Can be seen
from anywhere in the package (or anywhere at all when fully qualified)
40.
41.
our ($doctor, @timelords,
%home_planet);
42.
Or (in older
Perls) with use vars
43.
use vars qw($doctor
@timelords %home_planet);
44.
45.
46.
47.
48.
local $variable;
49.
This doesn't do
what you think it does
50.
Badly named function
51.
Doesn't create local
variables
52.
Creates a local
copy of a package variable
53.
54.
55.
It defines the
record separator
56.
You might want
to change it
57.
Always localise changes
58.
{
local $/ = “”; while (<FILE> ) { ... } }
59.
Strict and Warnings
60.
61.
Two features can
minimise the dangers
62.
use strict
/ use warnings
63.
A good habit
to get into
64.
No serious Perl
programmer codes without them
65.
66.
use strict 'refs'
– no symbolic references
67.
use strict 'subs'
– no barewords
68.
use strict 'vars'
– no undeclared variables
69.
use strict
– turn on all three at once
70.
turn them off
(carefully) with no strict
71.
72.
aka "using a
variable as another variable's name"
73.
$what = 'dalek';
$$what = 'Karn'; # sets $dalek to 'Karn'
74.
What if 'dalek'
came from user input?
75.
People often think
this is a cool feature
76.
It isn't
77.
78.
$what = 'dalek';
$alien{$what} = 'Karn';
79.
Self contained namespace
80.
Less chance of
clashes
81.
More information (e.g.
all keys)
82.
83.
Bareword is a
word with no other interpretation
84.
e.g. word without
$, @, %, &
85.
Treated as a
function call or a quoted string
86.
$dalek = Karn;
87.
May clash with
future reserved words
88.
89.
Prevents typos
90.
Less like BASIC
- more like Ada
91.
Thinking about scope
is good
92.
93.
94.
Using undefined variables
95.
Writing to read-only
file handles
96.
And many more...
97.
98.
Turn off use
warnings locally
99.
Turn off specific
warnings
100.
{
no warnings 'deprecated'; # dodgy code ... }
101.
See perldoc perllexwarn
102.
References
103.
104.
A reference is
a unique way to refer to a variable.
105.
A reference can
always fit into a scalar variable
106.
A reference looks
like SCALAR(0x20026730)
107.
108.
$array_ref = array;
109.
110.
$refs[0] = $array_ref;
111.
$another_ref = $refs[0];
112.
113.
$aref = [
'this', 'is', 'a', 'list']; $aref2 = [ @array ];
114.
{ LIST }
creates anonymous hash and returns a reference
115.
$href = {
1 => 'one', 2 => 'two' }; $href = { %hash };
116.
117.
Output ARRAY(0x20026800) ARRAY(0x2002bc00)
118.
Second method creates
a copy of the array
119.
120.
Whole array
121.
@array = @{$aref};
122.
@rev = reverse
@{$aref};
123.
Single elements
124.
$elem = ${$aref}[0];
125.
${$aref}[0] = 'foo';
126.
127.
Whole hash
128.
%hash = %{$href};
129.
@keys = keys
%{$href};
130.
Single elements
131.
$elem = ${$href}{key};
132.
${$href}{key} = 'foo';
133.
134.
Instead of
${$aref}[0] you can use $aref->[0]
135.
Instead of
${$href}{key} you can use $href->{key}
136.
137.
$aref = [
1, 2, 3 ]; print ref $aref; # prints ARRAY
138.
$href = {
1 => 'one', 2 => 'two' }; print ref $href; # prints HASH
139.
140.
Complex data structures
141.
142.
@arr1 = (1,
2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size { my (@a1, @a2) = @_; print @a1 == @a2 ? 'Yes' : 'No'; }
143.
144.
Arrays are combined
in @_
145.
All elements end
up in @a1
146.
How do we
fix it?
147.
Pass references to
the arrays
148.
149.
150.
Try to create
a 2-D array
151.
@arr_2d = ((1,
2, 3), (4, 5, 6), (7, 8, 9));
152.
@arr_2d contains (1,
2, 3, 4, 5, 6, 7, 8, 9)
153.
This is known
a array flattening
154.
155.
@arr_2d = ([1,
2, 3], [4, 5, 6], [7, 8, 9]);
156.
But how do
you access individual elements?
157.
$arr_2d[1] is
ref to array (4, 5, 6)
158.
$arr_2d[1]->[1] is
element 5
159.
160.
$arr_2d = [[1,
2, 3], [4, 5, 6], [7, 8, 9]];
161.
$arr_2d->[1] is
ref to array (4, 5, 6)
162.
$arr_2d->[1]->[1] is
element 5
163.
Can omit intermediate
arrows
164.
$arr_2d->[1][1]
165.
166.
Jones,Martha,UNIT Harkness,Jack,Torchwood Smith,Sarah
Jane,Journalist
167.
What would be
a good data structure?
168.
Hash for each
record
169.
Array of records
170.
Array of hashes
171.
172.
my @records; my
@cols = ('s_name', 'f_name', 'job'); while (<FILE>) { chomp; my %rec; @rec{@cols} = split /,/; push @records, rec; }
173.
Using an Array
of Hashes foreach (@records) { print "$_->{f_name} ", "$_->{s_name} ". "is a $_->{job}"; }
174.
175.
Hash of lists
176.
177.
Sorting
178.
179.
@sorted = sort
@array;
180.
Note that it
does not sort the list in place
181.
@array = sort
@array;
182.
183.
@chars = sort
'e', 'b', 'a', 'd', 'c'; # @chars has ('a', 'b', 'c', 'd', 'e')
184.
This can sometimes
give strange results
185.
@chars = sort
'E', 'b', 'a', 'D', 'c'; # @chars has ('D', 'E', 'a', 'b', 'c')
186.
@nums = sort
1 .. 10; # @nums has (1, 10, 2, 3, 4, # 5, 6, 7, 8, 9)
187.
188.
@nums =
sort { $a <=> $b } 1 .. 10;
189.
Perl puts two
of the values from the list into $a and $b
190.
Block compares values
and returns -1, 0 or 1
191.
<=> does
this for numbers ( cmp for strings)
192.
193.
sort { $b
cmp $a } @words
194.
sort { lc
$a cmp lc $b } @words
195.
sort { substr($a,
4) cmp substr($b, 4) } @lines
196.
197.
@words = sort
dictionary @words; sub dictionary { # Don't change $a and $b my ($A, $B) = ($a, $b); $A =~ s/+//g; $B =~ s/+//g; $A cmp $B; }
198.
199.
Need to write
sort_names so that it sorts on surname and then forename.
200.
201.
202.
Can be inefficient
on large amounts of data
203.
Multiple splits on
the same data
204.
205.
@split = map
{ [ split ] } @names;
206.
Do the comparison
207.
@sort = sort
{ $a->[1] cmp $b->[1] or $a->[0] cmp $b->[0] } @split;
208.
Join the data
together
209.
@names = map
{ join ' ', @$_ } @sort;
210.
211.
@names = map
{ join ' ', @$_ } sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } map { [ split ] } @names;
212.
All functions work
on the output from the previous function in the chain
213.
214.
Old Lisp trick
215.
Named after Randal
Schwartz
216.
Reusable Code
217.
218.
Prevent reinventing the
wheel
219.
Easier to share
across projects
220.
Better design, more
generic
221.
222.
223.
224.
225.
226.
The default package
is main
227.
New packages are
introduced with the package keyword
228.
A subroutine's full
name is package::name
229.
Package name can
be omitted from within same package
230.
Like family names
231.
232.
Exporter.pm defines a
subroutine called import
233.
import is
automatically called whenever a module is used
234.
import puts
references to our subroutines into our caller's symbol table
235.
236.
We make use
of inheritance
237.
Inheritance is defined
using the @ISA array
238.
If we call
a subroutine that doesn't exist in our module, then the modules in @ISA are also checked
239.
Therefore Exporter::import
is called
240.
241.
Exports are defined
in @EXPORT or @EXPORT_OK
242.
Automatic exports are
defined in @EXPORT
243.
Optional exports are
defined in @EXPORT_OK
244.
245.
Key is set
name
246.
Value is reference
to an array of names
247.
our %EXPORT_TAGS =
(advanced => [ qw( my_sub my_other_sub ) ]; use MyModule qw(:advanced); my_sub(); my_other_sub();
248.
249.
Less chances of
name clashes
250.
Use @EXPORT_OK
in preference to @EXPORT
251.
Document the exported
names and sets
252.
253.
@EXPORT_OK = qw($scalar,
@array, %hash);
254.
Can be part
of export sets
255.
Any variables you
export must be package variables
256.
257.
Don't write the
boilerplate yourself
258.
Copy from an
existing module
259.
Or look at
Module::Starter
260.
Object Orientation
261.
262.
OO inverts this
263.
Classes contain methods
which define their actions
264.
Objects are instances
of classes
265.
Perl has an
OO system bolted on
266.
Best of both
worlds
267.
268.
269.
An Object is
a reference (usually to a hash)
270.
271.
272.
273.
274.
275.
Moose is on
CPAN
276.
Based on Perl
6 OO
277.
Well worth investigating
278.
279.
1;
280.
281.
perldoc perltoot
282.
perldoc perlobj
283.
perldoc perlbot
284.
perldoc Moose (if
it is installed)
285.
Object Oriented Perl
(Conway)
286.
Testing
287.
288.
Does your code
do what it is supposed to do?
289.
Will your code
continue to do what it is supposed to do?
290.
Write unit tests
291.
Run those tests
all the time
292.
293.
Before you add
a feature
294.
After you have
added a feature
295.
Before checking in
code
296.
Before releasing code
297.
Constantly, automaticall y
298.
299.
A lot of
work in this area over the last eight years
300.
Test::Simple and Test::More
included in Perl distribution
301.
Many more testing
modules on CPAN
302.
303.
304.
305.
306.
307.
Runs given tests
using Test::Harness
308.
Comes with the
Perl distribution
309.
310.
-r recurse
311.
-s shuffle tests
312.
Many more
313.
314.
Now this ad-hoc
format has a definition and a name
315.
The Test Anything
Protocol (TAP)
316.
See Test::Harness::TAP (documentation
module) and TAP::Parser
317.
318.
319.
320.
Some of my
favourites
321.
Test::File
322.
Test::Exception, Test::Warn
323.
Test::Differences
324.
Test::XML (includes Test::XML::XPath)
325.
326.
Built using Test::Builder
327.
Ensures that test
modules all use the same framework
328.
Use it as
the basis of your own Test::* modules
329.
Who tests the
testers?
330.
Test your Test::Builder
test modules with Test::Builder::Tester
331.
332.
Devel::Cover can help
you to find out
333.
Deep internal magic
334.
335.
cover
336.
Devel::Cover Output
337.
Devel::Cover Output
338.
Devel::Cover Output
339.
340.
Other frameworks are
available
341.
342.
http://fit.c2.com
343.
344.
perldoc Test::Tutorial
345.
perldoc Test::Simple
346.
perldoc Test::More
347.
perldoc Test::Builder
348.
etc...
349.
Dates and Times
350.
351.
Flexible but confusing
352.
time –
seconds since 1st Jan 1970
353.
localtime –
convert to human-readable
354.
timelocal (in
Time::Local) – inverse of localtime
355.
strftime (in
POSIX) – formatting dates and times
356.
357.
Dozens of date/time
modules on CPAN
358.
Date::Manip is almost
never what you want
359.
Date::Calc, Date::Parse, Class::Date,
Date::Simple, etc
360.
Which one do
you choose?
361.
362.
"The DateTime family
of modules present a unified way to handle dates and times in Perl"
363.
"unified" is good
364.
Dozens of modules
that work together in a consistent fashion
365.
366.
367.
368.
For date arithmetic
you need a duration
369.
Number of years,
weeks, days, etc
370.
371.
372.
Control input format
with DateTime::Format::Strptime
373.
374.
DateTime::Format::HTTP
375.
DateTime::Format::MySQL
376.
DateTime::Format::Excel
377.
378.
379.
DateTime::Calendar::Julian
380.
DateTime::Calendar::Hebrew
381.
DateTime::Calendar::Mayan
382.
DateTime::Fiction::JRRTolkien::Shire
383.
384.
use DateTime::Fiction::JRRTolkien::Shire my
$dt = DateTime::Fiction::JRRTolkien::Shire->now; say $dt->on_date; # Trewsday 1 Wedmath 7473
385.
Templates
386.
387.
Advantages are well
known
388.
Standard look and
feel (static/dynamic)
389.
Reusable components
390.
Separation of code
logic from display logic
391.
Different skill-sets (HTML
vs Perl)
392.
393.
Reports
394.
Business documents
395.
Configuration files
396.
Anywhere you produce
output
397.
398.
See perlfaq4
399.
“How can I
expand variables in text strings?”
400.
401.
Don't do that
402.
403.
Text::Template, HTML::Template, Mason,
Template Toolkit
404.
Many, many more
405.
406.
407.
408.
Very powerful
409.
Both web and
non-web
410.
Simple template language
411.
Plugins give access
to much of CPAN
412.
413.
Good Book Too!
414.
415.
Data + Alternative
Template = Alternative Output
416.
Different views of
the same data
417.
Only the template
changes
418.
419.
420.
421.
422.
Just add new
output template
423.
Perl programmer need
not be involved
424.
425.
Databases
426.
427.
Perl has tools
to make this as simple as possible
428.
DBI is the
basis for all modern Perl database access
429.
430.
431.
Create a connection
to a particular type of database
432.
DBD module gets
loaded
433.
DBD translates from
DBI API to database specific calls
434.
DBD translates returned
data into Perl data stuctures
435.
436.
437.
Just change the
connection line
438.
439.
my $sth =
$dbh->prepare( 'select name, genre from artist' );
440.
my $sth =
$dbh->prepare( "select title, from song where artist = '$id'");
441.
Check return values
(syntax errors)
442.
443.
$sth->execute
444.
Still need to
check for errors
445.
446.
while (my @row
= $sth->fetchrow_array){ print "@row"; }
447.
Fields are returned
in the same order as they are defined in the query
448.
449.
fetchrow_hashref (keys
are column names)
450.
fetchall_arrayref
451.
452.
453.
454.
For (hopefully) obvious
reasons
455.
456.
my $sql =
"update table1 set col1 = '$val' where id_col = $id"; my $sth = $dbh->prepare($sql); $sth->execute;
457.
But there's a
shortcut
458.
$rows = $dbh->do($sql);
459.
460.
Recompiles the SQL
every time
461.
Very inefficient
462.
463.
my $sql =
"insert into tab values (?, ?, ?)"; my $sth = $dbh->prepare($sql); while (<FILE>) { my @data = split; bind_param(1, $data[0]); bind_param(2, $data[1]); bind_param(3, $data[2]); $sth->execute; }
464.
Bonus - binding
handles quoting for you
465.
466.
my $sql =
"insert into tab values (?, ?, ?)"; my $sth = $dbh->prepare($sql); while (<FILE>) { chomp; my @data = split; $sth->execute(@data); }
467.
468.
my $sql =
'insert into big_table values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
469.
Good chance of
getting the variables in the wrong order
470.
By the way
- there's a basic maintainability error in that SQL
471.
472.
473.
my %data =
(id => 42, code => 'H2G2', ... ); # and later... foreach my $col (keys %data) { $sth->bind_param(":$col", $data{$col}; }
474.
475.
Which is a
bit of a bugger
476.
Oracle does
477.
So does PostgreSQL
(tho' the docs discourage its use)
478.
Check your DBD
documentation
479.
Email your friendly
neighbourhood DBD author
480.
481.
482.
Store SQL queries
externally and reference them by name
483.
Use named bind
parameters if you can
484.
485.
486.
487.
Select all the
details of this row
488.
Select something about
related tables
489.
Update this row
with these values
490.
Insert a new
record with these values
491.
492.
493.
Tables (relations) map
onto classes
494.
Rows (tuples) map
onto objects
495.
Columns (attributes) map
onto attributes
496.
Don't write SQL
497.
498.
SELECT * FROM
my_table WHERE my_id = 10
499.
and then dealing
with the prepare/execute/fetch code
500.
501.
use My::Object; #
warning! not a real orm my $obj = My::Object->retrieve(10) $obj->name('A New Name'); $obj->save;
502.
Or something similar
503.
504.
Tangram
505.
Alzabo
506.
Class::DBI
507.
508.
Highly recommended
509.
510.
511.
DBD::Oracle
512.
513.
Further Information
514.
515.
516.
http://london.pm.org
517.
518.
519.
520.
http://www.yapceurope.org
521.
522.
Brazil
523.
524.
Australia
525.
526.
UK, Germany, Israel,
Pittsburgh, Nordic, Netherlands, France, Belgium, Russia, Minnesota, Austria
527.
528.
529.
CGI
530.
DBI
531.
XML
532.
Beginners
533.
Advocacy
534.
Fun with Perl
535.
536.
Intermediate Perl Schwartz,
foy & Phoenix (O'Reilly)
537.
Beginning Perl Cozens
(Wrox) http://www.perl.org/books/beginning-perl/
538.
539.
The Perl Cookbook
(2nd edition) Christiansen & Torkington (O'Reilly)
540.
Perl Best Practices
Conway (O'Reilly)
541.
Perl in a
Nutshell Siever, Spainhour & Patwardhan (O'Reilly)
542.
543.
Data Munging with
Perl Cross (Manning)
544.
Advanced Perl Programming
Cozens (O'Reilly)
545.
Perl Medic Scott
(Addison Wesley)
546.
547.
Programming the Perl
DBI Descartes & Bunce (O'Reilly)
548.
Writing CGI Applications
with Perl Meltzer & Michelski (Addison Wesley)
549.
Practical mod_perl Bekman
& Cholet (O'Reilly)
550.
Perl Template Toolkit
Chamberlain, Cross & Wardley
551.
552.
Descargar ahora