Mojolicious/Lite.pm

filter: Statementstmt 85.1% 40 / 47 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 68.7% 11 / 16 Podpod n/a totaltotal 77.7% 56 / 72 CC 6 SCAR 7.6
CC
6
Cov
83%
import
6.4
__ANON__
2.0
group
2.0
CRAP 2.1
« Mojo/EventEmitter.pm ↑ Summary Mojo/Server/Morbo/Backend.pm »
1 package Mojolicious::Lite;
2 11 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # "Bender: Bite my shiny metal ass!"
5 11 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Mojo::UserAgent::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Mojo::Util qw(monkey_patch);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶sub import {
Subroutine import: called CC=6 SCAR=18.5
10  
11 # Remember executable for later
12 13 ▶ $ENV{MOJO_EXE} ||= (caller)[1];
Condition66%2/3
$ENV{"MOJO_EXE"} ||= (caller)[1]
l!l&&r!l&&!r
1120
MC/DC0%0/2
$ENV{"MOJO_EXE"} ||= (caller)[1]
$ENV{"MOJO_EXE"} (caller)[1]
Truth table66%2/3
A ||= B
A= $ENV{"MOJO_EXE"}B= (caller)[1]
ABresult
1X1
011
000
13  
14 # Reuse home directory if possible
15 13 ▶ local $ENV{MOJO_HOME} = path($ENV{MOJO_EXE})->dirname->to_string unless $ENV{MOJO_HOME};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"MOJO_HOME"} 13 0 13
16  
17 # Initialize application class
18 13 my $caller = caller;
19 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 push @{"${caller}::ISA"}, 'Mojolicious';
21  
22 # Generate moniker based on filename
23 13 my $moniker = path($ENV{MOJO_EXE})->basename('.pl', '.pm', '.t');
24 13 my $app = shift->new(moniker => $moniker);
25  
26 # Initialize routes without namespaces
27 13 my $routes = $app->routes->namespaces([]);
28 13 $app->static->classes->[0] = $app->renderer->classes->[0] = $caller;
29  
30 # The Mojolicious::Lite DSL
31 13 my $root = $routes;
32 13 for my $name (qw(any get options patch post put query websocket)) {
33 0 ▶ monkey_patch $caller, $name, sub { $routes->$name(@_) }
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine any: called CC=1 SCAR=1.2
34 104 }
35 13 ▶ monkey_patch($caller, $_, sub {$app}) for qw(new app);
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine app: called CC=1 SCAR=0.0
36 0 ▶ monkey_patch $caller, del => sub { $routes->delete(@_) };
Subroutine del: not called CC=1 SCAR=1.2
37 0 ▶ monkey_patch $caller, group => sub (&) {
Subroutine group: not called CC=1 SCAR=6.9
38 0 (my $old, $root) = ($root, $routes);
39 0 shift->();
40 0 ($routes, $root) = ($root, $old);
41 13 };
42 monkey_patch $caller,
43 2 ▶ helper => sub { $app->helper(@_) },
Subroutine helper: called CC=1 SCAR=0.0
44 0 ▶ hook => sub { $app->hook(@_) },
Subroutine hook: not called CC=1 SCAR=6.9
45 2 ▶ plugin => sub { $app->plugin(@_) },
Subroutine plugin: called CC=1 SCAR=0.0
46 0 ▶ under => sub { $routes = $root->under(@_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
47  
48 # Make sure there's a default application for testing
49 13 ▶ Mojo::UserAgent::Server->app($app) unless Mojo::UserAgent::Server->app;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::UserAgent::Server"->app 10 3 13
50  
51 # Lite apps are strict!
52 13 unshift @_, 'Mojo::Base', '-strict';
53 13 goto &Mojo::Base::import;
54 }
55  
56 11 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<Mojolicious::Lite> is a tiny domain specific language built around L<Mojolicious>, 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<subroutine
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<Mojolicious::Guides::Tutorial> for more!
96  
97 =head1 GROWING
98  
99 While L<Mojolicious::Guides::Growing> will give you a detailed introduction to growing a L<Mojolicious::Lite> prototype
100 into a well-structured L<Mojolicious> 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</"get">, L</"post"> 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</"app"> is the first argument passed to the C<startup>
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</"plugin"> function you just use the method L<Mojolicious/"plugin">.
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</"helper"> function you just use the method L<Mojolicious/"helper">.
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<Mojolicious::Lite> 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<Mojolicious::Routes::Route/"any">, matching any of the listed HTTP request methods or all. See
194 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more information.
195  
196 =head2 app
197  
198 my $app = app;
199  
200 Returns the L<Mojolicious::Lite> application object, which is a subclass of L<Mojolicious>.
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<Mojolicious::Routes::Route/"delete">, matching only C<DELETE> requests. See
215 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> 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<Mojolicious::Routes::Route/"get">, matching only C<GET> requests. See
226 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> 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<Mojolicious/"helper">.
239  
240 =head2 hook
241  
242 hook after_dispatch => sub ($c) {...};
243  
244 Share code with L<Mojolicious/"hook">.
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<Mojolicious::Routes::Route/"options">, matching only C<OPTIONS> requests. See
255 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> 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<Mojolicious::Routes::Route/"patch">, matching only C<PATCH> requests. See
266 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more information.
267  
268 =head2 plugin
269  
270 plugin SomePlugin => {foo => 23};
271  
272 Load a plugin with L<Mojolicious/"plugin">.
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<Mojolicious::Routes::Route/"post">, matching only C<POST> requests. See
283 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> 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<Mojolicious::Routes::Route/"put">, matching only C<PUT> requests. See
294 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more information.
295  
296 =head2 query
297  
298 my $route = query '/:foo' => sub ($c) {...};
299 my $route = query '/:foo' => sub ($c) {...} => 'name';
300 my $route = query '/:foo' => {foo => 'bar'} => sub ($c) {...};
301 my $route = query '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
302 my $route = query '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
303  
304 Generate route with L<Mojolicious::Routes::Route/"query">, matching only C<QUERY> requests. See
305 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more information.
306  
307 =head2 under
308  
309 my $route = under sub ($c) {...};
310 my $route = under '/:foo' => sub ($c) {...};
311 my $route = under '/:foo' => {foo => 'bar'};
312 my $route = under '/:foo' => [foo => qr/\w+/];
313 my $route = under '/:foo' => (agent => qr/Firefox/);
314 my $route = under [format => ['json', 'yaml']];
315  
316 Generate nested route with L<Mojolicious::Routes::Route/"under">, to which all following routes are automatically
317 appended. See L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more information.
318  
319 =head2 websocket
320  
321 my $route = websocket '/:foo' => sub ($c) {...};
322 my $route = websocket '/:foo' => sub ($c) {...} => 'name';
323 my $route = websocket '/:foo' => {foo => 'bar'} => sub ($c) {...};
324 my $route = websocket '/:foo' => [foo => qr/\w+/] => sub ($c) {...};
325 my $route = websocket '/:foo' => (agent => qr/Firefox/) => sub ($c) {...};
326  
327 Generate route with L<Mojolicious::Routes::Route/"websocket">, matching only WebSocket handshakes. See
328 L<Mojolicious::Guides::Tutorial> and L<Mojolicious::Guides::Routing> for more information.
329  
330 =head1 ATTRIBUTES
331  
332 L<Mojolicious::Lite> inherits all attributes from L<Mojolicious>.
333  
334 =head1 METHODS
335  
336 L<Mojolicious::Lite> inherits all methods from L<Mojolicious>.
337  
338 =head1 SEE ALSO
339  
340 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
341  
342 =cut
« Mojo/EventEmitter.pm ↑ Summary Mojo/Server/Morbo/Backend.pm »