This talk provides a walkthrough of developing a simple web application with the Perl webapp framework Dancer. I gave this talk at the Dalhousie Faculty of Computer Science FreeSchool in March 2013
request containing some text to remember – Store it, and give the ID back – Respond to a GET for the ID with the paste content If you want to follow along: – Start installing dependencies: • cpanm --installdeps WWW::Hashbang::Pastebin – Grab the starter kit: http://mikedoherty.ca/t/dalfcs-dancer.tar.gz
autodie Moose cpanm, local::lib perlbrew Dist::Zilla rxrx Perl::Critic & Perl::Tidy DBIx::Class Plack Mojo/Dancer/Catalyst I'd love to tell you more about these in another session. http://mikedoherty.ca/t/dalfcs-dancer.tar.gz
$ (hostname ; uptime) | curl -F 'p=<-' <% request.uri_base %> <% request.uri_base %>/f4s2 $ chromium-browser <% request.uri_base %>/f4s2+#l2 DESCRIPTION This pastebin has no user interface - use "curl" to POST paste content. Your paste's ID is returned in the "X-Pastebin-ID" header; the URL in the "X-Pastebin-URL", as well as the response content. <a href="http://www.perl.org/">perl <% version %></a> WWW::Hashbang::Pastebin(3) </pre>
Dancer::Handler::Standalone handler in /home/mike/perl5/perlbrew/perls/demo-perl/lib/site_perl/5.16.2/ Dancer/Handler.pm l. 45 [13008] core @0.000219> loading handler 'Dancer::Handler::Standalone' in /home/mike/perl5/perlbrew/perls/demo-perl/lib/site_perl/5.16.2/ Dancer.pm l. 475 >> Dancer 1.3111 server 13008 listening on http://0.0.0.0:3000 >> Dancer::Plugin::DBIC (0.1802) == Entering the development dance floor ...
between RDBMSs Object-Relational Mapping (ORM) – Allows you to access DB rows as objects – Inflates relationships (foreign keys) into objects Write your own schema, or generate one from your DB
{ my $paste_content = param('p'); unless ($paste_content) { status 'bad_request'; return 'No paste content received'; } my $row = schema ->resultset('Paste') ->create({ paste_content => $paste_content, }); my $ext_id = ...; # get an ID };
But that's not compact enough – We have a whole alphabet of acceptable URL characters Integer::Tiny represents integers compactly my $mapper = do { my $key = join ( 'a'..'z', 0..9 ); Integer::Tiny ->new($key); }; ... my $ext_id = $mapper ->encrypt($row->id );
use Dancer::Test; my $rand = rand(); # paste content route_exists [POST => '/'], 'a route handler is defined for POST /'; my $response = dancer_response( 'POST', '/', { params => {p => $rand} } ); like $response->content => qr{^http://.+/.+} or diag explain $response;
need to send a POST with content • Safe to do this; we already tested that this works 2.Then, request the URL it gave us 3.Verify that the content matches what we sent in #1
{ content_type('text/plain'); status 'not_found'; return "No such paste as '$ext_id'"; } headers 'X-Pastebin-ID' => $ext_id; content_type('text/plain'); return $paste->content;
The slides are available at http://mikedoherty.ca/t/dalfcs-perldancer Thanks for joining me I'm happy to stick around for questions More Perl sessions?