Flow(3pm) | User Contributed Perl Documentation | Flow(3pm) |
Data::Flow - Perl extension for simple-minded recipe-controlled build of data.
use Data::Flow; $recipes = { path => { default => './MANIFEST'}, contents => { prerequisites => ['path', 'x'] , process => sub { my $data = shift; $data->{ shift() } = `cat $data->{'path'}` x $data->{'x'}; } }, }; $request = new Data::Flow $recipes; $request->set( x => 1); print $request->get('contents'); tie %request, Data::Flow, $recipes; $request{x} = 1; print $request{contents};
The module Data::Flow provides its services via objects. The objects may be obtained by the usual
$request = new Data::Flow $recipes;
paradigm. The argument $recipes is a hash reference, which provides the rules for request processing. The objects support three methods, set(), get(), aget(), and already_set(). The first one is used to provide input data for processing, the second one to obtain the output. The third one to obtain a reference to an array with results of repeated get(), and the last one to query whether a field is already known.
The unit of requested information is a field. The method set() takes a pair "field => value", the methods get() and already_set() take one argument: the "field", and the method aget() takes multiple fields.
Every object is created without any fields filled, but it knows how to construct fields basing on other fields or some global into. This knowledge is provided in the argument $recipe of the new() function. This is a reference to a hash, keyed by fields. The values of this hash are hash references themselves, which describe how to acquire the field which is the corresponding key of the initial hash.
The internal hashes may have the following keys:
default => $Config{installdir}
If "defaults" did not satisfy the request for a field, but "$recipe->{field}{prerequisites}" exists, the required fields are build before any further processing is done. Example:
prerequisites => [ qw(prefix arch) ]
process => sub { my $data = shift; $data->{time} = localtime(time) } }
oo_process => sub { my $data = shift; $data->set( time => localtime(time) ) }
output => sub { localtime(time) }
output => sub { my $self = shift; $self->get('r') . localtime(time) }
filter => [ sub { shift + shift }, 'first_half', 'second_half' ]
Note that the mentioned field will be automatically marked as prerequisites.
self_filter => [ sub { my ($self, $first_half = (shift, shift); $first_half *= -$self->get('total')*100 if $first_half < 0; # negative means percentage $first_half + shift }, 'first_half', 'second_half' ]
class_filter => [ 'new', 'FileHandle', 'filename' ]
method_filter => [ 'show', 'widget_name', 'current_display' ]
The access to the same functionality is available via tied hash interface.
Ilya Zakharevich, cpan@ilyaz.org, with multiple additions from Terrence Monroe Brannon and Radoslav Nedyalkov.
perl(1), make(1).
2022-10-13 | perl v5.36.0 |