Dancer (the Effortless Perl Web Framework) About me Sawyer X - - PowerPoint PPT Presentation

dancer
SMART_READER_LITE
LIVE PREVIEW

Dancer (the Effortless Perl Web Framework) About me Sawyer X - - PowerPoint PPT Presentation

Dancer (the Effortless Perl Web Framework) 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/


slide-1
SLIDE 1

Dancer

(the Effortless Perl Web Framework)

slide-2
SLIDE 2

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/
slide-3
SLIDE 3

Perl web recap

1995 CGI

slide-4
SLIDE 4

Perl web recap

2010 Many frameworks

(including micro-frameworks like Dancer)

slide-5
SLIDE 5

The big web religions, illustrated

slide-6
SLIDE 6

Ruby – the fanboys

slide-7
SLIDE 7

Python – the sticklers

slide-8
SLIDE 8

PHP – the nonsensical

slide-9
SLIDE 9

Perl – the nutcases

slide-10
SLIDE 10

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
slide-11
SLIDE 11

Flask (Pythonese)

from flask import Flask app = Flask(__name__) @app.route("/", methods=['GET']) def hello(): return "Hello World!" if __name__ == "__main__": app.run()

slide-12
SLIDE 12

Dancer (Perlesque)

use Dancer; get “/hi” => sub { “Hello, World!” }; dance;

slide-13
SLIDE 13

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;

slide-14
SLIDE 14

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)
slide-15
SLIDE 15

Recipe for Dancing

  • Take an HTTP method
  • Add a path to that
  • Mix with a subroutine
  • And sprinkle plugins and keywords on top
slide-16
SLIDE 16

Dancer route structure

get '/path' => sub { … }; post '/path' => sub { … }; put '/path' => sub { … }; del '/path' => sub { … };

  • ptions '/path' => sub { … };

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

slide-17
SLIDE 17

Dancer

  • Paths can contain variables

get '/hello/:entity/'

  • Paths can be Regular Expressions

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

slide-18
SLIDE 18

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'; } };

slide-19
SLIDE 19

Templating

get '/' => sub { template index => { greeting => 'welcome' } };

slide-20
SLIDE 20

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
slide-21
SLIDE 21

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)
slide-22
SLIDE 22

Oh yeah, route caching...

slide-23
SLIDE 23

Dancer::Plugin::REST

get '/user/:id.:format' => sub { UserRS->find( params->{id} ); }; # curl http://mywebservice/user/42.json { "id": 42, "name": "John Foo", "email": "john.foo@hopkins.com" } # curl http://mywebservice/user/42.yml

  • id: 42

name: "John Foo" email: "john.foo@hopkins.com"

slide-24
SLIDE 24

Dancer::Plugin::SiteMap

use Dancer::Plugin::SiteMap;

  • You get: /sitemap and /sitemap.xml
  • “Yup, it's that simple.”
slide-25
SLIDE 25

Dancer::Plugin::Email

post '/contact' => sub { email { to => 'a@b.com', subject => 'Test', message => $msg, attach => [ path => 'name' ], } };

slide-26
SLIDE 26

Dancer::Plugin::Authorize

post '/login' => sub { my $user = params->{'user'}; my $pass = params->{'pass'}; if ( auth( $user, $pass ) ) { if ( auth_asa('guest') ) {...} if ( auth_can('create') ) {...} } };

slide-27
SLIDE 27

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 POST request
slide-28
SLIDE 28

Dancer::Plugin::DBIC

  • DBIC (DBIx::Class) – a sophisticated ORM
  • Configure the connection in the config file
  • Make the ResultSets available in routes
slide-29
SLIDE 29

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, }; };

slide-30
SLIDE 30

In culmination

Dancer is beautiful and fun The way programming should be PerlDancer.org search.cpan.org/perldoc?Dancer

slide-31
SLIDE 31