App-CLI-Extension

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


1.42  Mon Sep 19 16:08:26 JST 2011
        - update Makefile.PL
1.41  Mon Sep 19 10:19:51 JST 2011
        - update Changes
1.4   Mon Sep 19 10:14:08 JST 2011
        - change dependencies module Class::Accessor::Grouped (before Class::Data::Accessor)
1.3   Sun Jul 18 18:14:44 2010
        - changed the implementation of exception handling
        - add A::C::E::Exception. exception module
        - add throw method (A::C::E::Component::ErrorHandler)
        - add e / finished method (A::C::E::Component::RunCommand)
1.21  Tue Jan  5 01:39:09 2010
        - mini fix
1.2   Sun Jan  3 22:53:02 2010
        - add exit_value method(A::C::E::Component::RunCommand)
1.1   Sat Dec 13 02:45:21 2009
        - mini fix
1.0   Sat Dec 13 02:45:21 2009
        - add A::C::E::Component::RunMethod
        - add A::C::E::Component::InstallCallback (before A::C::P::InstallCallback)

README.pod  view on Meta::CPAN

      $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 {
  

README.pod  view on Meta::CPAN

  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

inc/Module/Install.pm  view on Meta::CPAN

	# is unreliable on some platforms and requires write permissions)
	# for now we should catch this and refuse to run.
	if ( -f $0 ) {
		my $s = (stat($0))[9];

		# If the modification time is only slightly in the future,
		# sleep briefly to remove the problem.
		my $a = $s - time;
		if ( $a > 0 and $a < 5 ) { sleep 5 }

		# Too far in the future, throw an error.
		my $t = time;
		if ( $s > $t ) { die <<"END_DIE" }

Your installer $0 has a modification time in the future ($s > $t).

This is known to create infinite loops in make.

Please correct this, then run $0 again.

END_DIE

lib/App/CLI/Extension.pm  view on Meta::CPAN

      $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 {
  

lib/App/CLI/Extension.pm  view on Meta::CPAN

  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

lib/App/CLI/Extension/Component/ErrorHandler.pm  view on Meta::CPAN

1.421

=cut

use strict;
use App::CLI::Extension::Exception;
use Error;

our $VERSION  = '1.421';

sub throw {

	my($self, $message) = @_;
	Error::throw App::CLI::Extension::Exception $message;
}

1;

__END__

=head1 SEE ALSO

L<App::CLI::Extension>

lib/App/CLI/Extension/Component/RunCommand.pm  view on Meta::CPAN


	try {
		$self->setup(@argv);
		$self->prerun(@argv);
		if ($self->finished == 0) {
			$self->run(@argv);
			$self->postrun(@argv);
		}
	}
	catch App::CLI::Extension::Exception with {
		# $self->e is App::CLI::Extension::Exception object. execute $self->throw($message)
		$self->e(shift);
		$self->exit_value($FAIL_EXIT_VALUE);
		$self->fail(@argv);
	}
	otherwise {
		# $self->e is Error::Simple object
		$self->e(shift);
		$self->exit_value($FAIL_EXIT_VALUE);
		$self->fail(@argv);
	}

t/12_fail_exception_package1.t  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use Test::More tests => 1;
use File::Spec;
use lib qw(t/lib);
use MyAppFailPackage;

our $RESULT;
my @argv = ("raiseerror", "--throw");

{
    local *ARGV = \@argv;
    MyAppFailPackage->dispatch;
}

ok($RESULT eq "App::CLI::Extension::Exception");

t/lib/MyAppFail/FailTest.pm  view on Meta::CPAN

package MyAppFail::FailTest;

use strict;
use base qw(App::CLI::Command);

sub run {

    my($self, @args) = @_;
    $self->throw("dying message");
}
1;

t/lib/MyAppFailPackage/RaiseError.pm  view on Meta::CPAN

package MyAppFailPackage::RaiseError;

use strict;
use base qw(App::CLI::Command);

sub options {

	return ("throw" => "throw");
}

sub run {

    my($self, @args) = @_;
	my $message = "dying message";
	if (defined $self->{'throw'}) {
    	$self->throw($message);
	} else {
		die $message;
	}
}

sub fail {
	
    my($self, @args) = @_;
	$main::RESULT = ref($self->e);
}



( run in 0.293 second using v1.01-cache-2.11-cpan-496ff517765 )