File Coverage

blib/lib/Mojolicious/Lite.pm
Criterion Covered Total %
statement 44 44 100.0
branch 3 4 75.0
condition 2 3 66.6
subroutine 34 36 94.4
pod n/a
total 83 87 95.4


line stmt bran cond sub pod time code
1             package Mojolicious::Lite;
2 44     125   222353 use Mojo::Base 'Mojolicious';
  44         126  
  44         337  
3              
4             # "Bender: Bite my shiny metal ass!"
5 44     125   369 use Mojo::File qw(path);
  44         118  
  44         2165  
6 44     125   361 use Mojo::UserAgent::Server;
  44         114  
  44         470  
7 44     125   312 use Mojo::Util qw(monkey_patch);
  44         130  
  44         5819  
8              
9             sub import {
10              
11             # Remember executable for later
12 58   66 139   1458 $ENV{MOJO_EXE} ||= (caller)[1];
13              
14             # Reuse home directory if possible
15 58 50       453 local $ENV{MOJO_HOME} = path($ENV{MOJO_EXE})->dirname->to_string unless $ENV{MOJO_HOME};
16              
17             # Initialize application class
18 58         235 my $caller = caller;
19 44     125   410 no strict 'refs';
  44         110  
  44         31120  
20 58         181 push @{"${caller}::ISA"}, 'Mojolicious';
  58         1087  
21              
22             # Generate moniker based on filename
23 58         261 my $moniker = path($ENV{MOJO_EXE})->basename('.pl', '.pm', '.t');
24 58         670 my $app = shift->new(moniker => $moniker);
25              
26             # Initialize routes without namespaces
27 58         349 my $routes = $app->routes->namespaces([]);
28 58         243 $app->static->classes->[0] = $app->renderer->classes->[0] = $caller;
29              
30             # The Mojolicious::Lite DSL
31 58         138 my $root = $routes;
32 58         162 for my $name (qw(any get options patch post put websocket)) {
33 399     480   11274 monkey_patch $caller, $name, sub { $routes->$name(@_) }
        399      
        399      
        480      
        561      
        642      
        723      
        804      
        28      
        18      
        18      
        16      
        16      
        0      
        0      
34 406         1637 }
35 58     675   375 monkey_patch($caller, $_, sub {$app}) for qw(new app);
  189     675   52347  
        633      
36 58     404   387 monkey_patch $caller, del => sub { $routes->delete(@_) };
  2     323   12  
37             monkey_patch $caller, group => sub (&) {
38 3     243   18 (my $old, $root) = ($root, $routes);
        162      
39 3         8 shift->();
40 3         14 ($routes, $root) = ($root, $old);
41 58         368 };
42             monkey_patch $caller,
43 19     97   793 helper => sub { $app->helper(@_) },
        97      
44 26     75   716 hook => sub { $app->hook(@_) },
        36      
45 42     54   24127 plugin => sub { $app->plugin(@_) },
        54      
46 58     45   574 under => sub { $routes = $root->under(@_) };
  17     45   131  
47              
48             # Make sure there's a default application for testing
49 58 100       688 Mojo::UserAgent::Server->app($app) unless Mojo::UserAgent::Server->app;
50              
51             # Lite apps are strict!
52 58         257 unshift @_, 'Mojo::Base', '-strict';
53 58         646 goto &Mojo::Base::import;
54             }
55              
56             1;
57              
58             =encoding utf8
59              
60             =head1 NAME
61              
62             Mojolicious::Lite - Micro real-time web framework
63              
64             =head1 SYNOPSIS
65              
66             # Automatically enables "strict", "warnings", "utf8" and Perl 5.16 features
67             use Mojolicious::Lite -signatures;
68              
69             # Route with placeholder
70             get '/:foo' => sub ($c) {
71             my $foo = $c->param('foo');
72             $c->render(text => "Hello from $foo.");
73             };
74              
75             # Start the Mojolicious command system
76             app->start;
77              
78             =head1 DESCRIPTION
79              
80             L is a tiny domain specific language built around L, made up of only about a dozen Perl
81             functions.
82              
83             On Perl 5.20+ you can also use a C<-signatures> flag to enable support for L
84             signatures|perlsub/"Signatures">.
85              
86             use Mojolicious::Lite -signatures;
87              
88             get '/:foo' => sub ($c) {
89             my $foo = $c->param('foo');
90             $c->render(text => "Hello from $foo.");
91             };
92              
93             app->start;
94              
95             See L for more!
96              
97             =head1 GROWING
98              
99             While L will give you a detailed introduction to growing a L prototype
100             into a well-structured L application, here we have collected a few snippets that illustrate very well just
101             how similar both of them are.
102              
103             =head2 Routes
104              
105             The functions L, L and friends all have equivalent methods.
106              
107             # Mojolicious::Lite
108             get '/foo' => sub ($c) {
109             $c->render(text => 'Hello World!');
110             };
111              
112             # Mojolicious
113             sub startup ($self) {
114            
115             my $routes = $self->routes;
116             $routes->get('/foo' => sub ($c) {
117             $c->render(text => 'Hello World!');
118             });
119             }
120              
121             =head2 Application
122              
123             The application object you can access with the function L is the first argument passed to the C
124             method.
125              
126             # Mojolicious::Lite
127             app->max_request_size(16777216);
128              
129             # Mojolicious
130             sub startup ($self) {
131             $self->max_request_size(16777216);
132             }
133              
134             =head2 Plugins
135              
136             Instead of the L function you just use the method L.
137              
138             # Mojolicious::Lite
139             plugin 'Config';
140              
141             # Mojolicious
142             sub startup ($self) {
143             $self->plugin('Config');
144             }
145              
146             =head2 Helpers
147              
148             Similar to plugins, instead of the L function you just use the method L.
149              
150             # Mojolicious::Lite
151             helper two => sub ($c) {
152             return 1 + 1;
153             };
154              
155             # Mojolicious
156             sub startup ($self) {
157             $self->helper(two => sub ($c) {
158             return 1 + 1;
159             });
160             }
161              
162             =head2 Under
163              
164             Instead of sequential function calls, we can use methods to build a tree with nested routes, that much better
165             illustrates how routes work internally.
166              
167             # Mojolicious::Lite
168             under '/foo';
169             get '/bar' => sub ($c) {...};
170              
171             # Mojolicious
172             sub startup ($self) {
173              
174             my $routes = $self->routes;
175             my $foo = $routes->under('/foo');
176             $foo->get('/bar' => sub ($c) {...});
177             }
178              
179             =head1 FUNCTIONS
180              
181             L implements the following functions, which are automatically exported.
182              
183             =head2 any
184              
185             my $route = any '/:foo' => sub ($c) {...};
186             my $route = any '/:foo' => sub ($c) {...} => 'name';
187             my $route = any '/:foo' => {foo => 'bar'} => sub ($c) {...};
188             my $route = any '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
189             my $route = any ['GET', 'POST'] => '/:foo' => sub ($c) {...};
190             my $route = any ['GET', 'POST'] => '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
191             my $route = any ['GET', 'POST'] => '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
192              
193             Generate route with L, matching any of the listed HTTP request methods or all. See
194             L and L for more information.
195              
196             =head2 app
197              
198             my $app = app;
199              
200             Returns the L application object, which is a subclass of L.
201              
202             # Use all the available attributes and methods
203             app->log->level('error');
204             app->defaults(foo => 'bar');
205              
206             =head2 del
207              
208             my $route = del '/:foo' => sub ($c) {...};
209             my $route = del '/:foo' => sub ($c) {...} => 'name';
210             my $route = del '/:foo' => {foo => 'bar'} => sub ($c) {...};
211             my $route = del '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
212             my $route = del '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
213              
214             Generate route with L, matching only C requests. See
215             L and L for more information.
216              
217             =head2 get
218              
219             my $route = get '/:foo' => sub ($c) {...};
220             my $route = get '/:foo' => sub ($c) {...} => 'name';
221             my $route = get '/:foo' => {foo => 'bar'} => sub ($c) {...};
222             my $route = get '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
223             my $route = get '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
224              
225             Generate route with L, matching only C requests. See
226             L and L for more information.
227              
228             =head2 group
229              
230             group {...};
231              
232             Start a new route group.
233              
234             =head2 helper
235              
236             helper foo => sub ($c, @args) {...};
237              
238             Add a new helper with L.
239              
240             =head2 hook
241              
242             hook after_dispatch => sub ($c) {...};
243              
244             Share code with L.
245              
246             =head2 options
247              
248             my $route = options '/:foo' => sub ($c) {...};
249             my $route = options '/:foo' => sub ($c) {...} => 'name';
250             my $route = options '/:foo' => {foo => 'bar'} => sub ($c) {...};
251             my $route = options '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
252             my $route = options '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
253              
254             Generate route with L, matching only C requests. See
255             L and L for more information.
256              
257             =head2 patch
258              
259             my $route = patch '/:foo' => sub ($c) {...};
260             my $route = patch '/:foo' => sub ($c) {...} => 'name';
261             my $route = patch '/:foo' => {foo => 'bar'} => sub ($c) {...};
262             my $route = patch '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
263             my $route = patch '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
264              
265             Generate route with L, matching only C requests. See
266             L and L for more information.
267              
268             =head2 plugin
269              
270             plugin SomePlugin => {foo => 23};
271              
272             Load a plugin with L.
273              
274             =head2 post
275              
276             my $route = post '/:foo' => sub ($c) {...};
277             my $route = post '/:foo' => sub ($c) {...} => 'name';
278             my $route = post '/:foo' => {foo => 'bar'} => sub ($c) {...};
279             my $route = post '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
280             my $route = post '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
281              
282             Generate route with L, matching only C requests. See
283             L and L for more information.
284              
285             =head2 put
286              
287             my $route = put '/:foo' => sub ($c) {...};
288             my $route = put '/:foo' => sub ($c) {...} => 'name';
289             my $route = put '/:foo' => {foo => 'bar'} => sub ($c) {...};
290             my $route = put '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
291             my $route = put '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
292              
293             Generate route with L, matching only C requests. See
294             L and L for more information.
295              
296             =head2 under
297              
298             my $route = under sub ($c) {...};
299             my $route = under '/:foo' => sub ($c) {...};
300             my $route = under '/:foo' => {foo => 'bar'};
301             my $route = under '/:foo' => [foo => qr/\w+/];
302             my $route = under '/:foo' => (agent => qr/Firefox/);
303             my $route = under [format => ['json', 'yaml']];
304              
305             Generate nested route with L, to which all following routes are automatically
306             appended. See L and L for more information.
307              
308             =head2 websocket
309              
310             my $route = websocket '/:foo' => sub ($c) {...};
311             my $route = websocket '/:foo' => sub ($c) {...} => 'name';
312             my $route = websocket '/:foo' => {foo => 'bar'} => sub ($c) {...};
313             my $route = websocket '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
314             my $route = websocket '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
315              
316             Generate route with L, matching only WebSocket handshakes. See
317             L and L for more information.
318              
319             =head1 ATTRIBUTES
320              
321             L inherits all attributes from L.
322              
323             =head1 METHODS
324              
325             L inherits all methods from L.
326              
327             =head1 SEE ALSO
328              
329             L, L, L.
330              
331             =cut