File Coverage

blib/lib/Mojolicious/Command/eval.pm
Criterion Covered Total %
statement 37 37 100.0
branch 8 10 80.0
condition 3 8 37.5
subroutine 10 10 100.0
pod 1 1 100.0
total 59 66 89.3


line stmt bran cond sub pod time code
1             package Mojolicious::Command::eval;
2 1     1   7 use Mojo::Base 'Mojolicious::Command';
  1         4  
  1         7  
3              
4 1     1   8 use Mojo::Promise;
  1         5  
  1         9  
5 1     1   5 use Mojo::Util qw(getopt);
  1         2  
  1         357  
6              
7             has description => 'Run code against application';
8             has usage => sub { shift->extract_usage };
9              
10             sub run {
11 6     6 1 2609 my ($self, @args) = @_;
12              
13 6 100       27 die $self->usage unless getopt \@args, 'v|verbose' => \my $v1, 'V' => \my $v2;
14 5   50     23 my $code = shift @args || '';
15              
16             # Run code against application
17 5         37 my $app = $self->app;
18 1     1   9 my $result = eval "package main; no warnings 'redefine'; sub app; local *app = sub { \$app }; $code";
  1     1   3  
  1     1   109  
  1     1   9  
  1     1   3  
  1         80  
  1         7  
  1         3  
  1         92  
  1         22  
  1         8  
  1         75  
  1         8  
  1         3  
  1         130  
  5         465  
19 5 100       37 die $@ if $@;
20              
21             # Handle promises
22 4         7 my $err;
23 4     1   30 Mojo::Promise->resolve($result)->then(sub { $result = shift }, sub { $err = shift })->wait;
  3         15  
  1         3  
24 4 100       9 die $err if $err;
25              
26 3 50 33     19 return $result unless defined $result && ($v1 || $v2);
      33        
27 3 50       42 $v2 ? print($app->dumper($result)) : say $result;
28             }
29              
30             1;
31              
32             =encoding utf8
33              
34             =head1 NAME
35              
36             Mojolicious::Command::eval - Eval command
37              
38             =head1 SYNOPSIS
39              
40             Usage: APPLICATION eval [OPTIONS] CODE
41              
42             ./myapp.pl eval 'say app->ua->get("/")->result->body'
43             ./myapp.pl eval 'say for sort keys %{app->renderer->helpers}'
44             ./myapp.pl eval -v 'app->home'
45             ./myapp.pl eval -V 'app->renderer->paths'
46              
47             Options:
48             -h, --help Show this summary of available options
49             --home Path to home directory of your application, defaults to
50             the value of MOJO_HOME or auto-detection
51             -m, --mode Operating mode for your application, defaults to the
52             value of MOJO_MODE/PLACK_ENV or "development"
53             -v, --verbose Print return value to STDOUT
54             -V Print returned data structure to STDOUT
55              
56             =head1 DESCRIPTION
57              
58             L runs code against applications. If the result is a promise (then-able), it will wait
59             until the promise is fulfilled or rejected and the result is returned.
60              
61             This is a core command, that means it is always enabled and its code a good example for learning to build new commands,
62             you're welcome to fork it.
63              
64             See L for a list of commands that are available by default.
65              
66             =head1 ATTRIBUTES
67              
68             L inherits all attributes from L and implements the following new
69             ones.
70              
71             =head2 description
72              
73             my $description = $eval->description;
74             $eval = $eval->description('Foo');
75              
76             Short description of this command, used for the command list.
77              
78             =head2 usage
79              
80             my $usage = $eval->usage;
81             $eval = $eval->usage('Foo');
82              
83             Usage information for this command, used for the help screen.
84              
85             =head1 METHODS
86              
87             L inherits all methods from L and implements the following new ones.
88              
89             =head2 run
90              
91             $eval->run(@ARGV);
92              
93             Run this command.
94              
95             =head1 SEE ALSO
96              
97             L, L, L.
98              
99             =cut