=encoding utf8 =head1 NAME Mojolicious::Guides::Tutorial - Get started with Mojolicious =head1 TUTORIAL A quick example-driven introduction to the wonders of L. Almost everything you'll learn here also applies to full L applications. This is only the first of the L. Other guides delve deeper into topics like L a L prototype into a well-structured L application, L, L and more. It is highly encouraged that readers continue on to the remaining guides after reading this one. =head2 Hello World A simple Hello World application can look like this, L, L, L and Perl 5.16 L are automatically enabled and a few L imported, when you use L, turning your script into a full featured web application. #!/usr/bin/env perl use Mojolicious::Lite -signatures; get '/' => sub ($c) { $c->render(text => 'Hello World!'); }; app->start; With L there is also a helper command to generate a small example application. $ mojo generate lite-app myapp.pl =head2 Commands Many different L are automatically available from the command line. CGI and L environments can even be detected and will usually just work without commands. $ ./myapp.pl daemon Web application available at http://127.0.0.1:3000 $ ./myapp.pl daemon -l http://*:8080 Web application available at http://127.0.0.1:8080 $ ./myapp.pl cgi ...CGI output... $ ./myapp.pl get / Hello World! $ ./myapp.pl ...List of available commands (or automatically detected environment)... A call to L (Cstart>), which starts the command system, should be the last expression in your application, because its return value can be significant. # Use @ARGV to pick a command app->start; # Start the "daemon" command app->start('daemon', '-l', 'http://*:8080'); =head2 Reloading Your application will automatically reload itself if you start it with the L development web server, so you don't have to restart the server after every change. $ morbo ./myapp.pl Web application available at http://127.0.0.1:3000 For more information about how to deploy your application see also L. =head2 Routes Routes are basically just fancy paths that can contain different kinds of placeholders and usually lead to an action, if they match the path part of the request URL. The first argument passed to all actions (C<$c>) is a L object, containing both the HTTP request and response. use Mojolicious::Lite -signatures; # Route leading to an action that renders some text get '/foo' => sub ($c) { $c->render(text => 'Hello World!'); }; app->start; Response content is often generated by actions with L, but more about that later. =head2 GET/POST parameters All C and C parameters sent with the request are accessible via L. use Mojolicious::Lite -signatures; # /foo?user=sri get '/foo' => sub ($c) { my $user = $c->param('user'); $c->render(text => "Hello $user."); }; app->start; =head2 Stash and templates The L is used to pass data to templates, which can be inlined in the C section. A few stash values like C