perl 101

51
Perl 101 An introduction to the Perl programming language Alex Balhatchet @ Makers Academy, November 2013

Upload: alex-balhatchet

Post on 08-May-2015

1.788 views

Category:

Technology


1 download

DESCRIPTION

An Introduction for Perl, presented to two Makers Academy cohorts on Monday November 4th 2013. Makers Academy is a 12 week intensive course teaching Ruby and web development. This presentation was intended to give them an introduction to another language they might be interested in picking up to widen their range of skills.

TRANSCRIPT

Page 1: Perl 101

Perl 101An introduction to the Perl programming language

Alex Balhatchet @ Makers Academy, November 2013

Page 2: Perl 101

Who Am I?

Page 3: Perl 101

Who’s this guy?

● Alex Balhatchet

● CTO at Nestoria property search engine

● Programming Perl for 12 years

● Hiring, training and mentoring Perl interns and permanent hires for 4 years

Page 4: Perl 101

Nestoria

● Property search engine

● Operating in 8 countries, 6 languages

● Serving 1.3 million search requests per day

● 85% Perl, 5% JavaScript, 5% C, 5% Other

Page 5: Perl 101

Nestoria

Page 6: Perl 101

Perl History

Page 7: Perl 101

What is Perl?

Perl 5 is a high-level, general purpose, interpreted, dynamic programming language.

It was largely inspired by grep, sed, awk, and C.

It influenced Python, Ruby, and PHP.

Page 8: Perl 101

So, Perl

● 1.0 released in 1987

● 5.0 released in 1994

● Language is now “Perl 5”

● Perl 6 is a new language separate from, but related to, Perl 5

Page 9: Perl 101

So, Perl 5

● Annual releases since 2010

● Perl 5.18 was released May 2013

● Perl 5.20 will be released May 2014

● New releases contain new features, bug fixes, and performance improvements

Page 10: Perl 101

Perl Philosophy

Page 11: Perl 101

TIMTOWTDI

TIMTOWTDI is pronounced “Tim Toady” and is:

There is more than one way to do it

Perl aims to be aggressively non-prescriptivist. Every problem should have multiple solutions.

Page 12: Perl 101

Making easy things easy& hard things possible

This quote comes from the front of Learning Perl, also known as the Llama.

It goes hand-in-hand with TIMTOWTDI.

Every problem has multiple solutions implies every problem has at least one solution :-)

Page 13: Perl 101

Do What I Mean

Perl’s creator Larry Wall has a linguistics background, and took some of that with him when he designed Perl.

Keywords such as “for”, “my”, “defined”, “say”, “do”, “while”, “if”, “unless”, and “use” all read quite nicely to English eyes as well as to programmer eyes.

Page 14: Perl 101

Low Ambiguity

In many languages this is acceptable:

puts 1 + 2 # 3

puts "a" + "b" # "ab"

puts "2" + 1 # !!

in `+': can't convert Fixnum into String (TypeError)

In Perl we use different operators:

say 1 + 2; # 3

say "a" . "b"; # "ab"

say "2" + 1; # 3

say "2" . 1; # 21

Page 15: Perl 101

Perl Syntax

Page 16: Perl 101

Variables - Scalars

$foo = 1;

$bar = 3.14195;

$str = "Hello, World!";

Page 17: Perl 101

Variables - Arrays

@things = (1, 2, 3);

$things[2]; # 3

join(",", @things); # 1,2,3,4

Page 18: Perl 101

Variables - Hashes

%hash = (

key => "value",

foo => $bar,

thingies => "whatsits",

);

say $hash{key}; # "value"

Page 19: Perl 101

Variables - Data Structures

$alex_hashref = {

name => {

first => "Alex",

last => "Balhatchet",

},

languages => [

"English", "German", "Perl"

],

};

Page 20: Perl 101

Conditionals

if ($true) {

say "It’s true!";

}

say "It’s true too!" if $true_two;

Page 21: Perl 101

Loops

for $i (1 .. 20) {

say $i * $i;

}

while ($line = <STDIN>) {

say length $line;

}

Page 22: Perl 101

Regular Expressions - Matching

$str = "Hello, Makers Academy!";

if ($str =~ m/makers/i) {

say "matched!";

}

else {

say "no match :-(";

}

Page 23: Perl 101

Regular Expressions - Matching

$num = -1.23456;

if ($num =~ m/^ [+-]? \d+ ([.]\d+)? $/x) {

say "looks like a number!";

}

else {

say "that’s no number I ever saw...";

}

Page 24: Perl 101

Regular Expressions - Substitutions

$str = "Hello, Makers Academy!";

$str =~ s/Makers Academy/Makers!/;

say $str; # Hello, Makers!!

Page 25: Perl 101

Subroutines

sub add {

($num_a, $num_b) = @_;

return $num_a + $num_b;

}

say add(3, 4); # 7

say add(-3, 3); # 0

say add(1/3, 2/3); # 1

Page 26: Perl 101

Objectspackage MyClass;

sub new {

($class) = @_;

return bless {}, $class;

}

sub add {

($self, $n1, $n2) = @_;

return $n1 + $n2;

}

use MyClass;

$Object = MyClass->new();

say $Object->add(123, 456);

Page 27: Perl 101

Perl Advantages

Page 28: Perl 101

Perl AdvantagesCPAN

Page 29: Perl 101

CPAN

CPAN stands for Comprehensive Perl Archive Network, and is a collection of Perl modules.

You can browse CPAN at cpan.org

Today CPAN contains 126,892 Perl modules in 28,607 distributions, written by 11,031 authors, mirrored on 271 servers.

Page 30: Perl 101

CPAN Modules

Web Frameworks (Catalyst, Dancer, Mojo)

ORMs (DBIx::Class, Fey, Norma)

Serialization (XML::Simple, JSON)

Maths (Math::Random::MT, Statistics::TTest, Geo::Distance)

Page 31: Perl 101

Perl AdvantagesUnicode

Page 32: Perl 101

Unicode

Perl was made with text manipulation in mind, and has excellent support for Unicode.

All of Perl’s builtin functions, including regular expressions, are Unicode safe.

Many CPAN modules that deal with text will also have considered encoding issues.

Page 33: Perl 101

Unicode Examplesuse utf8::all;

$str = "Schwanhäußerstraße";

say $str; # Schwanhäußerstraße

say length $str; # 18

$str = uc $str;

say $str; # SCHWANHÄUSSERSTRASSE

say substr $str, 7, 1; # Ä

if ($str =~ m/schwanhäußerstraße/i) {

say "I \N{HEAVY BLACK HEART} Perl"; # I ❤ Perl

}

Page 34: Perl 101

Perl AdvantagesTesting

Page 35: Perl 101

Testing

Perl has a big testing culture.

Most CPAN modules have a test suite!

There are also a good number of CPAN modules to help you with testing your code.

Page 36: Perl 101

Testing Exampleuse Test::More tests => 3;

ok 1, "1 is true";

is 1 + 1, 2, "1 + 1 = 2";

@a = sort (4, 2, 3, 1);

is_deeply(

\@a,

[ 1, 2, 3, 4 ],

"got expected array"

);

~$ prove -v example.t

example.t ..

1..3

ok 1 - 1 is true

ok 2 - 1 + 1 = 2

ok 3 - got expected array

ok

All tests successful.

Files=1, Tests=3, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.02 cusr 0.00 csys = 0.04 CPU)

Result: PASS

Page 37: Perl 101

Perl AdvantagesCommunity

Page 38: Perl 101

Perl Community

Monthly social meetings (London.pm)

Bi-monthly technical meetings (London.pm)

Free conference (London Perl Workshop)

See the world! YAPC::EU, YAPC::NA, YAPC::Russia, YAPC::SA, YAPC::Asia, and YAPC::Australia

Page 40: Perl 101

Perl AdvantagesJobs

Page 41: Perl 101

Perl developers are in demand

We are hiring… and so is pretty much every other Perl company!

http://lokku.com/jobs

http://jobs.perl.org

Page 42: Perl 101

Perl at Nestoria

Page 43: Perl 101

Nestoria

Page 44: Perl 101

Nestoria Listings Processing

● XML parsing

● Geocoding

● Natural language processing

● De-duplication

● Image thumbnailing

Page 45: Perl 101

The Nestoria Website

● Apache + mod_perl

● Templating with HTML::Mason

● Maketext (and Locale::Maketext) for translations

● Query Analysis / Geographic lookup

● Spell checking and fuzzy matching

● Super fast (90% requests under 200ms)

Page 46: Perl 101

Other Stuff :-)

● geodata system

● Continuous testing and deployment

● Metrics processing system

● Automated PDF and Excel file “end of month” reports

● Email Alerts

● Average House Prices

Page 47: Perl 101

Summary

Page 48: Perl 101

Summary

● Perl has a rich history and an exciting future

● Perl is a straightforward, easy to learn language

● Perl has a lot of advantages (CPAN, Unicode, & Jobs!)

● At Nestoria we’ve shown that Perl can be the foundation of a highly successful and technically sophisticated startup business

Page 49: Perl 101

Thanks!

Page 50: Perl 101

Questions?

Page 51: Perl 101

Nestoria

is H

iring!

http://l

okku.co

m/jobs

Thanks!@kaokun@nestoria

http://github.com/lokku