SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Downloaden Sie, um offline zu lesen
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/
●   http://search.cpan.org/~xsawyerx/
Perl web recap




    1995
     CGI
Perl web recap



                   2010
    Many frameworks
(including micro-frameworks like Dancer)
           credit to Dave Rolsky for this intro
The big web religions, illustrated
Ruby – the fanboys
Python – the sticklers
PHP – the nonsensical
Perl – the nutcases
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
Flask (Pythonese)
from flask import Flask
app = Flask(__name__)


@app.route("/", methods=['GET'])
def hello():
    return "Hello World!"


if __name__ == "__main__":
    app.run()
Dancer (Perlesque)
use Dancer;


get “/” => sub {
    “Hello, World!”
};


dance;
In comparison
from flask import Flask            use Dancer;
app = Flask(__name__)


@app.route("/", methods=['GET'])   get “/” => sub {
def hello():                         “Hello, World!”
    return "Hello World!"
                                   };
if __name__ == "__main__":
    app.run()                      dance;
Web development models
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
MVC
●   Separation of Model, View, Controller
●   Much more flexible and scalable
●   Uses “Namespace Matching”
●   Sometimes an over-kill
●   MVC framework example: Catalyst
Route dispatching
●   Succinct
●   Clean
●   Lightweight
●   MVC-ish
●   Less enterprisey
●   Example: Dancer
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)
Recipe for Dancing
●   Take an HTTP method
●   Add a path to that
●   Mix with a subroutine
●   And sprinkle plugins and keywords on top
Dancer route structure
get     '/path' => sub { … };
post    '/path' => sub { … };
put     '/path' => sub { … };
del     '/path' => sub { … };
options '/path' => sub { … };
any     '/path' => sub { … };
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
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';
    }
 };
Templating
get '/' => sub {
    template index => {
        greeting => 'welcome'
    }
};
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
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)
Oh yeah, route caching...
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"
Dancer::Plugin::SiteMap
    use Dancer::Plugin::SiteMap;


●   You get: /sitemap and /sitemap.xml
●   “Yup, it's that simple.”
Dancer::Plugin::Email
post '/contact' => sub {
    email {
        to      => 'a@b.com',
        subject => 'Test',
        message => $msg,
        attach  => [ path => 'name' ],
    }
};
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') ) {...}
    }
};
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
Dancer::Plugin::DBIC
●   DBIC (DBIx::Class) – a sophisticated ORM
●   Configure the connection in the config file
●   Make the ResultSets available in routes
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,
       };
    };
Even more plugins
●   Dancer::Plugin::WebSocket
●   Dancer::Plugin::FlashMessage
●   Dancer::Plugin::Auth::Twitter
●   Dancer::Plugin::MobileDevice
●   Dancer::Plugin::Feed
●   Dancer::Plugin::Redis
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)
Thank you!

Weitere ähnliche Inhalte

Was ist angesagt?

Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
diego_k
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
Jeremy Kendall
 
優しいWAFの作り方
優しいWAFの作り方優しいWAFの作り方
優しいWAFの作り方
techmemo
 

Was ist angesagt? (20)

RESTful web services
RESTful web servicesRESTful web services
RESTful web services
 
Developing apps using Perl
Developing apps using PerlDeveloping apps using Perl
Developing apps using Perl
 
Quality Use Of Plugin
Quality Use Of PluginQuality Use Of Plugin
Quality Use Of Plugin
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Slim RedBeanPHP and Knockout
Slim RedBeanPHP and KnockoutSlim RedBeanPHP and Knockout
Slim RedBeanPHP and Knockout
 
Mojo as a_client
Mojo as a_clientMojo as a_client
Mojo as a_client
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Keeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro frameworkKeeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
 
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro FrameworkKeeping it Small: Getting to know the Slim Micro Framework
Keeping it Small: Getting to know the Slim Micro Framework
 
Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101
 
Webrtc mojo
Webrtc mojoWebrtc mojo
Webrtc mojo
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
 
Go初心者がGoでコマンドラインツールの作成に挑戦した話
Go初心者がGoでコマンドラインツールの作成に挑戦した話Go初心者がGoでコマンドラインツールの作成に挑戦した話
Go初心者がGoでコマンドラインツールの作成に挑戦した話
 
Intro to Rails
Intro to Rails Intro to Rails
Intro to Rails
 
How to develop modern web application framework
How to develop modern web application frameworkHow to develop modern web application framework
How to develop modern web application framework
 
優しいWAFの作り方
優しいWAFの作り方優しいWAFの作り方
優しいWAFの作り方
 
Modern Perl Web Development with Dancer
Modern Perl Web Development with DancerModern Perl Web Development with Dancer
Modern Perl Web Development with Dancer
 
Lightweight Webservices with Sinatra and RestClient
Lightweight Webservices with Sinatra and RestClientLightweight Webservices with Sinatra and RestClient
Lightweight Webservices with Sinatra and RestClient
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009
 
Perl: Hate it for the Right Reasons
Perl: Hate it for the Right ReasonsPerl: Hate it for the Right Reasons
Perl: Hate it for the Right Reasons
 

Ähnlich wie PerlDancer for Perlers (FOSDEM 2011)

Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Tatsuhiko Miyagawa
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
Kar Juan
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
Ben Lin
 

Ähnlich wie PerlDancer for Perlers (FOSDEM 2011) (20)

Laravel5 Introduction and essentials
Laravel5 Introduction and essentialsLaravel5 Introduction and essentials
Laravel5 Introduction and essentials
 
PSGI and Plack from first principles
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
 
What's New In Laravel 5
What's New In Laravel 5What's New In Laravel 5
What's New In Laravel 5
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
 
How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server
 
Writing and Publishing Puppet Modules - PuppetConf 2014
Writing and Publishing Puppet Modules - PuppetConf 2014Writing and Publishing Puppet Modules - PuppetConf 2014
Writing and Publishing Puppet Modules - PuppetConf 2014
 
Using the new WordPress REST API
Using the new WordPress REST APIUsing the new WordPress REST API
Using the new WordPress REST API
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
Javascript laravel's friend
Javascript laravel's friendJavascript laravel's friend
Javascript laravel's friend
 
Using Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in RubyUsing Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in Ruby
 
SF Grails - Ratpack - Compact Groovy Webapps - James Williams
SF Grails - Ratpack - Compact Groovy Webapps - James WilliamsSF Grails - Ratpack - Compact Groovy Webapps - James Williams
SF Grails - Ratpack - Compact Groovy Webapps - James Williams
 
Sinatra for REST services
Sinatra for REST servicesSinatra for REST services
Sinatra for REST services
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
 
Modern php
Modern phpModern php
Modern php
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Express Presentation
Express PresentationExpress Presentation
Express Presentation
 
Intro to PSGI and Plack
Intro to PSGI and PlackIntro to PSGI and Plack
Intro to PSGI and Plack
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
 

Mehr von xSawyer

Mehr von xSawyer (13)

do_this and die();
do_this and die();do_this and die();
do_this and die();
 
XS Fun
XS FunXS Fun
XS Fun
 
Asynchronous Programming FTW! 2 (with AnyEvent)
Asynchronous Programming FTW! 2 (with AnyEvent)Asynchronous Programming FTW! 2 (with AnyEvent)
Asynchronous Programming FTW! 2 (with AnyEvent)
 
Asynchronous programming FTW!
Asynchronous programming FTW!Asynchronous programming FTW!
Asynchronous programming FTW!
 
Moose - YAPC::NA 2012
Moose - YAPC::NA 2012Moose - YAPC::NA 2012
Moose - YAPC::NA 2012
 
Our local state, my, my - Understanding Perl variables
Our local state, my, my - Understanding Perl variablesOur local state, my, my - Understanding Perl variables
Our local state, my, my - Understanding Perl variables
 
Your first website in under a minute with Dancer
Your first website in under a minute with DancerYour first website in under a minute with Dancer
Your first website in under a minute with Dancer
 
Moose talk at FOSDEM 2011 (Perl devroom)
Moose talk at FOSDEM 2011 (Perl devroom)Moose talk at FOSDEM 2011 (Perl devroom)
Moose talk at FOSDEM 2011 (Perl devroom)
 
When Perl Met Android (YAPC::EU 2010)
When Perl Met Android (YAPC::EU 2010)When Perl Met Android (YAPC::EU 2010)
When Perl Met Android (YAPC::EU 2010)
 
Perl Dancer on Android (first attempt)
Perl Dancer on Android (first attempt)Perl Dancer on Android (first attempt)
Perl Dancer on Android (first attempt)
 
Source Code Management systems
Source Code Management systemsSource Code Management systems
Source Code Management systems
 
Moose (Perl 5)
Moose (Perl 5)Moose (Perl 5)
Moose (Perl 5)
 
Red Flags in Programming
Red Flags in ProgrammingRed Flags in Programming
Red Flags in Programming
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

PerlDancer for Perlers (FOSDEM 2011)