perl dancer for python programmers

31
Dancer (the Effortless Perl Web Framework)

Upload: xsawyer

Post on 08-May-2015

22.436 views

Category:

Technology


1 download

DESCRIPTION

This is a talk given at PyWeb IL Oct 4th.

TRANSCRIPT

Page 1: Perl Dancer for Python programmers

Dancer(the Effortless Perl Web Framework)

Page 2: Perl Dancer for Python programmers

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: Perl Dancer for Python programmers

Perl web recap

1995CGI

Page 4: Perl Dancer for Python programmers

Perl web recap

2010Many frameworks

(including micro-frameworks like Dancer)

Page 5: Perl Dancer for Python programmers

The big web religions, illustrated

Page 6: Perl Dancer for Python programmers

Ruby – the fanboys

Page 7: Perl Dancer for Python programmers

Python – the sticklers

Page 8: Perl Dancer for Python programmers

PHP – the nonsensical

Page 9: Perl Dancer for Python programmers

Perl – the nutcases

Page 10: Perl Dancer for Python programmers

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: Perl Dancer for Python programmers

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: Perl Dancer for Python programmers

Dancer (Perlesque)use Dancer;

get “/hi” => sub {

    “Hello, World!”

};

dance;

Page 13: Perl Dancer for Python programmers

In comparisonfrom 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: Perl Dancer for Python programmers

Dancer treats● Both read and write, easily!● Route-based (started as a port of Sinatra)● PSGI/Plack compliant (PSGI is our WSGI)● Minimum dependencies● Any app is also a web server● CPAN-friendly (<3 CPAN)

Page 15: Perl Dancer for Python programmers

Recipe for Dancing● Take an HTTP method● Add a path to that● Mix with a subroutine● And sprinkle plugins and keywords on top

Page 16: Perl Dancer for Python programmers

Dancer route structureget     '/path' => sub { … };

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

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

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

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

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

Page 17: Perl Dancer for Python programmers

Dancer● Paths can contain variablesget '/hello/:entity/'

● Paths can be Regular Expressionsget qr{/ (\w+) / \d{2,3} (.+)? }x

Page 18: Perl Dancer for Python programmers

Dancer login examplepost '/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 19: Perl Dancer for Python programmers

Templatingget '/' => sub {

    template index => {

        greeting => 'welcome'

    }

};

Page 20: Perl Dancer for Python programmers

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 21: Perl Dancer for Python programmers

Dancer as Perl philosophy● Dancer is succinct, efficient and easy to work with● Dancer is daring

(Do you have route caching in Django?)

(Websockets in near future!)● Dancer has a lot of plugins:

(engines for sessions, logging, templates)● Serializers (JSON, YAML, XML)● Route filters (before, after, before_template)

Page 22: Perl Dancer for Python programmers

Oh yeah, route caching...

Page 23: Perl Dancer for Python programmers

Dancer::Plugin::RESTget '/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 24: Perl Dancer for Python programmers

Dancer::Plugin::SiteMapuse Dancer::Plugin::SiteMap;

● You get: /sitemap and /sitemap.xml● “Yup, it's that simple.”

Page 25: Perl Dancer for Python programmers

Dancer::Plugin::Emailpost '/contact' => sub {

    email {

        to      => '[email protected]',

        subject => 'Test',

        message => $msg,

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

    }

};

Page 26: Perl Dancer for Python programmers

Dancer::Plugin::Authorizepost '/login' => sub {

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

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

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

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

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

    }

};

Page 27: Perl Dancer for Python programmers

Dancer::Plugin::Ajaxajax '/check_for_update' => sub {

    # some ajax code

};

● Pass if X-Request-With not “XMLHttpRequest”● Disable the layout● The action built is a POST request

Page 28: Perl Dancer for Python programmers

Dancer::Plugin::DBIC● DBIC (DBIx::Class) – a sophisticated ORM● Configure the connection in the config file● Make the ResultSets available in routes

Page 29: Perl Dancer for Python programmers

Dancer::Plugin::Database● Database(s) connection in Dancerget '/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 30: Perl Dancer for Python programmers

In culminationDancer is beautiful and fun

The way programming should be

PerlDancer.orgsearch.cpan.org/perldoc?Dancer

Page 31: Perl Dancer for Python programmers