48
low maintenance perl “optimizing for an easy life” perrin harkins plus three

Low-Maintenance Perl

Embed Size (px)

DESCRIPTION

This talk was presented at OSCON 2006 and ApacheCon 2006. It suffers quite a bit from not having the commentary that went with the slides, but my notes for this talk are available on this site as a PDF. This talk was probably the most well-received OSCON talk I've ever done. There were a lot of jokes and people were rolling in the aisles. Larry Wall and Damian Conway attended the talk at OSCON and while they did argue a couple of points they mostly laughed along.

Citation preview

Page 1: Low-Maintenance Perl

low maintenance perl“optimizing for an easy life”

perrin harkinsplus three

Page 2: Low-Maintenance Perl

“i would never use perl for a large project.”

“perl is unmaintainable.”

“perl is write­only.”

the fud

Page 3: Low-Maintenance Perl

i shouldn't have to tell you this

use strict;

use warnings;

perltidy

Page 4: Low-Maintenance Perl

know your audience

“code is always read more times than it is written.”

andy hunt

Page 5: Low-Maintenance Perl

choosing a dialect

don't use something complex when something simple will work.

Page 6: Low-Maintenance Perl

choosing a dialect

don't do things in a magical way when an explicit way will work.

Page 7: Low-Maintenance Perl

choosing a dialect

don't make your code complex just so you can get a certain syntax.

Page 8: Low-Maintenance Perl

choosing a dialect

fetch("search.cpan.org") > \my @cont;

is equivalent to

my $scraper = FEAR::API­>fear();my $page = $scraper­>fetch("search.cpan.org");push my @cont, $page­>document­>as_string;

Page 9: Low-Maintenance Perl

choosing a dialect

follow conventions when you can.

Page 10: Low-Maintenance Perl

choosing a dialect

which scans faster?

s{foo}{bar}g;

s/foo/bar/g;

Page 11: Low-Maintenance Perl

choosing a dialect

don't use an obscure language feature when a common one will work.

Page 12: Low-Maintenance Perl

choosing a dialect

dragonchild's law:"if i have to ask if something is possible on 

perlmonks, i probably should rethink my design."

Page 13: Low-Maintenance Perl

obscure features

“We redesigned the protocol several times until we had a protocol that performed well. However, 

the resulting protocol was too complex and depended on the behavior of Chubby features that were seldom exercised by 

other applications. We discovered that we were spending an inordinate amount of time debugging obscure corner cases, 

not only in Bigtable code, but also in Chubby code. Eventually, we scrapped this protocol and moved to a newer simpler 

protocol that depends solely on widely­used Chubby features.”Google Bigtable Paper

Page 14: Low-Maintenance Perl

obscure features

example of a changing feature:

my $foo = 1 if $bar;

Page 15: Low-Maintenance Perl

never

formats

Page 16: Low-Maintenance Perl

never

punctuation variables

my $text = do { local $/ = undef; <$fh>; };

Page 17: Low-Maintenance Perl

never

import functions that don't import

use Catalyst qw/­Debug/;

use Catalyst ();

Page 18: Low-Maintenance Perl

never

function prototypes

sub do_it (&@) {  blah blah blah}

Page 19: Low-Maintenance Perl

never

indirect object syntax

new Class    #no

Class­>new() #yes

Page 20: Low-Maintenance Perl

never

UNIVERSAL::

Page 21: Low-Maintenance Perl

never

alternative inheritance schemes

Page 22: Low-Maintenance Perl

never

re­blessing existing objects

bless, $object, 'Some::Other::Class';

Page 23: Low-Maintenance Perl

never

objects that aren't hashes

(but maybe Object::InsideOut)

Page 24: Low-Maintenance Perl

never

overloading

Page 25: Low-Maintenance Perl

overloading

use Exception::Class qw(MyProject::BadKarma);

  # in some method, the exception is triggeredMyProject::BadKarma­>throw();  # in the caller, we catch it with evalif ($@ and $@­>isa('MyProject::BadKarma')) {

Page 26: Low-Maintenance Perl

never

multiple packages in one file

Page 27: Low-Maintenance Perl

never

source filters

Page 28: Low-Maintenance Perl

never

the constant pragma

use constant TEA => 'Darjeeling';%beverages = (TEA => 1);  our $TEA = 'Darjeeling';%beverages = ($TEA => 1);

Page 29: Low-Maintenance Perl

never

tied variables

tie(%h,’SDBM_File’, ’filename’,...);

$h{'foo'} = 1;

Page 30: Low-Maintenance Perl

rarely

DESTROY methods

Page 31: Low-Maintenance Perl

rarely

weak references

Page 32: Low-Maintenance Perl

rarely

AUTOLOAD

Page 33: Low-Maintenance Perl

rarely

wantarray

@books = Book­>search(author => $author)    || die "book not found";

Page 34: Low-Maintenance Perl

sometimes

closures

Page 35: Low-Maintenance Perl

sometimes

string eval

Page 36: Low-Maintenance Perl

sometimes

sub attributes

sub foo : attribute {

Page 37: Low-Maintenance Perl

sometimes

code references

Page 38: Low-Maintenance Perl

sometimes

exported subs

Page 39: Low-Maintenance Perl

sometimes

chained map/grep

Page 40: Low-Maintenance Perl

sometimes

ternary operator

my $foo = $bar ? 'yes' : 'no';

Page 41: Low-Maintenance Perl

sometimes

$_

Page 42: Low-Maintenance Perl

questions you might have

doesn't this take all the fun out of programming?

Page 43: Low-Maintenance Perl

questions you might have

won't this make your code longer?

Page 44: Low-Maintenance Perl

questions you might have

but AUTOLOAD is awesome!

Page 45: Low-Maintenance Perl

questions you might have

why don't you just use Java?

Page 46: Low-Maintenance Perl

beyond the code

● configuration management● version control with branches

Page 47: Low-Maintenance Perl

beyond the code

● tests can save your life● Test::Class can save your tests● smolder, 

http://sourceforge.net/projects/smolder/

Page 48: Low-Maintenance Perl

thank you!