.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojolicious::Lite 3pm" .TH Mojolicious::Lite 3pm 2024-05-15 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Mojolicious::Lite \- Micro real\-time web framework .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& # Automatically enables "strict", "warnings", "utf8" and Perl 5.16 features \& use Mojolicious::Lite \-signatures; \& \& # Route with placeholder \& get \*(Aq/:foo\*(Aq => sub ($c) { \& my $foo = $c\->param(\*(Aqfoo\*(Aq); \& $c\->render(text => "Hello from $foo."); \& }; \& \& # Start the Mojolicious command system \& app\->start; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojolicious::Lite is a tiny domain specific language built around Mojolicious, made up of only about a dozen Perl functions. .PP On Perl 5.20+ you can also use a \f(CW\*(C`\-signatures\*(C'\fR flag to enable support for subroutine signatures. .PP .Vb 1 \& use Mojolicious::Lite \-signatures; \& \& get \*(Aq/:foo\*(Aq => sub ($c) { \& my $foo = $c\->param(\*(Aqfoo\*(Aq); \& $c\->render(text => "Hello from $foo."); \& }; \& \& app\->start; .Ve .PP See Mojolicious::Guides::Tutorial for more! .SH GROWING .IX Header "GROWING" While Mojolicious::Guides::Growing will give you a detailed introduction to growing a Mojolicious::Lite prototype into a well-structured Mojolicious application, here we have collected a few snippets that illustrate very well just how similar both of them are. .SS Routes .IX Subsection "Routes" The functions "get", "post" and friends all have equivalent methods. .PP .Vb 4 \& # Mojolicious::Lite \& get \*(Aq/foo\*(Aq => sub ($c) { \& $c\->render(text => \*(AqHello World!\*(Aq); \& }; \& \& # Mojolicious \& sub startup ($self) { \& \& my $routes = $self\->routes; \& $routes\->get(\*(Aq/foo\*(Aq => sub ($c) { \& $c\->render(text => \*(AqHello World!\*(Aq); \& }); \& } .Ve .SS Application .IX Subsection "Application" The application object you can access with the function "app" is the first argument passed to the \f(CW\*(C`startup\*(C'\fR method. .PP .Vb 2 \& # Mojolicious::Lite \& app\->max_request_size(16777216); \& \& # Mojolicious \& sub startup ($self) { \& $self\->max_request_size(16777216); \& } .Ve .SS Plugins .IX Subsection "Plugins" Instead of the "plugin" function you just use the method "plugin" in Mojolicious. .PP .Vb 2 \& # Mojolicious::Lite \& plugin \*(AqConfig\*(Aq; \& \& # Mojolicious \& sub startup ($self) { \& $self\->plugin(\*(AqConfig\*(Aq); \& } .Ve .SS Helpers .IX Subsection "Helpers" Similar to plugins, instead of the "helper" function you just use the method "helper" in Mojolicious. .PP .Vb 4 \& # Mojolicious::Lite \& helper two => sub ($c) { \& return 1 + 1; \& }; \& \& # Mojolicious \& sub startup ($self) { \& $self\->helper(two => sub ($c) { \& return 1 + 1; \& }); \& } .Ve .SS Under .IX Subsection "Under" Instead of sequential function calls, we can use methods to build a tree with nested routes, that much better illustrates how routes work internally. .PP .Vb 3 \& # Mojolicious::Lite \& under \*(Aq/foo\*(Aq; \& get \*(Aq/bar\*(Aq => sub ($c) {...}; \& \& # Mojolicious \& sub startup ($self) { \& \& my $routes = $self\->routes; \& my $foo = $routes\->under(\*(Aq/foo\*(Aq); \& $foo\->get(\*(Aq/bar\*(Aq => sub ($c) {...}); \& } .Ve .SH FUNCTIONS .IX Header "FUNCTIONS" Mojolicious::Lite implements the following functions, which are automatically exported. .SS any .IX Subsection "any" .Vb 7 \& my $route = any \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = any \*(Aq/:foo\*(Aq => sub ($c) {...} => \*(Aqname\*(Aq; \& my $route = any \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq} => sub ($c) {...}; \& my $route = any \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = any [\*(AqGET\*(Aq, \*(AqPOST\*(Aq] => \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = any [\*(AqGET\*(Aq, \*(AqPOST\*(Aq] => \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = any [\*(AqGET\*(Aq, \*(AqPOST\*(Aq] => \*(Aq/:foo\*(Aq => (agent => qr/Firefox/) => sub ($c) {...}; .Ve .PP Generate route with "any" in Mojolicious::Routes::Route, matching any of the listed HTTP request methods or all. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SS app .IX Subsection "app" .Vb 1 \& my $app = app; .Ve .PP Returns the Mojolicious::Lite application object, which is a subclass of Mojolicious. .PP .Vb 3 \& # Use all the available attributes and methods \& app\->log\->level(\*(Aqerror\*(Aq); \& app\->defaults(foo => \*(Aqbar\*(Aq); .Ve .SS del .IX Subsection "del" .Vb 5 \& my $route = del \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = del \*(Aq/:foo\*(Aq => sub ($c) {...} => \*(Aqname\*(Aq; \& my $route = del \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq} => sub ($c) {...}; \& my $route = del \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = del \*(Aq/:foo\*(Aq => (agent => qr/Firefox/) => sub ($c) {...}; .Ve .PP Generate route with "delete" in Mojolicious::Routes::Route, matching only \f(CW\*(C`DELETE\*(C'\fR requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SS get .IX Subsection "get" .Vb 5 \& my $route = get \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = get \*(Aq/:foo\*(Aq => sub ($c) {...} => \*(Aqname\*(Aq; \& my $route = get \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq} => sub ($c) {...}; \& my $route = get \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = get \*(Aq/:foo\*(Aq => (agent => qr/Firefox/) => sub ($c) {...}; .Ve .PP Generate route with "get" in Mojolicious::Routes::Route, matching only \f(CW\*(C`GET\*(C'\fR requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SS group .IX Subsection "group" .Vb 1 \& group {...}; .Ve .PP Start a new route group. .SS helper .IX Subsection "helper" .Vb 1 \& helper foo => sub ($c, @args) {...}; .Ve .PP Add a new helper with "helper" in Mojolicious. .SS hook .IX Subsection "hook" .Vb 1 \& hook after_dispatch => sub ($c) {...}; .Ve .PP Share code with "hook" in Mojolicious. .SS options .IX Subsection "options" .Vb 5 \& my $route = options \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = options \*(Aq/:foo\*(Aq => sub ($c) {...} => \*(Aqname\*(Aq; \& my $route = options \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq} => sub ($c) {...}; \& my $route = options \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = options \*(Aq/:foo\*(Aq => (agent => qr/Firefox/) => sub ($c) {...}; .Ve .PP Generate route with "options" in Mojolicious::Routes::Route, matching only \f(CW\*(C`OPTIONS\*(C'\fR requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SS patch .IX Subsection "patch" .Vb 5 \& my $route = patch \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = patch \*(Aq/:foo\*(Aq => sub ($c) {...} => \*(Aqname\*(Aq; \& my $route = patch \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq} => sub ($c) {...}; \& my $route = patch \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = patch \*(Aq/:foo\*(Aq => (agent => qr/Firefox/) => sub ($c) {...}; .Ve .PP Generate route with "patch" in Mojolicious::Routes::Route, matching only \f(CW\*(C`PATCH\*(C'\fR requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SS plugin .IX Subsection "plugin" .Vb 1 \& plugin SomePlugin => {foo => 23}; .Ve .PP Load a plugin with "plugin" in Mojolicious. .SS post .IX Subsection "post" .Vb 5 \& my $route = post \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = post \*(Aq/:foo\*(Aq => sub ($c) {...} => \*(Aqname\*(Aq; \& my $route = post \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq} => sub ($c) {...}; \& my $route = post \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = post \*(Aq/:foo\*(Aq => (agent => qr/Firefox/) => sub ($c) {...}; .Ve .PP Generate route with "post" in Mojolicious::Routes::Route, matching only \f(CW\*(C`POST\*(C'\fR requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SS put .IX Subsection "put" .Vb 5 \& my $route = put \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = put \*(Aq/:foo\*(Aq => sub ($c) {...} => \*(Aqname\*(Aq; \& my $route = put \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq} => sub ($c) {...}; \& my $route = put \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = put \*(Aq/:foo\*(Aq => (agent => qr/Firefox/) => sub ($c) {...}; .Ve .PP Generate route with "put" in Mojolicious::Routes::Route, matching only \f(CW\*(C`PUT\*(C'\fR requests. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SS under .IX Subsection "under" .Vb 6 \& my $route = under sub ($c) {...}; \& my $route = under \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = under \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq}; \& my $route = under \*(Aq/:foo\*(Aq => [foo => qr/\ew+/]; \& my $route = under \*(Aq/:foo\*(Aq => (agent => qr/Firefox/); \& my $route = under [format => [\*(Aqjson\*(Aq, \*(Aqyaml\*(Aq]]; .Ve .PP Generate nested route with "under" in Mojolicious::Routes::Route, to which all following routes are automatically appended. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SS websocket .IX Subsection "websocket" .Vb 5 \& my $route = websocket \*(Aq/:foo\*(Aq => sub ($c) {...}; \& my $route = websocket \*(Aq/:foo\*(Aq => sub ($c) {...} => \*(Aqname\*(Aq; \& my $route = websocket \*(Aq/:foo\*(Aq => {foo => \*(Aqbar\*(Aq} => sub ($c) {...}; \& my $route = websocket \*(Aq/:foo\*(Aq => [foo => qr/\ew+/] => sub ($c) {...}; \& my $route = websocket \*(Aq/:foo\*(Aq => (agent => qr/Firefox/) => sub ($c) {...}; .Ve .PP Generate route with "websocket" in Mojolicious::Routes::Route, matching only WebSocket handshakes. See Mojolicious::Guides::Tutorial and Mojolicious::Guides::Routing for more information. .SH ATTRIBUTES .IX Header "ATTRIBUTES" Mojolicious::Lite inherits all attributes from Mojolicious. .SH METHODS .IX Header "METHODS" Mojolicious::Lite inherits all methods from Mojolicious. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .