perl 101

Post on 08-May-2015

1.788 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

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

Perl 101An introduction to the Perl programming language

Alex Balhatchet @ Makers Academy, November 2013

Who Am I?

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

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

Nestoria

Perl History

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.

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

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

Perl Philosophy

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.

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

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.

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

Perl Syntax

Variables - Scalars

$foo = 1;

$bar = 3.14195;

$str = "Hello, World!";

Variables - Arrays

@things = (1, 2, 3);

$things[2]; # 3

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

Variables - Hashes

%hash = (

key => "value",

foo => $bar,

thingies => "whatsits",

);

say $hash{key}; # "value"

Variables - Data Structures

$alex_hashref = {

name => {

first => "Alex",

last => "Balhatchet",

},

languages => [

"English", "German", "Perl"

],

};

Conditionals

if ($true) {

say "It’s true!";

}

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

Loops

for $i (1 .. 20) {

say $i * $i;

}

while ($line = <STDIN>) {

say length $line;

}

Regular Expressions - Matching

$str = "Hello, Makers Academy!";

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

say "matched!";

}

else {

say "no match :-(";

}

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...";

}

Regular Expressions - Substitutions

$str = "Hello, Makers Academy!";

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

say $str; # Hello, Makers!!

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

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

Perl Advantages

Perl AdvantagesCPAN

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.

CPAN Modules

Web Frameworks (Catalyst, Dancer, Mojo)

ORMs (DBIx::Class, Fey, Norma)

Serialization (XML::Simple, JSON)

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

Perl AdvantagesUnicode

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.

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

}

Perl AdvantagesTesting

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.

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

Perl AdvantagesCommunity

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

Perl AdvantagesJobs

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

Perl at Nestoria

Nestoria

Nestoria Listings Processing

● XML parsing

● Geocoding

● Natural language processing

● De-duplication

● Image thumbnailing

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)

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

Summary

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

Thanks!

Questions?

Nestoria

is H

iring!

http://l

okku.co

m/jobs

Thanks!@kaokun@nestoria

http://github.com/lokku

top related