Dancing Tutorial - Dancer - Perl Web Framework for easy...
Transcript of Dancing Tutorial - Dancer - Perl Web Framework for easy...
![Page 1: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/1.jpg)
Dancing Tutorial
Alberto Manuel Brandao [email protected]
Braga Geek NightsMarch 1st
Alberto Simoes Dancing Tutorial
![Page 2: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/2.jpg)
Part 0: Ballet
Alberto Simoes Dancing Tutorial
![Page 3: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/3.jpg)
What’s Dancer?
![Page 4: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/4.jpg)
A micro frameworkfor writing web applications
![Page 5: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/5.jpg)
So, Yet Another Web Framework?
![Page 6: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/6.jpg)
Well, yes!
![Page 7: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/7.jpg)
CGI.pm webapps are spaghetti
![Page 8: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/8.jpg)
Catalyst is HUGE
![Page 9: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/9.jpg)
Ruby introduced something different
![Page 10: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/10.jpg)
![Page 11: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/11.jpg)
We needed something similar in Perl
![Page 12: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/12.jpg)
Definitely
![Page 13: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/13.jpg)
And they also imitated us!
![Page 15: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/15.jpg)
Part 1: Tango
Alberto Simoes Dancing Tutorial
![Page 16: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/16.jpg)
Installing
� �$ sudo cpan Dancer� �
or� �$ sudo cpan Task : : Dancer� �
or yet� �$ cpanm −S Task : : Dancer� �
Alberto Simoes Dancing Tutorial
![Page 17: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/17.jpg)
Bootstrap application� �$ dancer −a MyApp
+ MyApp/bin/app . pl+ MyApp/config . yml+ MyApp/environments/development . yml+ MyApp/environments/production . yml+ MyApp/views/index . tt+ MyApp/views/layouts/main . tt+ MyApp/lib/MyApp . pm+ MyApp/public/javascripts/jquery . js+ MyApp/public/css/style . css+ MyApp/public/css/error . css+ MyApp/public/images / . . .+ MyApp/public /500 . html+ MyApp/public /404 . html+ MyApp/public/dispatch . fcgi+ MyApp/public/dispatch . cgi+ MyApp/Makefile . PL+ MyApp/t/002 _index_route . t+ MyApp/t/001 _base . t� �
Alberto Simoes Dancing Tutorial
![Page 18: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/18.jpg)
What’s inside
� �+ MyApp/bin/app . pl� �a standalone light server (starts server in port 3000);
also used for fast-cgi or common cgi backends;
� �+ MyApp/config . yml+ MyApp/environments/development . yml+ MyApp/environments/production . yml� �main configuration file (plugins, modules, etc);
configuration files for production and development:
defines what to report, where to report, etc.
Alberto Simoes Dancing Tutorial
![Page 19: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/19.jpg)
What’s inside
� �+ MyApp/bin/app . pl� �a standalone light server (starts server in port 3000);
also used for fast-cgi or common cgi backends;
� �+ MyApp/config . yml+ MyApp/environments/development . yml+ MyApp/environments/production . yml� �main configuration file (plugins, modules, etc);
configuration files for production and development:
defines what to report, where to report, etc.
Alberto Simoes Dancing Tutorial
![Page 20: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/20.jpg)
What’s inside� �+ MyApp/views/index . tt+ MyApp/views/layouts/main . tt� �Templates and layouts:
templates are page portions/scraps;layouts are full page designs(they are automagically filled by the template);� �
+ MyApp/public/javascripts/jquery . js+ MyApp/public/css/style . css+ MyApp/public/css/error . css+ MyApp/public/images / . . .� �public/static files:
javascript (Dancer ships with jsquery);cascade style sheets;images (for default design);
Alberto Simoes Dancing Tutorial
![Page 21: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/21.jpg)
What’s inside� �+ MyApp/views/index . tt+ MyApp/views/layouts/main . tt� �Templates and layouts:
templates are page portions/scraps;layouts are full page designs(they are automagically filled by the template);� �
+ MyApp/public/javascripts/jquery . js+ MyApp/public/css/style . css+ MyApp/public/css/error . css+ MyApp/public/images / . . .� �public/static files:
javascript (Dancer ships with jsquery);cascade style sheets;images (for default design);
Alberto Simoes Dancing Tutorial
![Page 22: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/22.jpg)
What’s inside
� �+ MyApp/public /500 . html+ MyApp/public /404 . html� �pages for 500 and 404 errors;
� �+ MyApp/public/dispatch . fcgi+ MyApp/public/dispatch . cgi� �wrappers to configure fast-cgi and cgi backends;
will be back on this later (future talk?);
Alberto Simoes Dancing Tutorial
![Page 23: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/23.jpg)
What’s inside
� �+ MyApp/public /500 . html+ MyApp/public /404 . html� �pages for 500 and 404 errors;
� �+ MyApp/public/dispatch . fcgi+ MyApp/public/dispatch . cgi� �wrappers to configure fast-cgi and cgi backends;
will be back on this later (future talk?);
Alberto Simoes Dancing Tutorial
![Page 24: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/24.jpg)
What’s inside
� �+ MyApp/Makefile . PL+ MyApp/t/002 _index_route . t+ MyApp/t/001 _base . t� �Main module Makefile:
usefull to make module of your application
Your test suite;
� �+ MyApp/lib/MyApp . pm� �Your application!
Alberto Simoes Dancing Tutorial
![Page 25: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/25.jpg)
What’s inside
� �+ MyApp/Makefile . PL+ MyApp/t/002 _index_route . t+ MyApp/t/001 _base . t� �Main module Makefile:
usefull to make module of your application
Your test suite;
� �+ MyApp/lib/MyApp . pm� �Your application!
Alberto Simoes Dancing Tutorial
![Page 26: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/26.jpg)
Part 2: Flamenco
Alberto Simoes Dancing Tutorial
![Page 27: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/27.jpg)
Traffic Control
Your MyApp/lib/MyApp.pm includes:� �package MyApp ;use Dancer ’ : s yn t ax ’ ;
our $VERSION = ’ 0 .1 ’ ;
get ’ / ’ => sub {template ’ i n d e x ’ ;
} ;
true ;� �
Alberto Simoes Dancing Tutorial
![Page 28: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/28.jpg)
How to test it?
Start the standalone server,� �[ ambs@rachmaninoff MyApp ] $ bin/app . pl� �that shows debug info,� �[ 9 3 87 ] core @0 .000017> loading Dancer : : Handler : : Standalonehandler in /opt/lib/perl5/site_perl /5 . 12 . 3/ Dancer/Handler . pm l . 39[ 9 387 ] core @0 .000442> loading handler ’ Dancer : : Handler : :Standalone ’ in /opt/ l o c a l /lib/perl5/site_perl /5 . 12 . 3/ Dancer . pm l . 230>> Dancer 1 .3011 server 9387 listening on http : / / 0 . 0 . 0 . 0 : 3 0 0 0\subsection{ Entering the development dance floor . . . }� �and open a browser in the specified port.
Alberto Simoes Dancing Tutorial
![Page 29: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/29.jpg)
It Works!
![Page 30: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/30.jpg)
Route Handlers
A Dancer app is a collection of route handlers;
A route handler is, basically, a sub;
It is bound to an http method;
And to a path or a path pattern;
Example
get ’ / ’ => sub { . . . } ;
post ’ / submit / : f i l e ’ => sub { . . . } ;
del ’ / r e s o u r c e /∗ ’ => sub { . . . } ;
Alberto Simoes Dancing Tutorial
![Page 31: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/31.jpg)
Route Handlers
A Dancer app is a collection of route handlers;
A route handler is, basically, a sub;
It is bound to an http method;
And to a path or a path pattern;
Example
get ’ / ’ => sub { . . . } ;
post ’ / submit / : f i l e ’ => sub { . . . } ;
del ’ / r e s o u r c e /∗ ’ => sub { . . . } ;
Alberto Simoes Dancing Tutorial
![Page 32: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/32.jpg)
Route Handlers
Static patterns (paths):� �get ’ / ’ => sub { . . . } ;
get ’ / about ’ => sub { . . . } ;� �Patterns with named tokens:� �
get ’ /book / : i d ’ => sub {# do someth ing wi th params−>{ i d }. . .
} ;
get ’ / u s e r / : group / : u i d ’ => sub {# use params−>{u id } and params−>{group } he r e} ;� �
Alberto Simoes Dancing Tutorial
![Page 33: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/33.jpg)
Route Handlers
Static patterns (paths):� �get ’ / ’ => sub { . . . } ;
get ’ / about ’ => sub { . . . } ;� �Patterns with named tokens:� �
get ’ /book / : i d ’ => sub {# do someth ing wi th params−>{ i d }. . .
} ;
get ’ / u s e r / : group / : u i d ’ => sub {# use params−>{u id } and params−>{group } he r e} ;� �
Alberto Simoes Dancing Tutorial
![Page 34: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/34.jpg)
Route Handlers
Patterns with anonymous tokens:� �get ’ / f i l e /∗ .∗ ’ => sub {
my ( $file , $ext ) = splat ;. . .
}
get ’ /show/∗/∗ ’ => sub {my ( $cat , $subcat ) = splat ;
} ;� �Regular expressions:� �
get qr{post /(\d+)−(\d+)−(\d+)} => sub {my ( $year , $month , $day ) = splat ;
}� �Alberto Simoes Dancing Tutorial
![Page 35: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/35.jpg)
Route Handlers
Patterns with anonymous tokens:� �get ’ / f i l e /∗ .∗ ’ => sub {
my ( $file , $ext ) = splat ;. . .
}
get ’ /show/∗/∗ ’ => sub {my ( $cat , $subcat ) = splat ;
} ;� �Regular expressions:� �
get qr{post /(\d+)−(\d+)−(\d+)} => sub {my ( $year , $month , $day ) = splat ;
}� �Alberto Simoes Dancing Tutorial
![Page 36: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/36.jpg)
Part 3: Mambo
Alberto Simoes Dancing Tutorial
![Page 37: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/37.jpg)
Templating
Dancer has plugins for most templating systems:
Mason, Template Toolkit and others.
Default is a Simple template system, built-in Dancer.
Use the template inside the route:� �get ’ / u s e r / : name ’ => sub {
template ’ p r o f i l e ’ => { username => params−>{name} } ;} ;� �
Alberto Simoes Dancing Tutorial
![Page 38: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/38.jpg)
Templating
Dancer has plugins for most templating systems:
Mason, Template Toolkit and others.
Default is a Simple template system, built-in Dancer.
Use the template inside the route:� �get ’ / u s e r / : name ’ => sub {
template ’ p r o f i l e ’ => { username => params−>{name} } ;} ;� �
Alberto Simoes Dancing Tutorial
![Page 39: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/39.jpg)
Rerouting
You can reroute by:
Passing the control to the next matching handler:� �get ’ / l a z y ’ => sub {
pass and r e t u r n false ;} ;� �
Redirecting to other URI:� �get ’ / f o r b i d d e n ’ => sub {
r e t u r n redirect ’ / b e t t e r / p l a c e ’} ;� �
Alberto Simoes Dancing Tutorial
![Page 40: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/40.jpg)
Rerouting
You can reroute by:
Passing the control to the next matching handler:� �get ’ / l a z y ’ => sub {
pass and r e t u r n false ;} ;� �
Redirecting to other URI:� �get ’ / f o r b i d d e n ’ => sub {
r e t u r n redirect ’ / b e t t e r / p l a c e ’} ;� �
Alberto Simoes Dancing Tutorial
![Page 41: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/41.jpg)
Rerouting
You can reroute by:
Passing the control to the next matching handler:� �get ’ / l a z y ’ => sub {
pass and r e t u r n false ;} ;� �
Redirecting to other URI:� �get ’ / f o r b i d d e n ’ => sub {
r e t u r n redirect ’ / b e t t e r / p l a c e ’} ;� �
Alberto Simoes Dancing Tutorial
![Page 42: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/42.jpg)
Serving Files
You can serve a static file:� �get ’ / dowload / : f i l e ’ => sub {
my $file = params−>{file } ;
pass and r e t u r n false u n l e s s −f $file ;
send_file $file ;} ;� �
If the content is generated, just change content-type:� �get ’ / readme . t x t ’ => sub {
content_type ’ t e x t / p l a i n ’ ;r e t u r n ’ t h i s i s p l a i n t e x t ’
} ;� �Alberto Simoes Dancing Tutorial
![Page 43: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/43.jpg)
Serving Files
You can serve a static file:� �get ’ / dowload / : f i l e ’ => sub {
my $file = params−>{file } ;
pass and r e t u r n false u n l e s s −f $file ;
send_file $file ;} ;� �
If the content is generated, just change content-type:� �get ’ / readme . t x t ’ => sub {
content_type ’ t e x t / p l a i n ’ ;r e t u r n ’ t h i s i s p l a i n t e x t ’
} ;� �Alberto Simoes Dancing Tutorial
![Page 44: Dancing Tutorial - Dancer - Perl Web Framework for easy ...perldancer.org/slides/dancing-tutorial.pdf · Dancing Tutorial Alberto Manuel Brand~ao Sim~oes ambs@perl.pt Braga Geek Nights](https://reader031.fdocuments.net/reader031/viewer/2022022507/5ac79d697f8b9a6b578b6659/html5/thumbnails/44.jpg)
Thanks to Alexis Sukrieh
(I stole some slides)