low-maintenance perl

Post on 18-Nov-2014

377 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

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.

TRANSCRIPT

low maintenance perl“optimizing for an easy life”

perrin harkinsplus three

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

“perl is unmaintainable.”

“perl is write­only.”

the fud

i shouldn't have to tell you this

use strict;

use warnings;

perltidy

know your audience

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

andy hunt

choosing a dialect

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

choosing a dialect

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

choosing a dialect

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

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;

choosing a dialect

follow conventions when you can.

choosing a dialect

which scans faster?

s{foo}{bar}g;

s/foo/bar/g;

choosing a dialect

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

choosing a dialect

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

perlmonks, i probably should rethink my design."

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

obscure features

example of a changing feature:

my $foo = 1 if $bar;

never

formats

never

punctuation variables

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

never

import functions that don't import

use Catalyst qw/­Debug/;

use Catalyst ();

never

function prototypes

sub do_it (&@) {  blah blah blah}

never

indirect object syntax

new Class    #no

Class­>new() #yes

never

UNIVERSAL::

never

alternative inheritance schemes

never

re­blessing existing objects

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

never

objects that aren't hashes

(but maybe Object::InsideOut)

never

overloading

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')) {

never

multiple packages in one file

never

source filters

never

the constant pragma

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

never

tied variables

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

$h{'foo'} = 1;

rarely

DESTROY methods

rarely

weak references

rarely

AUTOLOAD

rarely

wantarray

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

sometimes

closures

sometimes

string eval

sometimes

sub attributes

sub foo : attribute {

sometimes

code references

sometimes

exported subs

sometimes

chained map/grep

sometimes

ternary operator

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

sometimes

$_

questions you might have

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

questions you might have

won't this make your code longer?

questions you might have

but AUTOLOAD is awesome!

questions you might have

why don't you just use Java?

beyond the code

● configuration management● version control with branches

beyond the code

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

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

thank you!

top related