perldancer for perlers (fosdem 2011)

36
Dancer (the Effortless Perl Web Framework)

Upload: xsawyer

Post on 08-May-2015

5.934 views

Category:

Technology


4 download

DESCRIPTION

This are the slides for the Dancer talk I gave at the Perl devroom at FOSDEM 2011.It is more up-to-date than the Pythoners version.

TRANSCRIPT

Page 1: PerlDancer for Perlers (FOSDEM 2011)

Dancer (the Effortless Perl Web Framework)

Page 2: PerlDancer for Perlers (FOSDEM 2011)

About me

● Sawyer X

● Sysadmin / Perl Ninja

● Israel.pm / Haifa.pm / TelAviv.pm / Rehovot.pm

● I do system, networking, web, applications, etc.

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

● http://search.cpan.org/~xsawyerx/

Page 3: PerlDancer for Perlers (FOSDEM 2011)

Perl web recap

1995

CGI

Page 4: PerlDancer for Perlers (FOSDEM 2011)

Perl web recap

2010

Many frameworks (including micro-frameworks like Dancer)

credit to Dave Rolsky for this intro

Page 5: PerlDancer for Perlers (FOSDEM 2011)

The big web religions, illustrated

Page 6: PerlDancer for Perlers (FOSDEM 2011)

Ruby – the fanboys

Page 7: PerlDancer for Perlers (FOSDEM 2011)

Python – the sticklers

Page 8: PerlDancer for Perlers (FOSDEM 2011)

PHP – the nonsensical

Page 9: PerlDancer for Perlers (FOSDEM 2011)

Perl – the nutcases

Page 10: PerlDancer for Perlers (FOSDEM 2011)

Nutcases?

● Yes, we are insane (but not LISP-insane)

● Insanity is a whole lot of fun!

● Insanity gives us flexibility

● Flexibility gives us cool stuff

● Like Moose and meta-programming

● Like DBIx::Class

● Like Dancer

Page 11: PerlDancer for Perlers (FOSDEM 2011)

Flask (Pythonese)

from flask import Flask

app = Flask(__name__)

@app.route("/", methods=['GET'])

def hello():

    return "Hello World!"

if __name__ == "__main__":

    app.run()

Page 12: PerlDancer for Perlers (FOSDEM 2011)

Dancer (Perlesque)

use Dancer;

get “/” => sub {

    “Hello, World!”

};

dance;

Page 13: PerlDancer for Perlers (FOSDEM 2011)

In comparison

from flask import Flask

app = Flask(__name__)

@app.route("/", methods=['GET'])

def hello():

    return "Hello World!"

if __name__ == "__main__":

    app.run()

use Dancer;

get “/” => sub {

  “Hello, World!”

};

dance;

Page 14: PerlDancer for Perlers (FOSDEM 2011)

Web development models

Page 15: PerlDancer for Perlers (FOSDEM 2011)

CGI

● Most common web programming model

● Easy to configure on a web server

● Programmer gets everything in %ENV

● First paragraph in output is for the browser

● Second paragraph is content for the browser

● Error prone, depressing, boring, long

Page 16: PerlDancer for Perlers (FOSDEM 2011)

MVC

● Separation of Model, View, Controller

● Much more flexible and scalable

● Uses “Namespace Matching”

● Sometimes an over-kill

● MVC framework example: Catalyst

Page 17: PerlDancer for Perlers (FOSDEM 2011)

Route dispatching

● Succinct

● Clean

● Lightweight

● MVC-ish

● Less enterprisey

● Example: Dancer

Page 18: PerlDancer for Perlers (FOSDEM 2011)

Dancer treats

● Both read and write, easily!

● Route-based (started as a port of Sinatra)

● PSGI/Plack compliant

● Minimal dependencies

● Any app is also a web server

● CPAN-friendly (<3 CPAN)

Page 19: PerlDancer for Perlers (FOSDEM 2011)

Recipe for Dancing

● Take an HTTP method

● Add a path to that

● Mix with a subroutine

● And sprinkle plugins and keywords on top

Page 20: PerlDancer for Perlers (FOSDEM 2011)

Dancer route structure

get     '/path' => sub { … };

post    '/path' => sub { … };

put     '/path' => sub { … };

del     '/path' => sub { … };

options '/path' => sub { … };

any     '/path' => sub { … };

Page 21: PerlDancer for Perlers (FOSDEM 2011)

Dancer

● Paths can contain variables

get '/hello/:entity/'

● Support optional variables

get '/hello/:entity?'

● Paths can be Regular Expressions

get qr{/ (\w+) / \d{2,3} (.+)? }x

Page 22: PerlDancer for Perlers (FOSDEM 2011)

Dancer login example

post '/login' => sub {

    # Validate the username and password

    if ( params­>{user} eq 'bob' &&

         params­>{pass} eq 'LetMeIn' ) {

        session user => params­>{user};

        redirect params­>{path} || '/';

    } else {

        redirect '/login?failed=1';

    }

 };

Page 23: PerlDancer for Perlers (FOSDEM 2011)

Templating

get '/' => sub {

    template index => {

        greeting => 'welcome'

    }

};

Page 24: PerlDancer for Perlers (FOSDEM 2011)

More nifty stuff

● headers 'My­X­Header' => 'Value'

● send_file('report.tar.gz')

● set_cookie name    => 'value',

           expires => ( time + 3600 ),

           domain  => 'foo.com'

● status 'not_found'

● to_json, to_yaml, to_xml

● my $file        = upload('file_input')

● my $all_uploads = request­>uploads

Page 25: PerlDancer for Perlers (FOSDEM 2011)

Dancer as Perl philosophy

● Dancer is succinct, efficient and easy to work with

● Dancer is daring

(Probably the only web framework with route caching)

(Websockets in the works!)

● Dancer has a lot of plugins:

(engines for sessions, logging, templates)

● Serializers (JSON, YAML, XML)

● Route filters (before, after, before_template)

Page 26: PerlDancer for Perlers (FOSDEM 2011)

Oh yeah, route caching...

Page 27: PerlDancer for Perlers (FOSDEM 2011)

Dancer::Plugin::REST

get '/user/:id.:format' => sub {

    UserRS­>find( params­>{id} );

};

# curl http://mywebservice/user/42.json

{ "id": 42, "name": "John Foo",             "email": "[email protected]" }

# curl http://mywebservice/user/42.yml

­­

id: 42

name: "John Foo"

email: "[email protected]"

Page 28: PerlDancer for Perlers (FOSDEM 2011)

Dancer::Plugin::SiteMap

use Dancer::Plugin::SiteMap;

● You get: /sitemap and /sitemap.xml

● “ Yup, it's that simple.”

Page 29: PerlDancer for Perlers (FOSDEM 2011)

Dancer::Plugin::Email

post '/contact' => sub {

    email {

        to      => '[email protected]',

        subject => 'Test',

        message => $msg,

        attach  => [ path => 'name' ],

    }

};

Page 30: PerlDancer for Perlers (FOSDEM 2011)

Dancer::Plugin::Auth::RBAC

post '/login' => sub {

    my $user = params­>{'user'};

    my $pass = params­>{'pass'};

    if ( auth( $user, $pass ) ) {

        if ( auth_asa('guest')  ) {...}

        if ( auth_can('create') ) {...}

    }

};

Page 31: PerlDancer for Perlers (FOSDEM 2011)

Dancer::Plugin::Ajax

ajax '/check_for_update' => sub {

    # some ajax code

};

● Pass if X-Request-With not “XMLHttpRequest”

● Disable the layout

● The action built is a POSTrequest

Page 32: PerlDancer for Perlers (FOSDEM 2011)

Dancer::Plugin::DBIC

● DBIC (DBIx::Class) – a sophisticated ORM

● Configure the connection in the config file

● Make the ResultSets available in routes

Page 33: PerlDancer for Perlers (FOSDEM 2011)

Dancer::Plugin::Database

● Database(s) connection in Dancer

get '/widget/view/:id' => sub {

   my $sth = database­>prepare(

        'select * from widgets where id = ?'

   );

   $sth­>execute( params­>{id} );

   template display_widget => {

       widget => $sth­>fetchrow_hashref,

   };

};

Page 34: PerlDancer for Perlers (FOSDEM 2011)

Even more plugins

● Dancer::Plugin::WebSocket

● Dancer::Plugin::FlashMessage

● Dancer::Plugin::Auth::Twitter

● Dancer::Plugin::MobileDevice

● Dancer::Plugin::Feed

● Dancer::Plugin::Redis

Page 35: PerlDancer for Perlers (FOSDEM 2011)

In culmination Dancer is beautiful and fun

The way programming should be

PerlDancer.org

search.cpan.org/perldoc?Dancer

http://advent.perldancer.org/2010

(http://advent.perldancer.org/2010/2)

Page 36: PerlDancer for Perlers (FOSDEM 2011)

Thank you!