App-CLI-Extension
view release on metacpan or search on metacpan
lib/App/CLI/Extension.pm view on Meta::CPAN
=head2 exec_callback
execute callback
Example:
$self->execute_callback("some_phase");
# some_phase method method No.1
# some_phase method method No.2
$self->execute_callback("any_phase", qw(one two three));
# any_phase args: one two three
=head2 exists_callback
exists callback check
Example:
if ($self->exists_callback("some_phase")) {
$self->exec_callback("some_phase");
} else {
die "some_phase is not exists callback phase";
}
=head2 exit_value
set exit value
Example:
# program exit value is 1(ex. echo $?)
$self->exit_value(1);
=head2 finished
setup or prepare phase and 1 set, run and postrun phase will not run. default 0
Example:
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use base qw(App::CLI::Command);
sub prerun {
my($self, @args) = @_;
$self->finished(1);
}
# non execute
sub run {
my($self, @args) = @_;
print "hello\n";
}
=head2 throw
raises an exception, fail phase transitions
Example:
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use base qw(App::CLI::Command);
sub run {
my($self, @args) = @_;
my $file = "/path/to/file";
open my $fh, "< $file" or $self->throw("can not open file:$file");
while ( my $line = <$fh> ) {
chomp $line;
print "$line\n";
}
close $fh;
}
# transitions fail phase method
sub fail {
my($self, @args) = @_;
# e is App:CLI::Extension::Exception instance
printf "ERROR: %s", $self->e;
printf "STACKTRACE: %s", $self->e->stacktrace;
}
# myapp
#!/usr/bin/perl
use strict;
use MyApp;
MyApp->dispatch;
# execute
[kurt@localhost ~] myapp hello
ERROR: can not open file:/path/to/file at lib/MyApp/Throw.pm line 10.
STACKTRACE: can not open file:/path/to/file at lib/MyApp/Throw.pm line 10
MyApp::Throw::run('MyApp::Throw=HASH(0x81bd6b4)') called at /usr/lib/perl5/site_perl/5.8.8/App/CLI/Extension/Component/RunCommand.pm line 36
App::CLI::Extension::Component::RunCommand::run_command('MyApp::Throw=HASH(0x81bd6b4)') called at /usr/lib/perl5/site_perl/5.8.8/App/CLI/Extension.pm line 177
App::CLI::Extension::dispatch('MyApp') called at ./myapp line 7
when you run throw method, App::CLI::Extension::Exception instance that $self->e is set to.
App::CLI::Extension::Exception is the Error::Simple is inherited. refer to the to documentation of C<Error>
throw method without running CORE::die if you run the $self->e is the Error::Simple instance will be set
=head2 e
App::CLI::Extension::Exception or Error::Simple instance. There is a ready to use, fail phase only
=head1 RUN PHASE METHOD
=head2 setup
=head2 prerun
=head2 postrun
=head2 finish
program last phase. By default, the exit will be executed automatically, exit if you do not want the APPCLI_NON_EXIT environ valiable how do I set the (value is whatever)
=head2 fail
error phase. default exit value is 255. if you want to change exit_value, see exit_value manual
=cut
1;
__END__
=head1 SEE ALSO
L<App::CLI> L<Class::Accessor::Grouped> L<UNIVERSAL::require>
=head1 AUTHOR
Akira Horimoto
=head1 COPYRIGHT AND LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
Copyright (C) 2009 Akira Horimoto
=cut
( run in 0.584 second using v1.01-cache-2.11-cpan-39bf76dae61 )