modern perl for the unfrozen paleolithic perl programmer

Post on 15-Apr-2017

362 Views

Category:

Internet

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Modern Perl for the Unfrozen Paleolithic Perl Programmer

John SJ Anderson ⭐ Infinity Interactive ⭐ @genehackOpenWest 2016 ⭐ 14 July 2016 ⭐ Sandy UT

Disclaimer

I'mma talk pretty fast. Feel free to ask questions particularly if I'm moving too quickly

Hi Hi I'm John

• VP Tech, Infinity Interactive • Ex-biologist • Linux • Perl tribe • Polyglot coder

• Just this guy, you know?

meet me!

I'M JUST A CAVEMAN

YOUR MODERN PERL FRIGHTENS AND CONFUSES ME

I was inspired to write this talk by a few online friends who happen to be Perl programmers -- but they're not engaged with the community, they're just using Perl to get a job done, and there's a widening gulf between what I think is reasonable and what they're doing -- so I wanted to put together sort of an info dump to bring people up to speed. this talk is going to a wide-ranging survey of what's happened in Perl in the last 5-10 years.

Welcome to 2016!

So, hi, unfrozen Paleolithic Perl programmer! Welcome to 2016!

Quite a few things have changed...

Many things have changed since you fell into that glacier in 2001,

...but many haven't

There's a Clinton running for President!

One thing is different: Perl6 was released!

but this talk is about perl5, so that's the last i'm going to talk about perl6

We're still using Perl5!

Most of us are still happily coding in Perl5, however

2001 Perl 5.6

When you slipped into that big hole in the ice, perl 5.6 was the latest and greatest

2016 Perl 5.24

But we're all the way up to 5.24 now. So let's talk about that 15 year gap...

We had some bad years

there was a rough patch

5.6.0 - 22 Mar 2000 5.8.0 - 18 Jul 2002

5.10.0 - 18 Dec 2007 5.12.0 - 12 April 2010

here's the release dates for several perl versions, starting with 5.6 (latest and greatest in 2001, remember)

5.6.0 - 22 Mar 2000 5.8.0 - 18 Jul 2002

5.10.0 - 18 Dec 2007 5.12.0 - 12 April 2010

see that five year gap there? yeah, those were not great times.

5.6.0 - 22 Mar 2000 5.8.0 - 18 Jul 2002

5.10.0 - 18 Dec 2007 5.12.0 - 12 April 2010

but, starting with 5.10, which sort of brought perl 5 back to life, and then particularly, since 5.12, we've had this great thing! ...

Regular release cycle

a regular, predictable release cycle!

5.12.0 - 12 April 2010 5.14.0 - 14 May 2011 5.16.0 - 20 May 2012 5.18.0 - 18 May 2013 5.20.0 - 27 May 2014 5.22.0 - 01 Jun 2015 5.24.0 - 09 May 2016

here are all the releases since 5.12. notice anything?

5.12.0 - 12 April 2010 5.14.0 - 14 May 2011 5.16.0 - 20 May 2012 5.18.0 - 18 May 2013 5.20.0 - 27 May 2014 5.22.0 - 01 Jun 2015 5.24.0 - 09 May 2016

see those nice regular predictable releases, happening every year like clockwork? that's pretty cool.

underlying this is a regular series of dev releases (those are in odd numbered versions, e.g. 5.23 is the series of dev releases between 5.22 and 5.24), which happen on a monthly cycle

Perl5 pumpking

credit for starting and more importantly continuing this goes to a couple of p5 pumpkings, which is what we call the person who is in charge of the Perl5 effort -- our Benevolent Dictator for the Moment, if you will

5.12.0 - 12 April 2010 5.14.0 - 14 May 2011 5.16.0 - 20 May 2012 5.18.0 - 18 May 2013 5.20.0 - 27 May 2014 5.22.0 - 01 Jun 2015 5.24.0 - 09 May 2016

Jesse

Rik

there were two pumpkings involved in this, Jesse Vincent for 5.12 and 5.14, and then Rik Signes for 5.16 thru 5.24 (which just came out a few months ago)

Regular release cycle

getting back onto a regular predictable release cycle has arguably been the single greatest factor keeping Perl5 relevant and catalyzing the Perl "Renaissance" you may be hearing about

❤ Jesse ❤ Rik

much respect to Jesse and Rik for the work involved in this, it was *not* a trivial undertaking to start or to keep going.

❤ Sawyer

this is also a good time to mention that Rik retired from the pumpking role after releasing 5.24.0, and our new pumpking is Sawyer. Sawyer is great and as you can see from this picture, he *really* loves hugs, so be sure to give him one when you see him at a conference.

Modern Perl

modernperlbooks.com

Another catalyst for the Perl Renaissance is the book "Modern Perl", written by chromatic. He has made this freely available online; you can also purchase a paper copy if that's how you roll.

❤ chromatic

modernperlbooks.com

Writing this book was a tremendous effort and it's an absolutely essential read if you want to understand what we call "Modern Perl", or how we do Perl in the 21st century.

Many language

improvements One of the main benefits of the regular release cycle has been a number of language improvements. I'm only going to talk about a few today, but you can always read the 'perldeltas', or change summaries that come out with each new release.

Unicode support!

In 2001, the world of Perl 5.6, Unicode existed but wasn't very well understood or very widely used. Today, of course, we live in a Unicode world, and it's something all programmers and languages have to deal with. It's kinda too complicated to get into here, so I'm just going to say that now we can get _really_ expressive in our Perl code

$code = "😀";

So it's much easier to tell if we're happy...

$code = "💩";

... or when things aren't going quite as well.

This should be a familiar way of communication for you!

(okay, this is my last caveman joke...)

Perl unicode support got _ok_ in 5.12, and got good in 5.14.

false values: 0, '', (), undef everything else is true

undefined values: undef everything else is defined

Quick Perl background

Before I can explain this next one, I need to do a brief refresher on a couple of aspects of Perl. Perl has the usual notion of true/false. These four values are false; everything else is true. 'undef' is a special value; it's what Perl gives to a newly declared but uninitialized variable. That means there's this additional dimension of defined/undefined we have to deal with

Boolean operators (or, and) look at truthiness.

Many times, only invalid value for something is undef. So can't look at truthiness; must look

at definedness

Quick Perl background

Now, Perl's Boolean operations *only* look at true/false. They don't care about defined/undefined at all, except for how undef is false.

But many times, you don't end up caring about true/false, you care about def/undef

if(defined($this)){$value=$this}else{$value=$that}

So that leads to writing code that looks like this... *explain code*

$value=defined($this)?$this:$that

The more idiomatic way of writing that code is to use the ternary operator -- this just does the same thing as the previous slide in a more compact way

$value=$this//$that

defined-or

Now that I've given you that background, now I can explain this great new feature we got in 5.10, called 'defined-or'. It let's you write that code like so.

Which is pretty awesome. Defined-or was my favorite new perl feature of the 2000s

$this//='default'#muchlikethis,but…$this||='default'

defined-or

You can also combine this with assignment, in much the same way you can say ||= for 'or equals'

my$copy=$orig;$copy=~s/swap/stuff/;

Regular expressions are a big part of Perl programming, including substitution style regexps. If you want to apply a substitution to a variable but _not_ change your original value, you need to do something like this - make a copy, then do the sub on the copy

(my$copy=$orig)=~s/swap/stuff/;

and this is just a more idiomatic way to do the same thing.

(my$copy=$orig)=~s/swap/stuff/;

this makes the copy (and the parens are *required*)...

(my$copy=$orig)=~s/swap/stuff/;

and then this applies the sub to the result of the left hand side, which means $copy gets changed and $orig is left unchanged

my$copy=$orig=~s/swap/stuff/r;

non-destructive s///

new in 5.14, we have a new way do this. don't need the parens.

the key here is ...

my$copy=$orig=~s/swap/stuff/r;

non-destructive s///

this 'r' modifier to the regexp, which makes

my$copy=$orig=~s/swap/stuff/r;

non-destructive s///

this part return the result of applying the sub to $orig (hence, 'r' - for return)

my$copy=$orig=~s/swap/stuff/r;

non-destructive s///

and then we just assign that to $copy.

Again, new as of 5.15

dereferencing @{$arrayref}

so, if you've ever worked with references in perl, you've probably done this. a reference is how perl does data abstraction, it's kinda like a pointer (only way safer). this is how you turn a reference to an array back into the actual array

postfix dereferencing $arrayref->@*

New syntax for this -- not really *less* punctuation, but different punctuation. I'm not a huge fan, but some people (including former pumpking Rik Signes) really like this.

We added postfix deref in 5.20; it became non-experimental in 5.24

double diamond <<>>

We also got something called the 'double diamond' operator in 5.22

diamond <>

This is related to the classic 'diamond' operator, which is used when processing files that are passed as arguments on the command line

#loopoverfilesfromcommandlinewhile(<>){#dosomethingwitheachline}

#calllike'script.plfile1file2file3'

using the diamond operator typically looks like this, you'll have a script with this while loop, and then call it with a bunch of files on the command line. this will loop over the lines in file1, then file2, then file3, running the loop body for each one

problem! <> uses 2-arg

open semantics! the problem with this is <> uses 2 argument open(). why is that bad?

2-arg open open(IN,'<file');

this is what we call a 2 arg open call. we're opening a file, for reading, and attaching it to a file handle called IN

2-arg open open(IN,'<file');

this thing right here, in front of the file name, is how you want to open the file. this is open for reading.

2-arg open open(IN,'>file');

where as this is open for writing. (and if you wrote this code and called the file handle IN, we can't be friends.)

2 arg is bad, because if 'file' is actually in a variable, and somehow that variable gets a '>' or '<' or '|' in it, unexpected things happen

3-arg open open($in,'<','file');

this is how we write that code these days, the three argument form. here the file name is just used literally, and if it gets one of those punctuation chars in it, you'll get an error rather than a bad surprise

problem! <> uses 2-arg

open semantics! so, the problem is <> uses the 2-arg form...

Means that this:

script.pl'>file1'

is gonna be a bad time

So doing something like this will open 'file1' for writing, clobbering whatever is there.

WHOOPS.

<<>> fixes this.

again, available as of 5.22

subroutine signatures

Finally, and I think this is maybe _the_ biggest feature this decade, we've also recently gotten support for subroutine signatures in 5.20

subadd{my($one,$two)=@_;return$one+$two;}

this is how you might write a simple perl function to add together two numbers. the @_ is a special variable that's used to pass in the arguments for the function.

subadd{my($one,$two)=@_;return$one+$two;}

This bit in the first line is called "unpacking @_", and code like this is at the top of literally almost every Perl function *EVER*

subadd($one,$two){return$one+$two;}

this code does the exact same thing!

subadd($one,$two){return$one+$two;}

so we don't even have to unpack @_ ourselves! you can even provide default values, validate arguments in the function signature, and all sorts of crazy stuff

Who has started using subroutine signatures? They're still experimental, but hopefully will be non-experimental in 5.26

new toolsIt's not just language level features, we also have a host of new tools to make it easier to work with Perl

"system" perl:

These days, we make a distinction between the Perl that comes with your OS

"system" perl: just say no

and encourage people not to use it for their development projects

/usr/bin/perl

Yes, that means good old /usr/bin/perl...

/usr/bin/perl

is no more. Instead, we use tools to install new Perls, outside the control of the OS

perlbrew

The first tool that was developed for this purpose is called Perlbrew

perlbrew.pl

which you can get at perlbrew.pl

plenv

there's also a newer option, plenv

https://github.com/tokuhirom/plenv

its website isn't quite as snazzy as perlbrew's

advantages• Solves "vendor Perl lockin" issues

• Install multiple Perls in your home directory

• ... or elsewhere

• Trivially switch from Perl version to Perl version

• Able to install modules without special permissions

• Easy to stay up to date with Perl development

either one of these tools will give you numerous advantages over using the system perl

plenv > perlbrew• Less magic messing around with $PATH

• Can "pin" Perl different ways: globally, per-shell, or per-directory

personally, i'm using plenv these days -- the ability to easily specify a Perl version for a particular project is particularly useful to me

perlbrew > plenv• Kicka** website

speaking of installing...

we also have a few new tools that make installing and managing modules easier

local::lib

one of them is a module called local::lib

local::lib• Install your own copies of modules

• In your $HOME (so no special permissions needed)

• Can also install per-project modules

• Integrates well with other tools

• Not going to go into how to make it work, but the docs are great!

❤ mst

originally developed by matt trout, so thanks matt

speaking of installation tools…

cpanminus a/k/a cpanm

We've got a new CPAN client these days, cpanminus

%cpanGit::WrapperCPAN:Storableloadedok(v2.54)Reading'/Users/genehack/.cpan/Metadata'DatabasewasgeneratedonSat,10Oct201501:17:02GMTRunninginstallformodule'Git::Wrapper'CPAN:LWP::UserAgentloadedok(v6.13)FetchingwithLWP:http://cpan.schatt.com/authors/id/G/GE/GENEHACK/Git-Wrapper-0.045.tar.gzCPAN:YAMLloadedok(v1.15)CPAN:Digest::SHAloadedok(v5.95)FetchingwithLWP:http://cpan.schatt.com/authors/id/G/GE/GENEHACK/CHECKSUMSCPAN:Compress::Zlibloadedok(v2.068)Checksumfor/Users/genehack/.cpan/sources/authors/id/G/GE/GENEHACK/Git-Wrapper-0.045.tar.gzoktmp-47326fortmp-47326:Nosuchfileordirectoryat/opt/plenv/versions/5.23.2/lib/perl5/5.23.2/CPAN/Distribution.pmline468.CPAN:File::Temploadedok(v0.2304)CPAN:CPAN::Meta::Requirementsloadedok(v2.133)CPAN:Parse::CPAN::Metaloadedok(v1.4417)CPAN:CPAN::Metaloadedok(v2.150005)CPAN:Module::CoreListloadedok(v5.20150820)ConfiguringG/GE/GENEHACK/Git-Wrapper-0.045.tar.gzwithMakefile.PLLocatingbin:git...foundat/opt/git/bin/git.Checkingifyourkitiscomplete...LooksgoodGeneratingaUnix-styleMakefileWritingMakefileforGit::WrapperWritingMYMETA.ymlandMYMETA.json

this is the output from using the default 'cpan' client to install something.

GENEHACK/Git-Wrapper-0.045.tar.gz[17/1516]/opt/plenv/versions/5.23.2/bin/perl5.23.2Makefile.PL--OKRunningmakeforG/GE/GENEHACK/Git-Wrapper-0.045.tar.gzcplib/Git/Wrapper/File/RawModification.pmblib/lib/Git/Wrapper/File/RawModification.pmcplib/Git/Wrapper.pmblib/lib/Git/Wrapper.pmcplib/Git/Wrapper/Statuses.pmblib/lib/Git/Wrapper/Statuses.pmcplib/Git/Wrapper/Exception.pmblib/lib/Git/Wrapper/Exception.pmcplib/Git/Wrapper/Log.pmblib/lib/Git/Wrapper/Log.pmcplib/Git/Wrapper/Status.pmblib/lib/Git/Wrapper/Status.pmManifying6poddocumentsGENEHACK/Git-Wrapper-0.045.tar.gz/usr/bin/make--OKRunningmaketestPERL_DL_NONLAZY=1"/opt/plenv/versions/5.23.2/bin/perl5.23.2""-MExtUtils::Command::MM""-MTest::Harness""-e""undef*Test::Harness::Switches;test_harness(0,'blib/lib','blib/arch')"t/*.tt/00-load.t...............1/6#TestingGit::Wrapper0.045t/00-load.t...............okt/author-err.t............skipped:thesetestsarefortestingbytheauthort/basic.t.................#Testinggitversion:2.5.2t/basic.t.................okt/git_binary.t............okt/parse_args.t............okt/path_class.t............#Testinggitversion:2.5.2t/path_class.t............okt/release-pod-coverage.t..skipped:thesetestsareforreleasecandidatetestingt/release-pod-syntax.t....skipped:thesetestsareforreleasecandidatetestingAlltestssuccessful.Files=8,Tests=67,1wallclocksecs(0.04usr0.02sys+0.39cusr0.31csys=0.76CPU)Result:PASSGENEHACK/Git-Wrapper-0.045.tar.gz

and this is more of the output...

(still not done but I got tired of pasting.)

%cpanmGit::Wrapper-->WorkingonGit::WrapperFetchinghttp://www.cpan.org/authors/id/G/GE/GENEHACK/Git-Wrapper-0.045.tar.gz...OKConfiguringGit-Wrapper-0.045...OKBuildingandtestingGit-Wrapper-0.045...OKSuccessfullyinstalledGit-Wrapper-0.045

this is the output from cpanm installing the same thing

%cpanmGit::Wrapper-->WorkingonGit::WrapperFetchinghttp://www.cpan.org/authors/id/G/GE/GENEHACK/Git-Wrapper-0.045.tar.gz...OKConfiguringGit-Wrapper-0.045...OKBuildingandtestingGit-Wrapper-0.045...OKSuccessfullyinstalledGit-Wrapper-0.045

this is the output from cpanm installing the same thing

cpanminus a/k/a cpanm

carton

Another tool, carton, helps manage your module dependencies in a project. Particularly useful for large projects with multiple devs

Like bundler but for Perl

it's like ruby's bundler, but for perl modules

Freeze deps,so you always install same

versions across dev/staging/prod

❤ Miyagawa

both cpanm and carton were developed by Miyagawa; many thanks to him!

speaking of stuff on CPAN…

so, CPAN...

metacpan.org

we have a whole new website for interacting with CPAN

search.cpan.org

search.cpan.org is still around...

but metacpan integrates and visualizes a bunch of information in a really useful way

things like a syntax-highlighted source view, linking to home pages and code repos, showing test results, and the amount of activity in a project

https://github.com/CPAN-API/metacpan-web

it's also open source, so if you can think of a way to make it better, you can

❤ MetaCPAN team

thanks to the whole metacpan team for all their hard work

Duck Duck Go

we also have a new search engine here in 2016. it's cool, and it's partially written in Perl

Duck Duck Go !cpanm

The most useful feature, though, is ability to use 'bang searches' to restrict your search to a particular site - this is how you search metacpan

speaking of modules...

if you haven't been playing close attention to perl community goings on, there are a few new modules you may have missed

JSON::MaybeXS

anybody doing web development these days needs to interact with JSON - using JSON::MaybeXS will make sure that you have a JSON library available, picking the best one from a number of alternatives

Cpanel::JSON::XS JSON::XS JSON::PP

here's how the fallback works -- and if you install JSON::MaybeXS and don't have any of these installed, it will require Cpanel::JSON::XS (as long as you have a compiler to build the XS)

Moose Moo

To get a handle on how we do OOP in Perl these days, you should look into Moose -- and then when you're ready to write some code, you'll probably be able to get away with dropping down to Moo

CGI.pm is

gone

I do have some bad news for you -- CGI.pm has been pulled out of core

(not really)

don't worry, you can still find it on CPAN

Plack

But the current standard for web development in Perl is Plack/PSGI. Offers a number of advantages over CGI, and is the basis for all modern Perl web frameworks

speaking of Perl websites…

we have quite a few new websites these days, which make it easier to keep up with the current state of things

http://cpanratings.perl.org/

one of the problems with cpan is there's just _so_ _much_ _stuff_ there. it can be hard to decide which one of a dozen different modules to use. cpanratings helps with this problem

http://cpanratings.perl.org/

you can see individual reviews, which version they're reviewing, and so on. one downside - because ratings are about a particular version, may not reflect current module state

https://metacpan.org/

MetaCPAN links to reviews as well as showing an average review score

http://cpants.cpanauthors.org/

we also have CPANTS - automated testing of some best practices around module development

http://cpants.cpanauthors.org/

here's what that looks like for a particular module. super useful if you're not sure you're doing things the "right" way

http://cpants.cpanauthors.org/author/GENEHACK

can also see modules by author, which can be useful when trying to decide whether to use somebody's code

https://metacpan.org/

MetaCPAN links to CPANTS too

http://prepan.org/

We also have PrePAN, which is a place to get feedback on module ideas you haven't even written yet

Perl 5 Porters mailing list summaries!

In a recent development, sawyer has revived the p5p weekly email summary -- excellent if you want to keep up with what's going on with perl5 development but don't have time to follow the email list yourself

http://blogs.perl.org/users/sawyer_x/

and in a recent development, sawyer has revived the p5p weekly email summary -- excellent if you want to keep up with what's going on with perl5 development but don't have time to follow the email list yourself

speaking of Perl community…

finally, you really should consider becoming more actively involved with the perl community, if you're working with (or even just playing with) perl

irc.perl.org

irc.perl.org had a *bit* of a bad reputation </understatement> but things have been cleaned up a bunch. there's a documented standards of conduct, abuse is not tolerated, and it's a good place to get help from experienced perl folk

Conferences & Workshops& Mongers

(oh my.)

big conferences: YAPC, once a year (or so), in North America, Europe, Asia. Sometimes Brazil, Russia too

workshops: smaller, regional. Pittsburgh, DC-Baltimore, Orlando

perl mongers groups: like MasterCard, we *errywhere* you want to be

http://www.pm.org/groups/north_america.html

finally, you really should consider becoming more actively involved with the perl community, if you're working with (or even just playing with) perl, there's probably a local perl mongers group near you

http://saltlake.pm.org/

here's the SLC one!

http://perlweekly.com/

finally, let me give a big shout out to perl weekly, which is a once a week email newsletter and website aggregating perl related news from all over the web

if you're only going to pay attention to one perl thing, perl weekly is your best choice! sign up at perlweekly.com

thanks!

thanks to the organizers for accepting my talk, thanks to all of you for attending and participating ...

thanks to my employer for giving me the time to write this talk and sending me here to deliver it

questions?

https://joind.in/event/openwest-2016/modern-perl-for-the-unfrozen-paleolithic-perl-programmer

http://tinyurl.com/unfrozen-perl

any questions? (mention feedback link)

top related