Home
Explore
Submit Search
Upload
Login
Signup
Advertisement
Introduction to Perl - Day 1
Report
Dave Cross
Follow
Owner / Senior Developer at Magnum Solutions Ltd
Aug. 1, 2009
•
0 likes
72 likes
×
Be the first to like this
Show More
•
57,593 views
views
×
Total views
0
On Slideshare
0
From embeds
0
Number of embeds
0
Check these out next
Introduction to Perl
Dave Cross
Dart programming language
Aniruddha Chakrabarti
Beginners PHP Tutorial
alexjones89
Php
Shyam Khant
01 Php Introduction
Geshan Manandhar
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
Php Using Arrays
mussawir20
Javascript basics
shreesenthil
1
of
141
Top clipped slide
Introduction to Perl - Day 1
Aug. 1, 2009
•
0 likes
72 likes
×
Be the first to like this
Show More
•
57,593 views
views
×
Total views
0
On Slideshare
0
From embeds
0
Number of embeds
0
Download Now
Download to read offline
Report
Technology
Education
Day 1 of the "Introducing Perl" training course that I ran at YAPC::Europe in Lisbon in August 2009.
Dave Cross
Follow
Owner / Senior Developer at Magnum Solutions Ltd
Advertisement
Advertisement
Advertisement
Recommended
Introduction to Perl - Day 2
Dave Cross
18.7K views
•
160 slides
DBIx::Class beginners
leo lapworth
25.8K views
•
95 slides
Advanced Perl Techniques
Dave Cross
7.7K views
•
182 slides
PHP Powerpoint -- Teach PHP with this
Ian Macali
54K views
•
354 slides
LPW: Beginners Perl
Dave Cross
43.1K views
•
120 slides
Perl tutorial
Manav Prasad
1.1K views
•
42 slides
More Related Content
Slideshows for you
(20)
Introduction to Perl
Dave Cross
•
3.2K views
Dart programming language
Aniruddha Chakrabarti
•
4.9K views
Beginners PHP Tutorial
alexjones89
•
18.2K views
Php
Shyam Khant
•
4.8K views
01 Php Introduction
Geshan Manandhar
•
2.7K views
PHP complete reference with database concepts for beginners
Mohammed Mushtaq Ahmed
•
6K views
Php Using Arrays
mussawir20
•
16.3K views
Javascript basics
shreesenthil
•
1.1K views
JavaScript Basics
Mats Bryntse
•
1.5K views
Advanced Cascading Style Sheets
fantasticdigitaltools
•
2.9K views
CSS
seedinteractive
•
3.2K views
Perl programming language
Elie Obeid
•
5.2K views
Perl Basics with Examples
Nithin Kumar Singani
•
2.7K views
Regular expressions
Eran Zimbler
•
2.3K views
PHP
sometech
•
5.1K views
Php array
Nikul Shah
•
4.9K views
Javascript
mussawir20
•
4.2K views
Arrays in PHP
Compare Infobase Limited
•
3.7K views
Operators in java
yugandhar vadlamudi
•
2K views
jQuery for beginners
Arulmurugan Rajaraman
•
34.5K views
Viewers also liked
(16)
Gccgdb
selva raj
•
1.6K views
C compilation process
RajKumar Rampelli
•
9.3K views
GCC Compiler as a Performance Testing tool for C programs
Daniel Ilunga
•
3.9K views
Gcc opt
Mark Veltzer
•
2K views
GCC, GNU compiler collection
Alberto Bustamante Reyes
•
4.1K views
GEM - GNU C Compiler Extensions Framework
Alexey Smirnov
•
2.1K views
MinGw Compiler
Avnish Patel
•
1.7K views
How it's made: C++ compilers (GCC)
Sławomir Zborowski
•
2.4K views
GNU Compiler Collection - August 2005
Saleem Ansari
•
1.7K views
Compiling Under Linux
PierreMASURE
•
1.4K views
HRM - PM in GCC
Muhammad Danish Azad
•
1.4K views
NetBeans para Java, C, C++
Manuel Antonio
•
1.9K views
Principles of compiler design
Janani Parthiban
•
7.6K views
G++ & GCC
Beste Ekmen
•
2.4K views
Deep C
Olve Maudal
•
867.7K views
GCC
Kir Chou
•
3.7K views
Advertisement
Similar to Introduction to Perl - Day 1
(20)
Beginning Perl
Dave Cross
•
3.9K views
Perl Presentation
Sopan Shewale
•
4.9K views
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
•
1K views
Intermediate Perl
Dave Cross
•
1.7K views
Introduction to Perl
worr1244
•
565 views
Introduction to Modern Perl
Dave Cross
•
18K views
Plunging Into Perl While Avoiding the Deep End (mostly)
Roy Zimmer
•
1.3K views
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
•
1.5K views
Introduction to Perl
NBACriteria2SICET
•
5 views
Php2
Keennary Pungyera
•
745 views
P H P Part I, By Kian
phelios
•
2.8K views
Tutorial perl programming basic eng ver
Qrembiezs Intruder
•
676 views
Good Evils In Perl (Yapc Asia)
Kang-min Liu
•
8.9K views
Perl slid
pacatarpit
•
906 views
Dealing with Legacy Perl Code - Peter Scott
O'Reilly Media
•
12.8K views
CGI With Object Oriented Perl
Bunty Ray
•
32.3K views
Training on php by cyber security infotech (csi)
Cyber Security Infotech Pvt. Ltd.
•
276 views
Modern Perl
Marcos Rebelo
•
1.8K views
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Andrea Telatin
•
954 views
Bioinformatica 10-11-2011-p6-bioperl
Prof. Wim Van Criekinge
•
867 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)
ServeDocs - User Guide.pdf
VivekPatil607881
•
0 views
SpinoS Corporate Presentation.pptx
Vaishak26
•
0 views
Blockchain Technology
Aryan Chaudhary
•
0 views
Why should your startup outsource software development.pdf
MaryLogan11
•
0 views
EN-04 (1).pptx
TienTran779192
•
0 views
DIMT '23 Session_Demo_ Latest Innovations Breakout.pdf
confluent
•
0 views
MCQ.pptx
Sudipta Roy
•
0 views
How Your Business Can Leverage AI/ML in the Cloud for Competitive Advantage?
Kaspar Lavik
•
0 views
Photoshop Tools.pptx
yelnatz
•
0 views
Antenna_Design__Measurements_Laboratory_Lectures.pdf
Fredrick Isingo
•
0 views
Cisco SecureX.pdf
WildhaniIhyaraRahman1
•
0 views
NLEM.pptx
Sudipta Roy
•
0 views
Innovation to startup.pptx
ravikumark42
•
0 views
Slide share
Vasuki Arumugam
•
0 views
Decision Transformers Model.pdf
JamieDornan2
•
0 views
TRANSACTION CONCEPTppt.pptx
DummyTest9
•
0 views
different-os.pptx
leilibarekatin
•
0 views
Event Stream Processing with BeepBeep 3
Sylvain Hallé
•
0 views
Intelligent Document Processing IDP.pdf
JamieDornan2
•
0 views
Angiography Imaging Systems.pdf
RushiDalve
•
0 views
Introduction to Perl - Day 1
Introduction to Perl
Day 1 An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
Creating and running
a Perl program
Getting help
Input and Output
Perl variables
Operators and Functions
Subroutines
Regular Expressions
Smart Matching
Finding and using
Modules
11:00 – Coffee
break (30 mins)
13:00 – Lunch
(90 mins)
14:30 – Begin
16:00 – Coffee
break (30 mins)
18:00 – End
What is Perl?
Pathologically Eclectic Rubbish
Lister
“Perl” is the
language
“perl” is the
compiler
Never “PERL”
System administration tasks
CGI and web
programming
Database interaction
Other Internet programming
NASA
Free (open source)
Fast
Flexible
Secure
Fun
Impatience
Creating and Running
a Perl Program
print "Hello world";
Put this in
a file called hello.pl
$ perl hello.pl
#!/usr/bin/perl
Make program executable
$ chmod +x
hello.pl
Run from command
line
$ ./hello.pl
Start with a
hash (#)
Continue to end
of line
# This is
a hello world program print "Hello, world!"; # print
For example, -w
turns on warnings
Use on command
line
perl -w hello.pl
Or on shebang
line
#!/usr/bin/perl -w
More usually
use warnings
Getting Help
Accessed through the
perldoc command
perldoc perl
perldoc perltoc
– table of contents
Also online at
http://perldoc.perl.org/
Lots of references
through the course
perldata
perlsyn
perlfaq
perlstyle
perlcheat
Many many more
Perl Variables
To retrieve the
data stored in it
User variable names
may not start with numbers
Variable names are
preceded by a punctuation mark indicating the type of data
Array variables start
with @
use strict; my
$var;
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
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.'";
my $price =
'$9.95';
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;";
print "He said
amp;quot;The price is 300amp;quot;";
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");
Test for it
with defined() function
if (defined($my_var)) {
... }
my @fruit =
('apples', 'oranges', 'guavas', 'passionfruit', 'grapes');
my @magic_numbers =
(23, 42, 69);
my @random_scalars =
('mumble', 123.45, 'dave cross', -300, $name);
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
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
$array[400] = 'something
else';
Also with slices
@array[4, 7 ..
9] = ('four', 'seven', 'eight', 'nine');
@array[1, 2] =
@array[2, 1];
Therefore $#array
+ 1 is the number of elements
$count = @array;
Does the same
thing and is easier to understand
Initialised with a
list
%french = ('one',
'un', 'two', 'deux', 'three', 'trois');
“fat comma” (=>)
is easier to understand
%german = (one
=> 'ein', two => 'zwei', three => 'drei');
print $german{two};
As with arrays,
notice the use of $ to indicate that we're accessing a single value
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
$hash{bar} = 'something
else';
Also with slices
@hash{'foo', 'bar'} =
('something', 'else');
@hash{'foo', 'bar'} =
@hash{'bar', 'foo'};
There is no
equivalent to $#array
print %hash
is unhelpful
We'll see ways
round these restrictions later
Many of them
have punctuation marks as names
Others have names
in ALL_CAPS
They are documented
in perlvar
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
Three uses of
$_
Contains your programs
command line arguments
my $num =
@ARGV; print "$num arguments: @ARGV";
perl printargs.pl foo
bar baz
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
We'll see more
input and output methods later in the day
But here are
some simple methods
print “Hello world”;
$input = <STDIN>;
< ... >
is the “read from filehandle” operator
STDIN is the
standard input filehandle
Operators and Functions
“Things” that do
“stuff”
Routines built into
Perl to manipulate data
Less standard operations
modulus (%), exponentiation (**)
$speed = $distance
/ $time;
$vol = $length
* $breadth * $height;
$area = $pi
* ($radius ** 2);
$odd = $number
% 2;
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
Subtle difference between
$x++ and ++$x
$name = $firstname
. ' ' . $surname;
Repetition (x)
$line = '-'
x 80; $police = 'hello ' x 3;
Shortcut versions available
$page .= $line;
# $page = $page . $line
$thing x= $i;
# $thing = $thing x $i
-e $file
does the file exist
-r $file
is the file readable
-w $file
is the file writeable
-d $file
is the file a directory
-f $file
is the file a normal file
-T $file
is a text file
-B $file
is a binary file
Can take more
arguments than operators
Arguments follow the
function name
See perlfunc for
a complete list of Perl's built-in functions
$age = 29.75;
$years = int($age);
@list = ('a',
'random', 'collection', 'of', 'words'); @sorted = sort(@list); # a collection of random words
$len = length
$a_string;
uc and
lc return upper and lower case versions of a string
$string = 'MiXeD
CaSe'; print "$string", uc $string, "", lc $string;
See also
ucfirst and lcfirst
$word = 'word';
$letter = chop $word;
chomp removes
the last character only if it is a newline and returns true or false appropriately
$string = 'Hello
world'; print substr($string, 0, 5); # prints 'Hello'
Unlike many other
languages you can assign to a substring
substr($string, 0, 5)
= 'Greetings'; print $string; # prints 'Greetings world'
cos ,
sin , tan standard trigonometric functions
exp exponentiation
using e
log logarithm
to base e
rand returns
a random number
sqrt returns
the square root
push @array, $value;
pop removes
and returns the last element in an array
$value = pop
@array;
shift and
unshift do the same for the start of an array
@sorted = sort
@array;
sort does
a lot more besides, see the docs (perldoc -f sort)
reverse returns
a reversed list
@reverse = reverse
@array;
@array = (1
.. 5); $string = join ', ', @array; # $string is '1, 2, 3, 4, 5'
split takes
a string and converts it into an array
$string = '1~2~3~4~5';
@array = split(/~/, $string); # @array is (1, 2, 3, 4, 5)
exists tells
you if an element exists in a hash
keys returns
a list of all the keys in a hash
values returns
a list of all the values in a hash
open(my $file, '<',
'in.dat');
You can then
read the file with <$file>
$line =
<$file>; # one line
@lines = <$file>;
# all lines
Finally, close the
file with close
close($file);
$bytes = read(FILE,
$buffer, 1024);
seek to
move to a random position in a file
seek(FILE, 0, 0);
tell to
get current file position
$where = tell
FILE;
truncate to
truncate file to given size
truncate FILE, $where;
open my $file,
'>', 'out.dat'; # overwrite
open my $file,
'>>', 'out.dat'; # append
Write to file
using print
print $file “some
data”;
Note lack of
comma
$now = time;
localtime converts
that into more usable values
($sec, $min, $hour,
$mday, $mon, $year, $wday, $yday, $isdst) = localtime($now);
$year is
years since 1900
$wday is
0 (Sun) to 6 (Sat)
@time_bits = localtime(time);
Call to time
can be omitted
@time_bits = localtime;
Use array slices
($d, $m, $y)
= (localtime)[3 .. 5];
Easier to use
strftime (from POSIX.pm)
print strftime('%Y-%m-%d',
localtime);
Format followed by
list of date/time values
Conditional Constructs
This makes for
far more interesting programs
The unit of
program execution is a block of code
Blocks are delimited
with braces { … }
But what is
truth?
'' (the empty
string)
undef (an undefined
value)
$x >= $y
or $x ge $y
Also <
( lt ) and <= ( le )
'0' == (3
* 2) - 6 # true
'apple' gt 'banana'
# false
'apple' == 'banana'
# true(!)
1 + 2
== '3 bears' # true
1 + 3
== 'three' # false
Different precedence though
Only need to
evaluate EXPR_2 if EXPR_1 evaluates as false
We can use
this to make code easier to follow
open FILE, 'something.dat'
or die "Can't open file: $!";
@ARGV == 2
or print $usage_msg;
if (EXPR) {
BLOCK }
Only executes BLOCK
if EXPR is true
if ($name eq
'Doctor') { regenerate(); }
if (EXPR) {
BLOCK1 } else { BLOCK2 }
If EXPR is
true, execute BLOCK1, otherwise execute BLOCK2
if ($name eq
'Doctor') { regenerate(); } else { die "Game over!"; }
if (EXPR1) {
BLOCK1 } elsif (EXPR2) { BLOCK2 } else { BLOCK3 }
If EXPR1 is
true, execute BLOCK1 else if EXPR2 is true, execute BLOCK2 otherwise execute BLOCK3
if ($name eq
'Doctor') { regenerate(); } elsif ($tardis_location eq $here) { escape(); } else { die "Game over!"; }
while (EXPR) {
BLOCK }
Repeat BLOCK while
EXPR is true
while ($dalek_prisoners) {
print "Ex-ter-min-ate"; $dalek_prisoners--; }
until (EXPR) {
BLOCK }
Execute BLOCK until
EXPR is true
until ($regenerations ==
12) { print "Regenerating"; regenerate(); $regenerations++; }
for (INIT; EXPR;
INCR) { BLOCK }
Like C
Execute INIT If
EXPR is false, exit loop, otherwise execute BLOCK, execute INCR and retest EXPR
for ($i =
1; $i <= 10; $i++) { print "$i squared is ", $i * $i, ""; }
Used surprisingly rarely
foreach VAR (LIST)
{ BLOCK }
For each element
of LIST, set VAR to equal the element and execute BLOCK
foreach $i (1
.. 10) { print "$i squared is ", $i * $i, ""; }
my %months =
(Jan => 31, Feb => 28, Mar => 31, Apr => 30, May => 31, Jun => 30, … ); foreach (keys %months) { print "$_ has $months{$_} days"; }
while (<STDIN>) {
print; }
This is the
same as
while (defined($_ =
<STDIN>)) { print $_; }
last –
jump out of loop
redo –
jump to start of same iteration of loop
Subroutines
Make it easy
to repeat code
Subroutines have a
name and a block of code
sub NAME {
BLOCK }
exterminate();
exterminate;
last one only
works if function has been predeclared
exterminate('The Doctor');
Arguments end up
in the @_ array within the function
sub exterminate {
my ($name) = @_; print "Ex-Ter-Min-Ate $name"; $timelords--; }
sub exterminate {
foreach (@_) { print "Ex-Ter-Min-Ate $_"; $timelords--; } }
&my_sub passes
on the contents of @_ to the called subroutine
sub first {
&second }; sub second { print @_ }; first('some', 'random', 'data');
You usually don't
want to do that
Passing by reference
passes the actual variable. Changing the argument alters the external value
Perl allows you
to choose
my ($arg1, $arg2)
= @_;
Updating $arg1
and $arg2 doesn’t effect anything outside the subroutine
Simulating pass by
reference
$_[0] = 'whatever';
Updating the contents
of @_ updates the external values
sub exterminate {
if (rand > .25) { print "Ex-Ter-Min-Ate $_[0]"; $timelords--; return 1; } else { return; } }
sub exterminate {
my @exterminated; foreach (@_) { if (rand > .25) { print "Ex-Ter-Min-Ate $_"; $timelords--; push @exterminated, $_; } } return @exterminated; }
Regular Expressions
A bit like
wild-cards
Sometimes overused!
Documented in perldoc
perlre
Works on
$_ by default
In scalar context
returns true if the match succeeds
In list context
returns list of "captured" text
m is optional
if you use / characters
With m you
can use any delimiters
Works on
$_ by default
In scalar context
returns true if substitution succeeds
In list context
returns number of replacements
Can choose any
delimiter
$name =~ s/Dave/David/;
^ -
matches start of string
$ -
matches end of string
. -
matches any character (except )
-
matches a whitespace character
-
matches a non-whitespace character
-
matches any non-digit
-
matches any "word" character
-
matches any "non-word" character
-
matches a word boundary
-
matches anywhere except a word boundary
? -
match zero or one
* -
match zero or more
+ -
match one or more
{n} -
match exactly n
{n,} -
match n or more
{n,m} -
match between n and m
/[aeiou]/ # match
any vowel
Use - to
define a contiguous range
/[A-Z]/ # match
upper case letters
Use ^ to
match inverse set
/[^A-Za-z] # match
non-letters
/rose|martha|donna/i;
Use parentheses for
grouping
/^(rose|martha|donna)$/i;
The captured parts
are in $1 , $2 , etc…
while (<FILE>) {
if (/^(+)+(+)/) { print "The first word was $1"; print "The second word was $2"; } }
my @nums =
$text =~ /(+)/g; print "I found these integers:"; print "@nums";
perldoc perlretut
Mastering Regular Expressions
– Jeffrey Freidl
Smart Matching
Powerful matching operator
DWIM
Examines operands
Decides which match
to apply
New operator
Looks a bit
like the binding operator ( =~ )
Can be used
in place of it
$some_text =~ /some
regex/
Can be replaced
with
$some_text ~~ /some
regex/
~~ does
a regex match
Cleverer than that
though
%hash ~~ /regex/
Regex match on
hash keys
@array ~~ /regex/
Regex match on
array elements
Checks that arrays
are the same
$scalar ~~ @array
Checks scalar exists
in array
$scalar ~~ %hash
Checks scalar is
a hash key
$scalar1 ~~ $scalar2
It depends
If both look
like numbers
~~ acts
like ==
Otherwise
~~ acts
like eq
Finding and Using
Modules
Perl comes with
over 100 modules (see “perldoc perlmodlib” for list)
http://search.cpan.org
distribution name
You can have
your own personal module library
CPANPLUS.pm is included
with newer Perls
Automatically carries out
installation process
Can also handle
required modules
Or
cpanp -i Some::Module
Object modules usually
don't
Difference not clear
cut (e.g. CGI.pm)
use My::Module;
Import optional components:
use My::Module qw(my_sub
@my_arr);
Import defined sets
of components:
use My:Module qw(:advanced);
Use imported components:
$data = my_sub(@my_arr);
use My::Object;
Create an object:
$obj->set_name($name);
Time::Local
Text::ParseWords
Getopt::Std
Cwd
POSIX
CGI
Carp
Benchmark
Data::Dumper
DBI
DBIx::Class
DateTime
HTML::Parser
WWW::Mechanize
Email::Simple
XML::LibXML
XML::Feed
Moose
See you tomorrow
Advertisement