convert to Dist::Zilla
[gitmo/MooseX-Runnable.git] / lib / MooseX / Runnable / Run.pm
1 package MooseX::Runnable::Run;
2 # ABSTRACT: Run a MooseX::Runnable class as an application
3 use strict;
4 use warnings;
5
6 use MooseX::Runnable::Invocation;
7
8 sub run_application($;@) {
9     my ($app, @args) = @_;
10
11     exit MooseX::Runnable::Invocation->new(
12         class => $app,
13     )->run(@args);
14 }
15
16 sub run_application_with_plugins($$;@){
17     my ($app, $plugins, @args) = @_;
18     exit MooseX::Runnable::Invocation->new(
19         class => $app,
20         plugins => $plugins,
21     )->run(@args);
22 }
23
24 sub import {
25     my ($class, $app) = @_;
26
27     if($app){
28         run_application $app, @ARGV;
29     }
30     else {
31         my $c = caller;
32         no strict 'refs';
33         *{ $c. '::run_application' } = \&run_application;
34         *{ $c. '::run_application_with_plugins' } = \&run_application_with_plugins;
35     }
36 }
37
38 1;
39
40 __END__
41
42 =pod
43
44 =head1 SYNOPSIS
45
46 Write an app:
47
48    package MyApp;
49    use Moose; with 'MooseX::Runnable';
50    sub run { say 'Hello, world.'; return 0 } # (UNIX exit code)
51
52 Write a wrapper script, C<myapp.pl>.  With sugar:
53
54    #!/usr/bin/env perl
55    use MooseX::Runnable::Run 'MyApp';
56
57 Or without:
58
59    #!/usr/bin/env perl
60    use MooseX::Runnable::Run;
61
62    run_application 'MyApp', @ARGV;
63
64 Then, run your app:
65
66    $ ./myapp.pl
67    Hello, world.
68    $ echo $?
69    0
70
71 =head1 DESCRIPTION
72
73 This is a utility module that runs a L<MooseX::Runnable|MooseX::Runnable> class with
74 L<MooseX::Runnable::Invocation|MooseX::Runnable::Invocation>.
75
76 =head1 SEE ALSO
77
78 L<mx-run>, a script that will run MooseX::Runnable apps, saving you
79 valuable seconds!
80
81 L<MooseX::Runnable>
82
83 =cut