add files
|
1 |
package Mojolicious::Command::eval; |
2 |
use Mojo::Base 'Mojolicious::Command'; |
|
3 | ||
4 |
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case); |
|
5 | ||
6 |
has description => "Run code against application.\n"; |
|
7 |
has usage => <<EOF; |
|
8 |
usage: $0 eval [OPTIONS] CODE |
|
9 | ||
10 |
mojo eval 'say app->ua->get("/")->res->body' |
|
11 |
mojo eval -v 'app->home' |
|
12 |
mojo eval -V 'app->renderer->paths' |
|
13 | ||
14 |
These options are available: |
|
15 |
-v, --verbose Print return value to STDOUT. |
|
16 |
-V Print returned data structure to STDOUT. |
|
17 |
EOF |
|
18 | ||
19 |
sub run { |
|
20 |
my ($self, @args) = @_; |
|
21 | ||
22 |
GetOptionsFromArray \@args, 'v|verbose' => \my $v1, 'V' => \my $v2; |
|
23 |
my $code = shift @args || ''; |
|
24 | ||
25 |
# Run code against application |
|
26 |
my $app = $self->app; |
|
27 |
no warnings; |
|
28 |
my $result = eval "package main; sub app; local *app = sub { \$app }; $code"; |
|
29 |
return $@ ? die $@ : $result unless defined $result && ($v1 || $v2); |
|
30 |
$v2 ? print($app->dumper($result)) : say $result; |
|
31 |
} |
|
32 | ||
33 |
1; |
|
34 | ||
35 |
=encoding utf8 |
|
36 | ||
37 |
=head1 NAME |
|
38 | ||
39 |
Mojolicious::Command::eval - Eval command |
|
40 | ||
41 |
=head1 SYNOPSIS |
|
42 | ||
43 |
use Mojolicious::Command::eval; |
|
44 | ||
45 |
my $eval = Mojolicious::Command::eval->new; |
|
46 |
$eval->run(@ARGV); |
|
47 | ||
48 |
=head1 DESCRIPTION |
|
49 | ||
50 |
L<Mojolicious::Command::eval> runs code against applications. |
|
51 | ||
52 |
This is a core command, that means it is always enabled and its code a good |
|
53 |
example for learning to build new commands, you're welcome to fork it. |
|
54 | ||
55 |
=head1 ATTRIBUTES |
|
56 | ||
57 |
L<Mojolicious::Command::eval> inherits all attributes from |
|
58 |
L<Mojolicious::Command> and implements the following new ones. |
|
59 | ||
60 |
=head2 description |
|
61 | ||
62 |
my $description = $eval->description; |
|
63 |
$eval = $eval->description('Foo!'); |
|
64 | ||
65 |
Short description of this command, used for the command list. |
|
66 | ||
67 |
=head2 usage |
|
68 | ||
69 |
my $usage = $eval->usage; |
|
70 |
$eval = $eval->usage('Foo!'); |
|
71 | ||
72 |
Usage information for this command, used for the help screen. |
|
73 | ||
74 |
=head1 METHODS |
|
75 | ||
76 |
L<Mojolicious::Command::eval> inherits all methods from |
|
77 |
L<Mojolicious::Command> and implements the following new ones. |
|
78 | ||
79 |
=head2 run |
|
80 | ||
81 |
$eval->run(@ARGV); |
|
82 | ||
83 |
Run this command. |
|
84 | ||
85 |
=head1 SEE ALSO |
|
86 | ||
87 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
88 | ||
89 |
=cut |