Home
Explore
Submit Search
Upload
Login
Signup
Advertisement
Introduction to Perl - Day 2
Report
Dave Cross
Follow
Owner / Senior Developer at Magnum Solutions Ltd
Aug. 3, 2009
•
0 likes
30 likes
×
Be the first to like this
Show More
•
18,689 views
views
×
Total views
0
On Slideshare
0
From embeds
0
Number of embeds
0
Check these out next
Introduction to PHP
Kengatharaiyer Sarveswaran
Perl programming language
Elie Obeid
Php mysql ppt
Karmatechnologies Pvt. Ltd.
Php and MySQL
Tiji Thomas
Ruby Programming Language - Introduction
Kwangshin Oh
Scalar expressions and control structures in perl
sana mateen
Regular expression in javascript
Toan Nguyen
Php Tutorial
pratik tambekar
1
of
160
Top clipped slide
Introduction to Perl - Day 2
Aug. 3, 2009
•
0 likes
30 likes
×
Be the first to like this
Show More
•
18,689 views
views
×
Total views
0
On Slideshare
0
From embeds
0
Number of embeds
0
Download Now
Download to read offline
Report
Technology
Education
Dave Cross
Follow
Owner / Senior Developer at Magnum Solutions Ltd
Advertisement
Advertisement
Advertisement
Recommended
Introduction to Perl - Day 1
Dave Cross
57.6K views
•
141 slides
DBIx::Class beginners
leo lapworth
25.8K views
•
95 slides
Introduction To Catalyst - Part 1
Dan Dascalescu
11.9K views
•
10 slides
LPW: Beginners Perl
Dave Cross
43.1K views
•
120 slides
Introduction to Perl
Dave Cross
3.2K views
•
152 slides
Advanced Perl Techniques
Dave Cross
7.7K views
•
182 slides
More Related Content
Slideshows for you
(20)
Introduction to PHP
Kengatharaiyer Sarveswaran
•
3.8K views
Perl programming language
Elie Obeid
•
5.2K views
Php mysql ppt
Karmatechnologies Pvt. Ltd.
•
69.3K views
Php and MySQL
Tiji Thomas
•
29.1K views
Ruby Programming Language - Introduction
Kwangshin Oh
•
9.6K views
Scalar expressions and control structures in perl
sana mateen
•
2.5K views
Regular expression in javascript
Toan Nguyen
•
2.8K views
Php Tutorial
pratik tambekar
•
3.6K views
Perl Scripting
Varadharajan Mukundan
•
9.9K views
Php Presentation
Manish Bothra
•
93.1K views
Javascript
guest03a6e6
•
19.9K views
Sql
Priyank Tewari
•
1.3K views
Unix commands reference
Pavan Kumar
•
12.2K views
Java script final presentation
Adhoura Academy
•
3.8K views
Array,lists and hashes in perl
sana mateen
•
1.8K views
PHP Powerpoint -- Teach PHP with this
Ian Macali
•
54K views
Php.ppt
Nidhi mishra
•
17.7K views
Hash map
Emmanuel Fuchs
•
2.1K views
Javascript
Manav Prasad
•
519 views
Php
Shyam Khant
•
4.8K views
Viewers also liked
(7)
Perl Introduction
Marcos Rebelo
•
19.1K views
Perl hosting for beginners - Cluj.pm March 2013
Arpad Szasz
•
7.3K views
Introduction to Web Programming with Perl
Dave Cross
•
25.4K views
DBI Advanced Tutorial 2007
Tim Bunce
•
21.4K views
Database Programming with Perl and DBIx::Class
Dave Cross
•
14.6K views
Lagrange’s interpolation formula
Mukunda Madhav Changmai
•
9.2K views
Cookie and session
Aashish Ghale
•
9K views
Advertisement
Similar to Introduction to Perl - Day 2
(20)
Intermediate Perl
Dave Cross
•
1.7K views
Beginning Perl
Dave Cross
•
3.9K views
Perl Presentation
Sopan Shewale
•
4.9K views
Introduction to Modern Perl
Dave Cross
•
18K views
What's new, what's hot in PHP 5.3
Jeremy Coates
•
9.6K views
Bioinformatica 10-11-2011-p6-bioperl
Prof. Wim Van Criekinge
•
867 views
Php2
Keennary Pungyera
•
745 views
Cleancode
hendrikvb
•
856 views
Introduction to Perl
worr1244
•
565 views
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
•
1K views
PHP
webhostingguy
•
1.8K views
Advanced Perl Techniques
Dave Cross
•
7.2K views
Tutorial perl programming basic eng ver
Qrembiezs Intruder
•
676 views
Hashes Master
Paolo Marcatili
•
330 views
PHP and Cassandra
Dave Gardner
•
15.7K views
Introduction to perl_lists
Vamshi Santhapuri
•
237 views
Php Using Arrays
mussawir20
•
16.3K views
Learning Perl 6
brian d foy
•
1.9K views
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
•
1.5K views
Drupal Lightning FAPI Jumpstart
guestfd47e4c7
•
3.6K views
More from Dave Cross
(20)
Measuring the Quality of Your Perl Code
Dave Cross
•
865 views
Apollo 11 at 50 - A Simple Twitter Bot
Dave Cross
•
594 views
Monoliths, Balls of Mud and Silver Bullets
Dave Cross
•
582 views
The Professional Programmer
Dave Cross
•
478 views
I'm A Republic (Honest!)
Dave Cross
•
650 views
Web Site Tune-Up - Improve Your Googlejuice
Dave Cross
•
1.1K views
Modern Perl Web Development with Dancer
Dave Cross
•
2.4K views
Freeing Tower Bridge
Dave Cross
•
685 views
Modern Perl Catch-Up
Dave Cross
•
2.3K views
Error(s) Free Programming
Dave Cross
•
4.4K views
Medium Perl
Dave Cross
•
4.7K views
Modern Web Development with Perl
Dave Cross
•
15.8K views
Improving Dev Assistant
Dave Cross
•
10K views
Conference Driven Publishing
Dave Cross
•
10.1K views
Conference Driven Publishing
Dave Cross
•
3.2K views
TwittElection
Dave Cross
•
4.5K views
Perl in the Internet of Things
Dave Cross
•
4.6K views
Return to the Kingdom of the Blind
Dave Cross
•
8.8K views
Github, Travis-CI and Perl
Dave Cross
•
9.9K views
Object-Oriented Programming with Perl and Moose
Dave Cross
•
10.6K views
Advertisement
Recently uploaded
(20)
Responsive Web Design Crafting Websites for the Multi-Device World (2).pdf
iSQUARE Business Solution
•
0 views
Howard Wilner Explains the Impact of 5G on Automotive Manufacturers
jimcarns
•
0 views
Hybrid Mobile App Development Frameworks.pdf
TarunTiwari94
•
0 views
GD Water Consult.pdf
AmitMehta214
•
0 views
KMM - Kanban Maturity Model
Adail Viana Neto
•
0 views
Exploratory Data Analysis - A Comprehensive Guide to EDA.pdf
StephenAmell4
•
0 views
Managing a WordPress Multisite Network
Jonathan Bossenger
•
0 views
Varanasi_Meetup_Universal API Managment.pdf
Santosh Ojha
•
0 views
英国格鲁斯特大学毕业证文凭成绩单制作指南
nahej99297
•
0 views
2023 Global Privacy Benchmarks Survey - Webinar May 30 2023.pdf
TrustArc
•
0 views
在哪里可以办美国大学文凭《夏威夷太平洋大学毕业证成绩单仿制》
efagvah
•
0 views
Secure all things with CBSecurity 3
Ortus Solutions, Corp
•
0 views
2023-05-31_ESWC.pptx
Anisa Rula
•
0 views
Agile Mindset, Ahmed Sidky PhD.pdf
FarizGhozali
•
0 views
AI HELPS PARALYSED MAN TO WALK NATURALLY.pdf
sudhakargeruganti
•
0 views
ChatGPT_Prompts.pptx
Chakrit Phain
•
0 views
澳洲查尔斯达尔文大学毕业证文凭成绩单制作指南
nahej99297
•
0 views
BIS Research conducted a webinar on Carbon Neutral Data Center Practices
BIS Research Inc.
•
0 views
End to End Process Transformation with Signavio.pdf
IgnacioPeredoCL
•
0 views
Ericsson LTE Commands.pdf
MbBot
•
0 views
Introduction to Perl - Day 2
Introduction to Perl
Day 2 An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
Strict and warnings
References
Sorting
Reusable Code
Object Orientation
Dates and Times
Templates
Databases
Further Information
11:00 – Coffee
break (30 mins)
13:00 – Lunch
(90 mins)
14:30 – Begin
16:00 – Coffee
break (30 mins)
18:00 – End
Types of Variable
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
my ($doctor, @timelords,
%home_planets);
"Lexical" because the
scope is defined purely by the text
Used to avoid
name clashes with libraries
Default package is
called main
Can be seen
from anywhere in the package (or anywhere at all when fully qualified)
our ($doctor, @timelords,
%home_planet);
Or (in older
Perls) with use vars
use vars qw($doctor
@timelords %home_planet);
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
It defines the
record separator
You might want
to change it
Always localise changes
{
local $/ = “”; while (<FILE> ) { ... } }
Strict and Warnings
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 '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
aka "using a
variable as another variable's name"
$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
$what = 'dalek';
$alien{$what} = 'Karn';
Self contained namespace
Less chance of
clashes
More information (e.g.
all keys)
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
Prevents typos
Less like BASIC
- more like Ada
Thinking about scope
is good
Using undefined variables
Writing to read-only
file handles
And many more...
Turn off use
warnings locally
Turn off specific
warnings
{
no warnings 'deprecated'; # dodgy code ... }
See perldoc perllexwarn
References
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)
$array_ref = array;
$refs[0] = $array_ref;
$another_ref = $refs[0];
$aref = [
'this', 'is', 'a', 'list']; $aref2 = [ @array ];
{ LIST }
creates anonymous hash and returns a reference
$href = {
1 => 'one', 2 => 'two' }; $href = { %hash };
Output ARRAY(0x20026800) ARRAY(0x2002bc00)
Second method creates
a copy of the array
Whole array
@array = @{$aref};
@rev = reverse
@{$aref};
Single elements
$elem = ${$aref}[0];
${$aref}[0] = 'foo';
Whole hash
%hash = %{$href};
@keys = keys
%{$href};
Single elements
$elem = ${$href}{key};
${$href}{key} = 'foo';
Instead of
${$aref}[0] you can use $aref->[0]
Instead of
${$href}{key} you can use $href->{key}
$aref = [
1, 2, 3 ]; print ref $aref; # prints ARRAY
$href = {
1 => 'one', 2 => 'two' }; print ref $href; # prints HASH
Complex data structures
@arr1 = (1,
2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size { my (@a1, @a2) = @_; print @a1 == @a2 ? 'Yes' : 'No'; }
Arrays are combined
in @_
All elements end
up in @a1
How do we
fix it?
Pass references to
the arrays
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
@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
$arr_2d = [[1,
2, 3], [4, 5, 6], [7, 8, 9]];
$arr_2d->[1] is
ref to array (4, 5, 6)
$arr_2d->[1]->[1] is
element 5
Can omit intermediate
arrows
$arr_2d->[1][1]
Jones,Martha,UNIT Harkness,Jack,Torchwood Smith,Sarah
Jane,Journalist
What would be
a good data structure?
Hash for each
record
Array of records
Array of hashes
my @records; my
@cols = ('s_name', 'f_name', 'job'); while (<FILE>) { chomp; my %rec; @rec{@cols} = split /,/; push @records, rec; }
Using an Array
of Hashes foreach (@records) { print "$_->{f_name} ", "$_->{s_name} ". "is a $_->{job}"; }
Hash of lists
Sorting
@sorted = sort
@array;
Note that it
does not sort the list in place
@array = sort
@array;
@chars = sort
'e', 'b', 'a', 'd', 'c'; # @chars has ('a', 'b', 'c', 'd', 'e')
This can sometimes
give strange results
@chars = sort
'E', 'b', 'a', 'D', 'c'; # @chars has ('D', 'E', 'a', 'b', 'c')
@nums = sort
1 .. 10; # @nums has (1, 10, 2, 3, 4, # 5, 6, 7, 8, 9)
@nums =
sort { $a <=> $b } 1 .. 10;
Perl puts two
of the values from the list into $a and $b
Block compares values
and returns -1, 0 or 1
<=> does
this for numbers ( cmp for strings)
sort { $b
cmp $a } @words
sort { lc
$a cmp lc $b } @words
sort { substr($a,
4) cmp substr($b, 4) } @lines
@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; }
Need to write
sort_names so that it sorts on surname and then forename.
Can be inefficient
on large amounts of data
Multiple splits on
the same data
@split = map
{ [ split ] } @names;
Do the comparison
@sort = sort
{ $a->[1] cmp $b->[1] or $a->[0] cmp $b->[0] } @split;
Join the data
together
@names = map
{ join ' ', @$_ } @sort;
@names = map
{ join ' ', @$_ } sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } map { [ split ] } @names;
All functions work
on the output from the previous function in the chain
Old Lisp trick
Named after Randal
Schwartz
Reusable Code
Prevent reinventing the
wheel
Easier to share
across projects
Better design, more
generic
The default package
is main
New packages are
introduced with the package keyword
A subroutine's full
name is package::name
Package name can
be omitted from within same package
Like family names
Exporter.pm defines a
subroutine called import
import is
automatically called whenever a module is used
import puts
references to our subroutines into our caller's symbol table
We make use
of inheritance
Inheritance is defined
using the @ISA array
If we call
a subroutine that doesn't exist in our module, then the modules in @ISA are also checked
Therefore Exporter::import
is called
Exports are defined
in @EXPORT or @EXPORT_OK
Automatic exports are
defined in @EXPORT
Optional exports are
defined in @EXPORT_OK
Key is set
name
Value is reference
to an array of names
our %EXPORT_TAGS =
(advanced => [ qw( my_sub my_other_sub ) ]; use MyModule qw(:advanced); my_sub(); my_other_sub();
Less chances of
name clashes
Use @EXPORT_OK
in preference to @EXPORT
Document the exported
names and sets
@EXPORT_OK = qw($scalar,
@array, %hash);
Can be part
of export sets
Any variables you
export must be package variables
Don't write the
boilerplate yourself
Copy from an
existing module
Or look at
Module::Starter
Object Orientation
OO inverts this
Classes contain methods
which define their actions
Objects are instances
of classes
Perl has an
OO system bolted on
Best of both
worlds
An Object is
a reference (usually to a hash)
Moose is on
CPAN
Based on Perl
6 OO
Well worth investigating
1;
perldoc perltoot
perldoc perlobj
perldoc perlbot
perldoc Moose (if
it is installed)
Object Oriented Perl
(Conway)
Testing
Does your code
do what it is supposed to do?
Will your code
continue to do what it is supposed to do?
Write unit tests
Run those tests
all the time
Before you add
a feature
After you have
added a feature
Before checking in
code
Before releasing code
Constantly, automaticall y
A lot of
work in this area over the last eight years
Test::Simple and Test::More
included in Perl distribution
Many more testing
modules on CPAN
Runs given tests
using Test::Harness
Comes with the
Perl distribution
-r recurse
-s shuffle tests
Many more
Now this ad-hoc
format has a definition and a name
The Test Anything
Protocol (TAP)
See Test::Harness::TAP (documentation
module) and TAP::Parser
Some of my
favourites
Test::File
Test::Exception, Test::Warn
Test::Differences
Test::XML (includes Test::XML::XPath)
Built using Test::Builder
Ensures that test
modules all use the same framework
Use it as
the basis of your own Test::* modules
Who tests the
testers?
Test your Test::Builder
test modules with Test::Builder::Tester
Devel::Cover can help
you to find out
Deep internal magic
cover
Devel::Cover Output
Devel::Cover Output
Devel::Cover Output
Other frameworks are
available
http://fit.c2.com
perldoc Test::Tutorial
perldoc Test::Simple
perldoc Test::More
perldoc Test::Builder
etc...
Dates and Times
Flexible but confusing
time –
seconds since 1st Jan 1970
localtime –
convert to human-readable
timelocal (in
Time::Local) – inverse of localtime
strftime (in
POSIX) – formatting dates and times
Dozens of date/time
modules on CPAN
Date::Manip is almost
never what you want
Date::Calc, Date::Parse, Class::Date,
Date::Simple, etc
Which one do
you choose?
"The DateTime family
of modules present a unified way to handle dates and times in Perl"
"unified" is good
Dozens of modules
that work together in a consistent fashion
For date arithmetic
you need a duration
Number of years,
weeks, days, etc
Control input format
with DateTime::Format::Strptime
DateTime::Format::HTTP
DateTime::Format::MySQL
DateTime::Format::Excel
DateTime::Calendar::Julian
DateTime::Calendar::Hebrew
DateTime::Calendar::Mayan
DateTime::Fiction::JRRTolkien::Shire
use DateTime::Fiction::JRRTolkien::Shire my
$dt = DateTime::Fiction::JRRTolkien::Shire->now; say $dt->on_date; # Trewsday 1 Wedmath 7473
Templates
Advantages are well
known
Standard look and
feel (static/dynamic)
Reusable components
Separation of code
logic from display logic
Different skill-sets (HTML
vs Perl)
Reports
Business documents
Configuration files
Anywhere you produce
output
See perlfaq4
“How can I
expand variables in text strings?”
Don't do that
Text::Template, HTML::Template, Mason,
Template Toolkit
Many, many more
Very powerful
Both web and
non-web
Simple template language
Plugins give access
to much of CPAN
Good Book Too!
Data + Alternative
Template = Alternative Output
Different views of
the same data
Only the template
changes
Just add new
output template
Perl programmer need
not be involved
Databases
Perl has tools
to make this as simple as possible
DBI is the
basis for all modern Perl database access
Create a connection
to a particular type of database
DBD module gets
loaded
DBD translates from
DBI API to database specific calls
DBD translates returned
data into Perl data stuctures
Just change the
connection line
my $sth =
$dbh->prepare( 'select name, genre from artist' );
my $sth =
$dbh->prepare( "select title, from song where artist = '$id'");
Check return values
(syntax errors)
$sth->execute
Still need to
check for errors
while (my @row
= $sth->fetchrow_array){ print "@row"; }
Fields are returned
in the same order as they are defined in the query
fetchrow_hashref (keys
are column names)
fetchall_arrayref
For (hopefully) obvious
reasons
my $sql =
"update table1 set col1 = '$val' where id_col = $id"; my $sth = $dbh->prepare($sql); $sth->execute;
But there's a
shortcut
$rows = $dbh->do($sql);
Recompiles the SQL
every time
Very inefficient
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; }
Bonus - binding
handles quoting for you
my $sql =
"insert into tab values (?, ?, ?)"; my $sth = $dbh->prepare($sql); while (<FILE>) { chomp; my @data = split; $sth->execute(@data); }
my $sql =
'insert into big_table values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
Good chance of
getting the variables in the wrong order
By the way
- there's a basic maintainability error in that SQL
my %data =
(id => 42, code => 'H2G2', ... ); # and later... foreach my $col (keys %data) { $sth->bind_param(":$col", $data{$col}; }
Which is a
bit of a bugger
Oracle does
So does PostgreSQL
(tho' the docs discourage its use)
Check your DBD
documentation
Email your friendly
neighbourhood DBD author
Store SQL queries
externally and reference them by name
Use named bind
parameters if you can
Select all the
details of this row
Select something about
related tables
Update this row
with these values
Insert a new
record with these values
Tables (relations) map
onto classes
Rows (tuples) map
onto objects
Columns (attributes) map
onto attributes
Don't write SQL
SELECT * FROM
my_table WHERE my_id = 10
and then dealing
with the prepare/execute/fetch code
use My::Object; #
warning! not a real orm my $obj = My::Object->retrieve(10) $obj->name('A New Name'); $obj->save;
Or something similar
Tangram
Alzabo
Class::DBI
Highly recommended
DBD::Oracle
Further Information
http://london.pm.org
http://www.yapceurope.org
Brazil
Australia
UK, Germany, Israel,
Pittsburgh, Nordic, Netherlands, France, Belgium, Russia, Minnesota, Austria
CGI
DBI
XML
Beginners
Advocacy
Fun with Perl
Intermediate Perl Schwartz,
foy & Phoenix (O'Reilly)
Beginning Perl Cozens
(Wrox) http://www.perl.org/books/beginning-perl/
The Perl Cookbook
(2nd edition) Christiansen & Torkington (O'Reilly)
Perl Best Practices
Conway (O'Reilly)
Perl in a
Nutshell Siever, Spainhour & Patwardhan (O'Reilly)
Data Munging with
Perl Cross (Manning)
Advanced Perl Programming
Cozens (O'Reilly)
Perl Medic Scott
(Addison Wesley)
Programming the Perl
DBI Descartes & Bunce (O'Reilly)
Writing CGI Applications
with Perl Meltzer & Michelski (Addison Wesley)
Practical mod_perl Bekman
& Cholet (O'Reilly)
Perl Template Toolkit
Chamberlain, Cross & Wardley
Advertisement