The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Class::Action::Step - Base class for use by Class::Action "step" objects

VERSION

This document describes Class::Action::Step version 0.1

SYNOPSIS

    package MyAction;
        
    require Class::Action::Step;
    @MyAction::ISA = qw(Class::Action::Step);

    sub get_class_action_steps {
        my ($class, @args) = @_; 
        return [MyAction::Foo->new(@args), MyAction::Bar->new(@args)];
    }
   
    package MyAction::Foo;
    @MyAction::Foo::ISA = qw(MyAction);
   
    sub new { ... 
   
    package MyAction::Bar;
    @MyAction::Bar::ISA = qw(MyAction);
   
    sub new { ... 
   
   
    1;

Then in your script:

    use Class::Action;
    use MyAction;
    my $action = Class::Action->new();
    $action->set_steps_from_class('MyAction');
    $action->execute("3.14159") or die $action->get_errstr();
    $action->execute("1.61803") or die $action->get_errstr();

or wrap it up even more w/ something like this:

   package MyAction;
   
   ...
   
   use Class::Action;
   
   sub get_action_object {
       my $action = Class::Action->new();
       $action->set_steps_from_class(@_);
       return $action;
   }
   
   sub get_class_action_steps {
   
   ...
 

Then in your script:

    use MyAction;
    my $action = MyAction->get_action_object();
    $action->execute("3.14159") or die $action->get_errstr();
    $action->execute("1.61803") or die $action->get_errstr();

DESCRIPTION

This module contains definitions for all the necessary methods a "step" class needs. The intent is to be used as base class so that all neccessary methods exist.

INTERFACE

'step_stack' method get_class_action_steps()

This method needs defined in Your::Class::Here in order to be passed to $action->set_steps_from_class('Your::Class::Here')

It should return an array or array reference of objects and/or name spaces that are Class::Action::Step compatible.

   $action->set_steps_from_class('Your::Class::Here');

essentially calls:

   Your::Class::Here->get_class_action_steps()
   

Additional arguments are passed through:

   $action->set_steps_from_class('Your::Class::Here',1,2,3);

essentially calls:

   Your::Class::Here->get_class_action_steps(1,2,3)

If it returns a namespace then that namespace's new() is called when it is run in execute() and rollback().

Step::NS->new() is called with the arguments passed to execute() and rollback()

Class::Action::Step compatible

The following methods are used in Class::Action->execute() and Class::Action->rollback() and therefore need defined in your "step" classes.

Mandatory methods

These have to be defined in your "step" class. If they are not then it will carp and return nothing.

new()

This should return an object that represents this "step".

clone_obj()

Takes no arguments, should return an identical but independent object in a fresh state.

state()

Takes no arguments, should return string or data structure reference representing the "state" (e.g. any important messages and status that you might want to examine after reset_obj_state() has wiped the object clean).

reset_obj_state()

Takes no arguments, resets the internal state of the object, called in void context.

execute()

The first argument (after the object itself of course) is a hash reference of "global" data that each "step" object can modify in order to aggregate data, report, back and communicate between each other.

The remaining arguments are the arguments passed to the main Class::Action object's execute() method.

It should return true if it was successful, false otherwise.

    sub execute {
        my ($step_obj, $global_data_hr, @args_to_execute) = @_;
        
        $global_data_hr->{'foo'} = _find_foo($args_to_execute[1]);
        
        return 1 if $global_data_hr->{'foo'} eq 'bar';
        
        $step_obj->{'last_errstr'} = 'foo did not equal bar';
        return;
    }

Optional Methods

retry_execute()

This method is called when the step's execute() fails. It is intended as a way to try and address why it failed and then return a boolean of if the step's execute() should be tried again.

The first argument (after the object itself of course) is a hash reference of "global" data that each "step" object can modify in order to aggregate data, report, back and communicate between each other.

The rest of the arguments are what were passed to execute().

clean_failed_execute()

This method is called when the step's execute() fails and we will not be retrying it again. It is intended as a way to try and address why it failed. It is called in void context.

The first argument (after the object itself of course) is a hash reference of "global" data that each "step" object can modify in order to aggregate data, report, back and communicate between each other.

The rest of the arguments are what were passed to execute().

undo()

This is equivalent to $step_obj->execute() except that it should undo what execute() does. Return true to continue the undo stack, false to stop.

retry_undo()

This is equivalent to $step_obj->retry_execute() except that it's return value indicates whether we should try to run the $step_obj->undo() that failed again.

clean_failed_undo()

This is equivalent to $step_obj->clean_failed_execute() except that it relate's to the entire rollback/undo process instead of execute process.

DIAGNOSTICS

CLASS does not implement METHOD()

Your CLASS needs to define the METHOD() method.

CONFIGURATION AND ENVIRONMENT

Class::Action::Step requires no configuration files or environment variables.

DEPENDENCIES

None.

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-class-action@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Daniel Muey <http://drmuey.com/cpan_contact.pl>

LICENCE AND COPYRIGHT

Copyright (c) 2009, Daniel Muey <http://drmuey.com/cpan_contact.pl>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.