File Coverage

blib/lib/ojo.pm
Criterion Covered Total %
statement 51 51 100.0
branch 5 10 50.0
condition 2 2 100.0
subroutine 34 34 100.0
pod n/a
total 92 97 94.8


line stmt bran cond sub pod time code
1             package ojo;
2 3     3   133774 use Mojo::Base -strict;
  3         8  
  3         25  
3              
4 3     3   1884 use Benchmark qw(timeit timestr :hireswallclock);
  3         15192  
  3         22  
5 3     3   1407 use Mojo::ByteStream qw(b);
  3         17  
  3         190  
6 3     3   20 use Mojo::Collection qw(c);
  3         6  
  3         142  
7 3     3   1105 use Mojo::DOM;
  3         11  
  3         111  
8 3     3   931 use Mojo::File qw(path);
  3         12  
  3         168  
9 3     3   1030 use Mojo::JSON qw(j);
  3         8  
  3         219  
10 3     3   973 use Mojo::URL;
  3         24  
  3         23  
11 3     3   49 use Mojo::Util qw(dumper monkey_patch);
  3         8  
  3         2481  
12              
13             # Silent one-liners
14             $ENV{MOJO_LOG_LEVEL} ||= 'fatal';
15              
16             sub import {
17              
18             # Mojolicious::Lite
19 3     3   30 my $caller = caller;
20 3 50   3   946 eval "package $caller; use Mojolicious::Lite; 1" or die $@;
  3         13  
  3         19  
  3         255  
21 3 50       48 Mojo::Base->import(-strict, $] < 5.020 ? () : (-signatures));
22 3         307 my $ua = $caller->app->ua;
23 3     10   45 $ua->server->app->hook(around_action => sub { local $_ = $_[1]; $_[0]() });
  10         23  
  10         19  
24              
25 3 50       29 $ua->max_redirects(10) unless defined $ENV{MOJO_MAX_REDIRECTS};
26 3 50       31 $ua->proxy->detect unless defined $ENV{MOJO_PROXY};
27              
28             # The ojo DSL
29             monkey_patch $caller,
30 3 50   3   1639 a => sub { $caller->can('any')->(@_) and return $ua->server->app },
        3      
31             b => \&b,
32             c => \&c,
33 1     1   11 d => sub { $ua->delete(@_)->result },
        1      
34             f => \&path,
35 1     1   3168 g => sub { $ua->get(@_)->result },
        1      
36 1     1   7 h => sub { $ua->head(@_)->result },
        1      
37             j => \&j,
38 1     1   21684 l => sub { Mojo::URL->new(@_) },
        1      
39 2   100 2   22304 n => sub (&@) { say STDERR timestr timeit($_[1] // 1, $_[0]) },
        2      
40 1     1   8 o => sub { $ua->options(@_)->result },
        1      
41 3     3   17 p => sub { $ua->post(@_)->result },
        3      
42             r => \&dumper,
43 1     1   12 t => sub { $ua->patch(@_)->result },
        1      
44 1     1   11 u => sub { $ua->put(@_)->result },
        1      
45 3     1   150 x => sub { Mojo::DOM->new(@_) };
  1     1   2726  
46             }
47              
48             1;
49              
50             =encoding utf8
51              
52             =head1 NAME
53              
54             ojo - Fun one-liners with Mojo
55              
56             =head1 SYNOPSIS
57              
58             $ perl -Mojo -E 'say g("mojolicious.org")->dom->at("title")->text'
59              
60             =head1 DESCRIPTION
61              
62             A collection of automatically exported functions for fun Perl one-liners. Ten redirects will be followed by default,
63             you can change this behavior with the C environment variable.
64              
65             $ MOJO_MAX_REDIRECTS=0 perl -Mojo -E 'say g("example.com")->code'
66              
67             Proxy detection is enabled by default, but you can disable it with the C environment variable.
68              
69             $ MOJO_PROXY=0 perl -Mojo -E 'say g("example.com")->body'
70              
71             TLS certificate verification can be disabled with the C environment variable.
72              
73             $ MOJO_INSECURE=1 perl -Mojo -E 'say g("https://127.0.0.1:3000")->body'
74              
75             Every L one-liner is also a L application.
76              
77             $ perl -Mojo -E 'get "/" => {inline => "%= time"}; app->start' get /
78              
79             On Perl 5.20+ L will be enabled automatically.
80              
81             $ perl -Mojo -E 'a(sub ($c) { $c->render(text => "Hello!") })->start' get /
82              
83             If it is not already defined, the C environment variable will be set to C.
84              
85             =head1 FUNCTIONS
86              
87             L implements the following functions, which are automatically exported.
88              
89             =head2 a
90              
91             my $app = a('/hello' => sub { $_->render(json => {hello => 'world'}) });
92              
93             Create a route with L and return the current L object. The current
94             controller object is also available to actions as C<$_>. See also L for more argument
95             variations.
96              
97             $ perl -Mojo -E 'a("/hello" => {text => "Hello Mojo!"})->start' daemon
98              
99             =head2 b
100              
101             my $stream = b('lalala');
102              
103             Turn string into a L object.
104              
105             $ perl -Mojo -E 'b(g("mojolicious.org")->body)->html_unescape->say'
106              
107             =head2 c
108              
109             my $collection = c(1, 2, 3);
110              
111             Turn list into a L object.
112              
113             =head2 d
114              
115             my $res = d('example.com');
116             my $res = d('http://example.com' => {Accept => '*/*'} => 'Hi!');
117             my $res = d('http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
118             my $res = d('http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
119              
120             Perform C request with L and return resulting L object.
121              
122             =head2 f
123              
124             my $path = f('/home/sri/foo.txt');
125              
126             Turn string into a L object.
127              
128             $ perl -Mojo -E 'say r j f("hello.json")->slurp'
129              
130             =head2 g
131              
132             my $res = g('example.com');
133             my $res = g('http://example.com' => {Accept => '*/*'} => 'Hi!');
134             my $res = g('http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
135             my $res = g('http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
136              
137             Perform C request with L and return resulting L object.
138              
139             $ perl -Mojo -E 'say g("mojolicious.org")->dom("h1")->map("text")->join("\n")'
140              
141             =head2 h
142              
143             my $res = h('example.com');
144             my $res = h('http://example.com' => {Accept => '*/*'} => 'Hi!');
145             my $res = h('http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
146             my $res = h('http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
147              
148             Perform C request with L and return resulting L object.
149              
150             =head2 j
151              
152             my $bytes = j([1, 2, 3]);
153             my $bytes = j({foo => 'bar'});
154             my $value = j($bytes);
155              
156             Encode Perl data structure or decode JSON with L.
157              
158             $ perl -Mojo -E 'f("hello.json")->spew(j {hello => "world!"})'
159              
160             =head2 l
161              
162             my $url = l('https://mojolicious.org');
163              
164             Turn a string into a L object.
165              
166             $ perl -Mojo -E 'say l("/perldoc")->to_abs(l("https://mojolicious.org"))'
167              
168             =head2 n
169              
170             n {...};
171             n {...} 100;
172              
173             Benchmark block and print the results to C, with an optional number of iterations, which defaults to C<1>.
174              
175             $ perl -Mojo -E 'n { say g("mojolicious.org")->code }'
176              
177             =head2 o
178              
179             my $res = o('example.com');
180             my $res = o('http://example.com' => {Accept => '*/*'} => 'Hi!');
181             my $res = o('http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
182             my $res = o('http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
183              
184             Perform C request with L and return resulting L object.
185              
186             =head2 p
187              
188             my $res = p('example.com');
189             my $res = p('http://example.com' => {Accept => '*/*'} => 'Hi!');
190             my $res = p('http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
191             my $res = p('http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
192              
193             Perform C request with L and return resulting L object.
194              
195             =head2 r
196              
197             my $perl = r({data => 'structure'});
198              
199             Dump a Perl data structure with L.
200              
201             perl -Mojo -E 'say r g("example.com")->headers->to_hash'
202              
203             =head2 t
204              
205             my $res = t('example.com');
206             my $res = t('http://example.com' => {Accept => '*/*'} => 'Hi!');
207             my $res = t('http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
208             my $res = t('http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
209              
210             Perform C request with L and return resulting L object.
211              
212             =head2 u
213              
214             my $res = u('example.com');
215             my $res = u('http://example.com' => {Accept => '*/*'} => 'Hi!');
216             my $res = u('http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
217             my $res = u('http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
218              
219             Perform C request with L and return resulting L object.
220              
221             =head2 x
222              
223             my $dom = x('
Hello!
');
224              
225             Turn HTML/XML input into L object.
226              
227             $ perl -Mojo -E 'say x(f("test.html")->slurp)->at("title")->text'
228              
229             =head1 SEE ALSO
230              
231             L, L, L.
232              
233             =cut