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