| 1 | package ojo; | ||||||||||||
| 2 | 1 | ▶ | use Mojo::Base -strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 1 | ▶ | use Benchmark qw(timeit timestr :hireswallclock); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use Mojo::ByteStream qw(b); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Mojo::Collection qw(c); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Mojo::DOM; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Mojo::File qw(path); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use Mojo::JSON qw(j); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use Mojo::URL; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use Mojo::Util qw(dumper monkey_patch); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | # Silent one-liners | ||||||||||||
| 14 | 1 | ▶ | $ENV{MOJO_LOG_LEVEL} ||= 'fatal'; | ||||||||||
|
Condition50%1/2
$ENV{"MOJO_LOG_LEVEL"} ||= "fatal"
MC/DC0%0/1
$ENV{"MOJO_LOG_LEVEL"} ||= "fatal"
$ENV{"MOJO_LOG_LEVEL"}
Truth table50%1/2
A
A= $ENV{"MOJO_LOG_LEVEL"}
| |||||||||||||
| 15 | |||||||||||||
| 16 | 1 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=5 SCAR=23.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | # Mojolicious::Lite | ||||||||||||
| 19 | 1 | my $caller = caller; | |||||||||||
| 20 | 1 | ▶ | eval "package $caller; use Mojolicious::Lite; 1" or die $@; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 21 | 1 | ▶ | Mojo::Base->import(-strict, $] < 5.020 ? () : (-signatures)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 1 | my $ua = $caller->app->ua; | |||||||||||
| 23 | 0 | ▶ | $ua->server->app->hook(around_action => sub { local $_ = $_[1]; $_[0]() }); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||
| 24 | |||||||||||||
| 25 | 1 | ▶ | $ua->max_redirects(10) unless defined $ENV{MOJO_MAX_REDIRECTS}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | 1 | ▶ | $ua->proxy->detect unless defined $ENV{MOJO_PROXY}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | |||||||||||||
| 28 | # The ojo DSL | ||||||||||||
| 29 | monkey_patch $caller, | ||||||||||||
| 30 | 0 | ▶ | a => sub { $caller->can('any')->(@_) and return $ua->server->app }, | ||||||||||
|
Subroutine
a: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||
| 31 | b => \&b, | ||||||||||||
| 32 | c => \&c, | ||||||||||||
| 33 | 0 | ▶ | d => sub { $ua->delete(@_)->result }, | ||||||||||
|
Subroutine
d: not called CC=1 SCAR=6.9
| |||||||||||||
| 34 | f => \&path, | ||||||||||||
| 35 | 0 | ▶ | g => sub { $ua->get(@_)->result }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 36 | 0 | ▶ | h => sub { $ua->head(@_)->result }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 37 | j => \&j, | ||||||||||||
| 38 | 0 | ▶ | l => sub { Mojo::URL->new(@_) }, | ||||||||||
|
Subroutine
l: not called CC=1 SCAR=6.9
| |||||||||||||
| 39 | 0 | ▶ | n => sub (&@) { say STDERR timestr timeit($_[1] // 1, $_[0]) }, | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[1] // 1
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||
| 40 | 0 | ▶ | o => sub { $ua->options(@_)->result }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 41 | 0 | ▶ | p => sub { $ua->post(@_)->result }, | ||||||||||
|
Subroutine
p: not called CC=1 SCAR=6.9
| |||||||||||||
| 42 | r => \&dumper, | ||||||||||||
| 43 | 0 | ▶ | t => sub { $ua->patch(@_)->result }, | ||||||||||
|
Subroutine
t: not called CC=1 SCAR=6.9
| |||||||||||||
| 44 | 0 | ▶ | u => sub { $ua->put(@_)->result }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 45 | 0 | ▶ | x => sub { Mojo::DOM->new(@_) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 1 | 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<MOJO_MAX_REDIRECTS> 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<MOJO_PROXY> 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<MOJO_INSECURE> environment variable. | ||||||||||||
| 72 | |||||||||||||
| 73 | $ MOJO_INSECURE=1 perl -Mojo -E 'say g("https://127.0.0.1:3000")->body' | ||||||||||||
| 74 | |||||||||||||
| 75 | Every L<ojo> one-liner is also a L<Mojolicious::Lite> application. | ||||||||||||
| 76 | |||||||||||||
| 77 | $ perl -Mojo -E 'get "/" => {inline => "%= time"}; app->start' get / | ||||||||||||
| 78 | |||||||||||||
| 79 | On Perl 5.20+ L<subroutine signatures|perlsub/"Signatures"> 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<MOJO_LOG_LEVEL> environment variable will be set to C<fatal>. | ||||||||||||
| 84 | |||||||||||||
| 85 | =head1 FUNCTIONS | ||||||||||||
| 86 | |||||||||||||
| 87 | L<ojo> 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<Mojolicious::Lite/"any"> and return the current L<Mojolicious::Lite> object. The current | ||||||||||||
| 94 | controller object is also available to actions as C<$_>. See also L<Mojolicious::Guides::Tutorial> 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<Mojo::ByteStream> 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<Mojo::Collection> 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<DELETE> request with L<Mojo::UserAgent/"delete"> and return resulting L<Mojo::Message::Response> object. | ||||||||||||
| 121 | |||||||||||||
| 122 | =head2 f | ||||||||||||
| 123 | |||||||||||||
| 124 | my $path = f('/home/sri/foo.txt'); | ||||||||||||
| 125 | |||||||||||||
| 126 | Turn string into a L<Mojo::File> 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<GET> request with L<Mojo::UserAgent/"get"> and return resulting L<Mojo::Message::Response> 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<HEAD> request with L<Mojo::UserAgent/"head"> and return resulting L<Mojo::Message::Response> 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<Mojo::JSON/"j">. | ||||||||||||
| 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<Mojo::URL> 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<STDERR>, 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<OPTIONS> request with L<Mojo::UserAgent/"options"> and return resulting L<Mojo::Message::Response> 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<POST> request with L<Mojo::UserAgent/"post"> and return resulting L<Mojo::Message::Response> object. | ||||||||||||
| 194 | |||||||||||||
| 195 | =head2 r | ||||||||||||
| 196 | |||||||||||||
| 197 | my $perl = r({data => 'structure'}); | ||||||||||||
| 198 | |||||||||||||
| 199 | Dump a Perl data structure with L<Mojo::Util/"dumper">. | ||||||||||||
| 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<PATCH> request with L<Mojo::UserAgent/"patch"> and return resulting L<Mojo::Message::Response> 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<PUT> request with L<Mojo::UserAgent/"put"> and return resulting L<Mojo::Message::Response> object. | ||||||||||||
| 220 | |||||||||||||
| 221 | =head2 x | ||||||||||||
| 222 | |||||||||||||
| 223 | my $dom = x('<div>Hello!</div>'); | ||||||||||||
| 224 | |||||||||||||
| 225 | Turn HTML/XML input into L<Mojo::DOM> object. | ||||||||||||
| 226 | |||||||||||||
| 227 | $ perl -Mojo -E 'say x(f("test.html")->slurp)->at("title")->text' | ||||||||||||
| 228 | |||||||||||||
| 229 | =head1 SEE ALSO | ||||||||||||
| 230 | |||||||||||||
| 231 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||
| 232 | |||||||||||||
| 233 | =cut | ||||||||||||