| 1 | package Beam::Runnable; | ||
| 2 | 11 | our $VERSION = '0.016'; | |
| 3 | # ABSTRACT: Role for runnable objects | ||
| 4 | |||
| 5 | #pod =head1 SYNOPSIS | ||
| 6 | #pod | ||
| 7 | #pod package My::Runnable; | ||
| 8 | #pod use Moo; | ||
| 9 | #pod with 'Beam::Runnable'; | ||
| 10 | #pod sub run { ... } | ||
| 11 | #pod | ||
| 12 | #pod =head1 DESCRIPTION | ||
| 13 | #pod | ||
| 14 | #pod This role declares your object as runnable by the C<beam run> command. | ||
| 15 | #pod Runnable objects will be listed by the C<beam list> command, and their | ||
| 16 | #pod documentation displayed by the C<beam help> command. | ||
| 17 | #pod | ||
| 18 | #pod =head2 The C<run> method | ||
| 19 | #pod | ||
| 20 | #pod The C<run> method is the main function of your object. See below for its | ||
| 21 | #pod arguments and return value. | ||
| 22 | #pod | ||
| 23 | #pod The C<run> method should be as small as possible, ideally only parsing | ||
| 24 | #pod command-line arguments and delegating to other objects to do the real | ||
| 25 | #pod work. Though your runnable object can be used in other code, the API of | ||
| 26 | #pod the C<run> method is a terrible way to do that, and it is better to keep | ||
| 27 | #pod your business logic and other important code in another class. | ||
| 28 | #pod | ||
| 29 | #pod =head2 Documentation | ||
| 30 | #pod | ||
| 31 | #pod The C<beam help> command will display the documentation of your module: | ||
| 32 | #pod the C<NAME> (abstract), C<SYNOPSIS>, C<DESCRIPTION>, C<ARGUMENTS>, | ||
| 33 | #pod C<OPTIONS>, and C<SEE ALSO> sections. This is the same as what | ||
| 34 | #pod L<Pod::Usage> produces by default. | ||
| 35 | #pod | ||
| 36 | #pod The C<beam list> command, when listing runnable objects, will display | ||
| 37 | #pod either the C<summary> attribute or the C<NAME> POD section (abstract) | ||
| 38 | #pod next to the service name. | ||
| 39 | #pod | ||
| 40 | #pod =head2 Additional Roles | ||
| 41 | #pod | ||
| 42 | #pod Additional roles can add common functionality to your runnable script. | ||
| 43 | #pod Some of these are included in the C<Beam::Runner> distribution: | ||
| 44 | #pod | ||
| 45 | #pod =over | ||
| 46 | #pod | ||
| 47 | #pod =item L<Beam::Runnable::Timeout::Alarm> | ||
| 48 | #pod | ||
| 49 | #pod This role will add a timeout using Perl's built-in | ||
| 50 | #pod L<alarm()|perlfunc/alarm> function. Once the timeout is reached, the | ||
| 51 | #pod program will print a warning and exit with an error code. | ||
| 52 | #pod | ||
| 53 | #pod =back | ||
| 54 | #pod | ||
| 55 | #pod =head1 SEE ALSO | ||
| 56 | #pod | ||
| 57 | #pod L<beam>, L<Beam::Runner> | ||
| 58 | #pod | ||
| 59 | #pod =cut | ||
| 60 | |||
| 61 | 11 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 62 | 11 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 63 | 11 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 64 | 11 | with 'Beam::Service'; | |
| 65 | 11 | ▶ | use Types::Standard qw( Str ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 66 | |||
| 67 | #pod =attr summary | ||
| 68 | #pod | ||
| 69 | #pod A summary of the task to be run. This will be displayed by the C<beam | ||
| 70 | #pod list> command in the list. | ||
| 71 | #pod | ||
| 72 | #pod =cut | ||
| 73 | |||
| 74 | 11 | has summary => ( | |
| 75 | is => 'ro', | ||
| 76 | isa => Str, | ||
| 77 | ); | ||
| 78 | |||
| 79 | #pod =method run | ||
| 80 | #pod | ||
| 81 | #pod my $exit_code = $obj->run( @argv ); | ||
| 82 | #pod | ||
| 83 | #pod Execute the runnable object with the given arguments and returning the | ||
| 84 | #pod exit status. C<@argv> is passed-in from the command line and may contain | ||
| 85 | #pod options (which you can parse using L<Getopt::Long's GetOptionsFromArray | ||
| 86 | #pod function|Getopt::Long/Parsing options from an arbitrary array>. | ||
| 87 | #pod | ||
| 88 | #pod =cut | ||
| 89 | |||
| 90 | 11 | requires 'run'; | |
| 91 | |||
| 92 | 11 | 1; | |
| 93 | |||
| 94 | __END__ | ||