Data::ResultSet - Container for aggregating and examining multiple results
# Subclass the module package MyApp::ResultSet; use base qw( Data::ResultSet ); # Generate methods to wrap 'is_success' and 'is_error' __PACKAGE__->make_wrappers( qw( is_success is_error ) ); # And elsewhere... package MyApp; use MyApp::ResultSet; sub something { # Create a resultset object my $result = MyApp::ResultSet->new(); foreach my $thing ( @_ ) { # Add results of calling do_something() to the result # set $result->add( $thing->do_something(); ); } # Return the results return $result; } # And, check your results my $r = something( @some_data ); if( $r->all_success ) { # Only true if each result's ->is_success method returns true print "happiness and puppies!\n"; } elsif ( $r->all_error ) { # Only true if each result's ->is_error method returns true die 'Oh noes! Everything errored out!'; } else { foreach my $failed ( $r->list_not_success() ) { # Do something with each failed result } }
Data::ResultSet is a container object for aggregating and examining multiple results. It allows multiple result objects matching the same method signature to be returned as a single object that can then be queried for success or failure in a number of ways.
This is accomplished by generating wrappers to methods in the underlying list of result objects. For example, if you have a result object that has an is_ok() method, you can create a Data::ResultSet subclass to handle it with:
package MyApp::ResultSet; use base qw( Data::ResultSet ); __PACKAGE__->make_wrappers( 'is_ok' ); 1;
This will generate all_ok, has_ok, get_ok, and get_not_ok methods in MyApp::ResultSet that use the is_ok accessor on your result object.
all_ok
has_ok
get_ok
get_not_ok
is_ok
Creates a new Data::ResultSet object. Generally you will want to do this on a subclass, not on Data::ResultSet.
Generates all wrapper methods ( all_, has_, get_, get_not ) for the provided method names. The resulting wrapper will consist of the provided name and the appropriate prefix, with the exception that provided names beginning with is_ will have the is_ stripped first.
The wrappers can be generated individually using other methods (see below).
Generates the all_ wrapper method for each provided name.
all_
Generates the has_ wrapper method for each provided name.
has_
Generates the get_ wrapper method for each provided name.
get_
Generates the get_not_ wrapper method for each provided name.
get_not_
Adds an object to the result set. Returns $self.
Returns number of objects in the set.
Returns contents of set.
Clears contents of set. Returns true.
Generated method that returns true if the METHOD called on every object within the set returns true.
Generated method that returns true if one object within the set returns true for METHOD.
Generated method that returns all objects for which METHOD returns true.
Generated method that returns all objects for which METHOD returns false.
There are no known incompatibilities with this module.
The methods being wrapped shouldn't be anything more than simple accessors. They will get called an arbitrary number of times, so doing any real work, particularly anything that changes state or has side-effects, is a bad idea.
Please report any new problems to the author. Patches are welcome.
There are quite a few other packages on the CPAN for implementing polymorphic return values. You may wish to use one of these instead:
Class::ReturnValue
Return::Value
Contextual::Return
Dave O'Neill
Dianne Skoll (dianne@skoll.ca)
Copyright (c) 2007 Roaring Penguin Software, Inc. Copyright (c) 2022 Dianne Skoll
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Data::ResultSet, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::ResultSet
CPAN shell
perl -MCPAN -e shell install Data::ResultSet
For more information on module installation, please visit the detailed CPAN module installation guide.