Professional Documents
Culture Documents
Dancer2 Odp
Dancer2 Odp
Prasad
What is a Web framework?
● Install cpanminus,
#curl -L https://cpanmin.us | perl - --sudo
App::cpanminus
● Update cpanminus,
#cpanm --self-upgrade –sudo
● Installing Dancer2,
#curl -L http://cpanmin.us | perl
- --sudo Dancer2
Dancer Application
#!/usr/bin/env perl
use Dancer2;
get '/' => sub {
"Hello World!"
};
dance;
Static Patterns contd..
#!/usr/bin/env perl
use Dancer2;
get '/hello' => sub {
"Hello World!"
};
dance;
Patterns (Named Tokens)
#!/usr/bin/perl
use Dancer2;
get '/hello/:name' => sub {
return "Why, hello there " . params->{name};
};
dance;
Patterns (Anonymous Tokens)
#! /usr/bin/env perl
use Dancer2;
get '/hello/*.*' => sub {
return "Why, hello there "
};
dance;
Patterns (Regex)
#! /usr/bin/env perl
use Dancer2;
get qr{/quello/([\w]+)} => sub {
my($name)=splat;
return "Hello $name";
};
dance;
Patterns (Regex)
#! /usr/bin/env perl
use Dancer2;
get qr{/hello/(\d+)-(\d+)-(\d+)} => sub {
my($d,$m,$y)=splat;
return "Hello $d/$m/$y";
};
dance;
●
Creating a Dancer Project
$ dancer2 -a MyWeb::App
● MyWeb::App/bin/app.psgi (A standalone
Server)
To run,
$ plackup -r MyWeb::App/bin/app.psgi
Description
● MyWeb::App/views/index.tt
● MyWeb::App/views/layouts/main.tt
● MyWeb::App/public/javascripts/jquery.js
● MyWeb::App/public/css/style.css
● MyWeb::App/public/css/error.css
● MyWeb::App/public/images/...
Javascript (dancer2 ships with jsquery)
Cascade style sheets
Images (for default design)
Description
● MyWeb::App/public/500.html
● MyWeb::App/public/404.html
(Pages for 500 and 404 errors)
● MyWeb::App/public/dispatch.fcgi
● MyWeb::App/public/dispatch.cgi
(Wrappers to cofigure fast-cgi & cgi backends)
Description
● MyWeb::App/MakeFile.PL
(Main module MakeFile, useful to make module of
your application)
● MyWeb::App/t/002_index_route.t
● MyWeb::App/t/001_base.t
(test suite)
● MyWeb::App/lib/MyWeb-App.pm (application)
ROUTES
The following will match any HTTP request to the path /myaction:
any '/myaction' => sub {
# code
}
GET/POST
Views
It's possible to render the action's content with a template,
this is called a view. The `appdir/views' directory is the place
where views are located.
You can change this location by changing the setting 'views'.
By default, the internal template engine
Dancer::Template::Simple is used, but you may want to
upgrade to Template::Toolkit. If you do so, you have to enable
this engine in your settings as explained in
Dancer::Template::TemplateToolkit. If you do so, you'll also
have to import the Template module in your application code
Using templates - views and layouts
<html>
<head>...</head>
<body>
<div id="header">
...
</div>
<div id="content">
</div>
</body>
</html>
Layout
If you use Plack and have some unicode problem with your
Dancer application, don't forget to check if you have set your
template engine to use unicode, and set the default charset to
UTF-8. So, if you are using template toolkit, your config.yml
will look like this:
charset: UTF-8
engines:
template_toolkit:
ENCODING: utf8
Configuration Option
In Dancer2, static files should go into the public/ directory, but in the
application itself be sure to omit the public/ element from the path. For
example, the stylesheet for Dancr lives in
dancr/public/css/style.css but is served from
http://localhost:3000/css/style.css.
If you wanted to build a mostly static web site you could simply write
route handlers like this one:
get '/' => sub {
send_file 'index.html';
};
where index.html would live in your public/ directory.
send_file does exactly what it says: it loads a static file, then sends
the contents of that file to the user.
Writing a REST application