1Applify(3) User Contributed Perl Documentation Applify(3)
2
3
4
6 Applify - Write object oriented scripts with ease
7
9 0.15
10
12 This module should keep all the noise away and let you write scripts
13 very easily. These scripts can even be unit tested even though they are
14 defined directly in the script file and not in a module.
15
17 #!/usr/bin/perl
18 use Applify;
19
20 option file => input_file => 'File to read from';
21 option dir => output_dir => 'Directory to write files to';
22 option flag => dry_run => 'Use --no-dry-run to actually do something', 1;
23
24 documentation __FILE__;
25 version 1.23;
26
27 sub generate_exit_value {
28 return int rand 100;
29 }
30
31 # app {...}; must be the last statement in the script
32 app {
33 my($self, @extra) = @_;
34 my $exit_value = 0;
35
36 print "Extra arguments: @extra\n" if(@extra);
37 print "Will read from: ", $self->input_file, "\n";
38 print "Will write files to: ", $self->output_dir, "\n";
39
40 if($self->dry_run) {
41 die 'Will not run script';
42 }
43
44 return $self->generate_exit_value;
45 };
46
48 This module will generate an application class, which $self inside the
49 "app" block refer to. This class will have:
50
51 · "new()"
52
53 An object constructor. This method will not be auto generated if any
54 of the classes given to "extends" has the method "new()".
55
56 · "run()"
57
58 This method is basically the code block given to "app".
59
60 · Other methods
61
62 Other methods defined in the script file will be accesible from $self
63 inside "app{}".
64
65 · "_script()"
66
67 This is an accessor which return the Applify object which is refered
68 to as $self in this documentation.
69
70 NOTE: This accessor starts with an underscore to prevent conflicts
71 with "options".
72
73 · Other accessors
74
75 Any "option" (application option) will be available as an accessor on
76 the application object.
77
79 option
80 option $type => $name => $documentation;
81 option $type => $name => $documentation, $default;
82 option $type => $name => $documentation, $default, @args;
83 option $type => $name => $documentation, @args;
84
85 This function is used to define options which can be given to this
86 application. See "SYNOPSIS" for example code. This function can also be
87 called as a method on $self.
88
89 · $type
90
91 Used to define value types for this input. Can be:
92
93 | $type | Example | Attribute value |
94 |-------|---------------------|-----------------|
95 | bool | --foo, --no-foo | foo=1, foo=0 |
96 | flag | --foo, --no-foo | foo=1, foo=0 |
97 | inc | --verbose --verbose | verbose=2 |
98 | str | --name batwoman | name=batwoman |
99 | int | --answer 42 | answer=42 |
100 | num | --pie 3.14 | pie=3.14 |
101
102 · $name
103
104 The name of an application option. This name will also be used as
105 accessor name inside the application. Example:
106
107 # define an application option:
108 option file => some_file => '...';
109
110 # call the application from command line:
111 > myapp.pl --some-file /foo/bar
112
113 # run the application code:
114 app {
115 my $self = shift;
116 print $self->some_file # prints "/foo/bar"
117 return 0;
118 };
119
120 · $documentation
121
122 Used as description text when printing the usage text.
123
124 · @args
125
126 · "required"
127
128 The script will not start if a required field is omitted.
129
130 · "n_of"
131
132 Allow the option to hold a list of values. Examples: "@", "4",
133 "1,3". See "Options-with-multiple-values" in Getopt::Long for
134 details.
135
136 · "isa"
137
138 Specify the class an option should be instantiated as. Example:
139
140 option file => output => "output file", isa => "Mojo::File";
141
142 The "output()" attribute will then later return an object of
143 Mojo::File, instead of just a plain string.
144
145 · Other
146
147 Any other Moose attribute argument may/will be supported in future
148 release.
149
150 documentation
151 documentation __FILE__; # current file
152 documentation '/path/to/file';
153 documentation 'Some::Module';
154
155 Specifies where to retrieve documentaion from when giving the "--man"
156 option to your script.
157
158 version
159 version 'Some::Module';
160 version $num;
161
162 Specifies where to retrieve the version number from when giving the
163 "--version" option to your script.
164
165 extends
166 extends @classes;
167
168 Specify which classes this application should inherit from. These
169 classes can be Moose based.
170
171 subcommand
172 subcommand list => 'provide a listing objects' => sub {
173 option flag => long => 'long listing';
174 option flag => recursive => 'recursively list objects';
175 };
176
177 subcommand create => 'create a new object' => sub {
178 option str => name => 'name of new object', required => 1;
179 option str => description => 'description for the object', required => 1;
180 };
181
182 sub command_create {
183 my ($self, @extra) = @_;
184 ## do creating
185 return 0;
186 }
187
188 sub command_list {
189 my ($self, @extra) = @_;
190 ## do listing
191 return 0;
192 }
193
194 app {
195 my ($self, @extra) = @_;
196 ## fallback when no command given.
197 $self->_script->print_help;
198 return 0;
199 };
200
201 This function allows for creating multiple related sub commands within
202 the same script in a similar fashion to "git". The "option", "extends"
203 and "documentation" exported functions may sensibly be called within
204 the subroutine. Calling the function with no arguments will return the
205 running subcommand, i.e. a valid $ARGV[0]. Non valid values for the
206 subcommand given on the command line will result in the help being
207 displayed.
208
209 app
210 app CODE;
211
212 This function will define the code block which is called when the
213 application is started. See "SYNOPSIS" for example code. This function
214 can also be called as a method on $self.
215
216 IMPORTANT: This function must be the last function called in the script
217 file for unit tests to work. Reason for this is that this function runs
218 the application in void context (started from command line), but
219 returns the application object in list/scalar context (from "do" in
220 perlfunc).
221
223 options
224 $array_ref = $self->options;
225
226 Holds the application options given to "option".
227
229 new
230 $self = $class->new({ options => $array_ref, ... });
231
232 Object constructor. Creates a new object representing the script meta
233 information.
234
235 print_help
236 Will print "options" to selected filehandle (STDOUT by default) in a
237 normalized matter. Example:
238
239 Usage:
240 --foo Foo does this and that
241 * --bar Bar does something else
242
243 --help Print this help text
244 --man Display manual for this application
245 --version Print application name and version
246
247 print_version
248 Will print "version" to selected filehandle (STDOUT by default) in a
249 normalized matter. Example:
250
251 some-script.pl version 1.23
252
253 import
254 Will export the functions listed under "EXPORTED FUNCTIONS". The
255 functions will act on a Applify object created by this method.
256
258 This library is free software. You can redistribute it and/or modify it
259 under the same terms as Perl itself.
260
262 Jan Henning Thorsen - "jhthorsen@cpan.org"
263
264 Roy Storey - "kiwiroy@cpan.org"
265
266
267
268perl v5.28.1 2018-06-06 Applify(3)