=pod

=for vim
   vim: tw=72 ts=3 sts=3 sw=3 et ai :

=encoding utf8

=head1 NAME

Data::Tubes - Text and data canalising

=head1 VERSION

This document describes Data::Tubes version 0.738.

=begin html

<a href="https://travis-ci.org/polettix/Data-Tubes">
<img alt="Build Status" src="https://travis-ci.org/polettix/Data-Tubes.svg?branch=master">
</a>

<a href="https://www.perl.org/">
<img alt="Perl Version" src="https://img.shields.io/badge/perl-5.10+-brightgreen.svg">
</a>

<a href="https://badge.fury.io/pl/Data-Tubes">
<img alt="Current CPAN version" src="https://badge.fury.io/pl/Data-Tubes.svg">
</a>

<a href="http://cpants.cpanauthors.org/dist/Data-Tubes">
<img alt="Kwalitee" src="http://cpants.cpanauthors.org/dist/Data-Tubes.png">
</a>

<a href="http://www.cpantesters.org/distro/D/Data-Tubes.html?distmat=1">
<img alt="CPAN Testers" src="https://img.shields.io/badge/cpan-testers-blue.svg">
</a>

<a href="http://matrix.cpantesters.org/?dist=Data-Tubes">
<img alt="CPAN Testers Matrix" src="https://img.shields.io/badge/matrix-@testers-blue.svg">
</a>

=end html

=head1 SYNOPSIS

   use Data::Tubes qw< pipeline >, -api => '0.736';

   my $id = 0;
   my $tube = sequence(
      # automatic loading for simple cases
      'Source::iterate_files', # plugin to handle input files
      'Reader::by_line',       # plugin to read line by line
      'Parser::hashy',         # plugin to parse hashes

      # some operations will require some explicit coding of a tube
      # which is a sub ref with a contract on the return value
      sub {
         my $record = shift;
         $record->{structured}{id} = $id++;
         return $record;
      },

      # automatic loading, but with arguments
      [ # plugin to render stuff using Template::Perlish
         'Renderer::with_template_perlish',
         template => "[% a %]:\n  id: [% id %]\n  meet: [% b %]\n",
      ],
      [ # plugin to write stuff into output files, flexibly
         'Writer::to_files',
         filename => \*STDOUT,
         header   => "---\n",
         footer   => "...\n"
      ],

      # options for pipeline, in this case just pour into the sink
      {tap => 'sink'}
   );

   my $input = <<'END';
   a=Harry b=Sally
   a=Jekyll b=Hide
   a=Flavio b=Silvia
   a=some b=thing
   END
   $tube->([\$input]);

   ###############################################################

   # a somewhat similar example, with different facilities
   use Data::Tubes qw< drain summon >;

   # load components from relevant plugins
   summon(
      qw<
         Plumbing::sequence
         Source::iterate_files
         Reader::read_by_line
         Parser::parse_hashy
         Renderer::render_with_template_perlish
         Writer::write_to_files
         >
   );

   # define a sequence of tubes, they're just a bunch of sub references
   my $sequence = sequence(
      iterate_files(files => [\"n=Flavio|q=how are you\nn=X|q=Y"]),
      read_by_line(),
      parse_hashy(chunks_separator => '|'),
      render_with_template_perlish(template => "Hi [% n %], [% q %]?\n"),
      write_to_files(filename => \*STDOUT),
   );

   # run it, forget about what comes out of the end
   drain($sequence);

=head1 DESCRIPTION

This module allows you to define and manage I<tubes>, which are
transformation subroutines over records.

B<NOTE>: this software is usable but still in a state of flux with
respect to the interface. Most notably, although the provided plugins
are mostly stable, the accepted parameters' names might change to gain
greater consistency across the whole codebase. As an example, passing
options to sub-module C<foo> might be done via option C<foo_opt> in one
function, and via option C<opts_for_foo> in another, which is ugly and
likely to be changed to have only one single way.

B<NOTE>: to try and mitigate the previous statement, whenever possible
API changes will be versioned, so that both an "old" and the "new"
behaviour will be possible. See L</API Versioning> for the details.
Bottom line: always declare your C<-api> when loading C<Data::Tubes>!

=head2 First Things First: What's a I<Tube>?

A sort of operative definition in code:

   my @outcome = $tube->($input_record);
   if (scalar(@outcome) == 0) {
      print "nothing came out, input record was digested!\n";
   }
   elsif (scalar(@outcome) == 1) {
      my $output_record = shift @outcome;
   }
   else {
      my ($type, $value) = @outcome;
      if ($type eq 'records') {
         my @output_records = @$value;
      }
      elsif ($type eq 'iterator') {
         while (my ($output_record) = $iterator->()) {}
      }
      else {
         die "sorry, this tube's output was not valid!\n";
      }
   }

A I<tube> is a reference to a subroutine that accepts a single, scalar
C<$input_record> and can return zero, one or two (or more) values.

In particular:

=over

=item *

if it returns zero values, then the I<tube> just hasn't anything to
emit for that particular input record. The reasons depend on the tube,
but this is a perfectly valid outcome;

=item *

if it returns one single value, that is the C<$output_record>
corresponding to the C<$input_record>. This is probably the most common
case;

=item *

if it returns two (or more) values, the first one will tell you what is
returned (i.e. its I<type>, and the second will be some way to get the
return value(s). This is what you would use if a single C<$input_record>
can potentially give birth to multiple output records, like this:

=over

=item *

if you can/want to compute all the output records right away (e.g. you
just to need to C<split> something in the input record), you can use
C<records> for I<type> and pass a reference to an array as the second
output value (each of them will be considered an output record);

=item *

if you cannot (or don't want to) compute all the output records, e.g.
because they might just blow out your process' memory, you can use
I<type> C<iterator> and return a subroutine reference back. This
subroutine MUST be such that repeatingly calling it can yield two
possible results:

=over

=item *

one single element, that is the I<next> output record, OR

=item *

the empty list, that signals that the iterator has been emptied.

=back

=back

=back

This is all that is assumed about tubes in the general case. Some
plugins will make further assumptions about what's expected as an input
record (e.g. a hash reference in most of the cases) or what is provided
as output records, but the generic case is all in the above definition.

A few examples will help at this point.

=head3 A simple I<filter> tube

This is probably the most common type of tube: one record comes in, one
comes out. In the example, we will assume the input record is a string,
and will transform sequences of spacing characters into single spaces:

   my $tube = sub {
      my $text = shift;
      $text =~ s{\s+}{ }gmxs;
      return $text;
   };

=head3 A C<grep>-like tube

This is a tube that might potentially I<digest> the input record,
providing nothing out. In the example, we will assume that we're
focusing on valid non-negative integers only, and we will ignore
everything else:

   my $tube = sub {
      my $number = shift;

      # caution! A simple "return" is much more different than
      # "return undef", the first one is what we need to provide
      # "nothing" as output in the list context!
      return unless defined $number; # ignore input undef:s
      return unless $number =~ m{\A (?: 0 | [1-9]\d* ) \z}mxs;

      # this record passed all check, let's return it
      return $number;
   };


=head3 A few little children out of your input

This is a tube that will typically generate a few output records from an
input one. It's best suited to be used when you know that you have
control over the number of output records, and they will not make your
memory consumption explode. In the example, we will provide "words" from
a text as output records:

   my $tube = sub {
      my $text = shift;
      my @words = split /\W+/mxs, $text;
      return (records => \@words);
   };

=head3 Turning a filename into lines

This is a tube that might generate a lot of records out of a single
input one, so it's your best choice when you don't feel too confortable
with using the C<records> alternative above. In the example, we will
turn an input file name into a sequence of lines from that file:

   my $tube = sub {
      my $filename = shift;
      open my $fh, '<', $filename or die "open('$filename'): $!";

      # the iterator is a reference to a sub, no input parameters
      my $iterator = sub {
         my ($line) = <$fh> or return;
         return $line;
      };
   };

=head2 How Can Data::Tubes Help Me Then?

Data::Tubes can help you out in different ways:

=over

=item *

it provides you with a definition of tube (i.e. a I<transforming
function>) that will help you control what you're doing. We already
talked about this format, just take a look at
L<< /First Things First: What's a I<Tube>? >>

=item *

it gives you some I<plumbing> facilities to easily perform some common
actions over tubes, e.g. put them in sequence or dispatch an input
record to the right tube. This is the kind of stuff that you can find in
L<Data::Tubes::Plugin::Plumbing>;

=item *

it gives you a library of pre-defined tube types that will help you with
common tasks related to transforming input data in output data (e.g. in
some kind of I<Extract-Transform-Load> process). This is what you can
find in the I<Data::Tubes::Plugin> namespace!

=back

This module provides you a few useful facilities to make using tubes
easier. In particular:

=over

=item *

most of the times you should be interested into L</pipeline>, as it will
help you building a sequence of tubes and manage the output of the
overall sequence automatically (e.g. just drain it into the I<sink>,
after all records have been processed by the different tubes in the
sequence;

=item *

if for some reason you need to load a tube's factory, you can use
L</summon>, that basically does what C<import> usually does, but with
some additional DWIM-mery;

=item *

if you have a tube and you want to call it on some input, but you don't
care about what will get out, you can use L</drain>. This is
particularly useful if you know (or suspect) that the tube will return
an iterator (like L<Data::Tubes::Plugin::Plumbing/sequence>)
because L</drain> will ensure that the iterator is run until it is
exhausted.

=back

=head2 API Versioning

As of release 0.736, an experimental API versioning mechanism is
introduced to cope with interface changes. This should allow to keep
both "old" and "new" behaviours when there is a change in e.g. the input
parameters of a function, or what it returns in different contexts. Of
course this kind of "backwards compatibility" might not be possible all
times, in which case a regular deprecation cycle will be adopted or the
backwards incompatibility stressed loudly (starting with a major version
number change).

The mechanism is simple and is centered on package variable
C<$Data::Tubes::API_VERSION>, which by default is initialized with the
I<current> version (i.e. whatever C<$Data::Tubes::VERSION> is set to).
If you set a version value, the API SHOULD be compliant to what was
available at that specific version.

For example, in version 0.736 the function L</drain> below was changed
to expose a totally consistent behaviour when providing output in scalar
context. This new behaviour is used only if C<$Data::Tubes::API_VERSION>
is (lexicographically) greater than, or equal to, the string C<0.736>;
otherwise, the old behaviour applies.

You can set the api version value while importing the module, like this:

   use Data::Tubes -api => '0.734', @other_imports;

This will initialize C<$Data::Tubes::API_VERSION> to whatever you
provide. Order is not important but it is mandatory that you provide a
parameter if you pass option C<-api>.

Note that the API Versioning mechanism is dynamically triggered every
time, so you can e.g. do this:

   # import "drain()" with the new behaviour in 0.736
   use Data::Tubes qw< drain >, -api => '0.736';

   # use "drain()", but with the previous behaviour
   {
      local $Data::Tubes::API_VERSION = '0.734';
      my $whatever = drain($tube, @some_input);
   }

   # use "drain()", with the 0.736 behaviour
   my $whatever = drain($tube, @some_input);

In general, it's advised to always explicitly set your intentions
related to the API version you want to use, so that you will likely not
be biten by interface changes upon upgrades.

=head1 FUNCTIONS

=head2 B<< drain >>

   drain($tube, @tube_inputs);

drain whatever comes out of a tube. The tube is run with the provided
inputs, and if an iterator comes out of it, it is repeatedly run until
it provides no more output records. This is useful if the tube returns
an iterator, as it will be exhausted.

Returns different things depending on the calling context:

=over

=item *

in I<void> context, nothing is returned;

=item *

in I<scalar> context it always returns an array reference containing the
whole sequence of output records.

This behaviour is valid as of release C<0.736>, see below for a
description of the previous behaviours and L</API Versioning> for a way
to trigger them.

=item *

In I<list> context, it always returns a sequence of output records.

=back

Versioning notes (see L</API Versioning>):

=over

=item *

up to, and including, release C<0.734>, the behaviour of this function
when called in I<scalar> context was the following:

=over

Different things are returned depending on what the C<$tube> returns. If
it returns a single item (i.e. a record), it is returned back. If it
returns the string C<records> and an array reference, the array
reference is returned. If it returns an iterator, an array reference
with all the output records produced by the iterator is returned.

Note that the I<scalar> context requires you to know precisely what your
tube provides back, otherwise you might not know if what you are getting
back is a single record or an array reference with the records inside.

=back

=back

=head2 B<< pipeline >>

   $pl = pipeline(@tubes); # OR
   $pl = pipeline(@tubes, \%args);

build up a pipeline (sequence) of C<@tubes>, possibly with options in
C<%args>. This is actually only little more than a wrapper around
L<Data::Tubes::Plugin::Plumbing/sequence>.

The C<@tubes> are passed to
L<Data::Tubes::Plugin::Plumbing/sequence> as parameter C<tubes>. Basically,
Each item in it must be either a tube itself or something that can be
transformed into a tube via L</tube> below.

An optional last parameter allows you to specify additional options:

=over

=item C<gate>

a sub ref that is called over each intermediate record to establish if
it can continue down the sequence or it should be returned immediately,
depending on the truth of the returned value. See
L<Data::Tubes::Plugin::Plumbing/sequence>;

=item C<prefix>

an alternative prefix to be used whenever L<Data::Tubes::Util/load_sub>
is called behind the scenes during this invocation;

=item C<pump>

set a sub ref that will be called on the output stream from the
sequence. In particular, the output iterator from the C<sequence> is
repeatedly called to get an output record, and this record is fed into
the C<pump> sub ref;

=item C<tap>

set to either an allowed string or to a subroutine ref. In the second
case, the output iterator will be fed into the provided subroutine
reference, that will have to use it as it sees fit. Note that this
C<tap> will always be provided with an iterator, which means that it
MUST be exhausted in order to actually make the whole pipeline work.

You can also set this to one of the allowed strings, which will generate
a suitable tap for you:

=over

=item C<array>

available as of release 0.736, transforms the input iterator in an array
reference with all return values inside. Differently from C<bucket>,
only the array reference is returned.

=item C<bucket>

available as of release 0.732, transforms the input iterator into one of
the other allowed return values for a valid tube (i.e. the empty list, a
single output record, or a string `records` followed by an array
reference holding the output records). This is useful if you are
interested into what goes out of the pipeline, but you don't want the
delayed processing provided by the iterator.

=item C<first>

available as of release 0.736, gets the first record from the input
iterator and returns it (turning the pipeline into a I<simple> tube that
only returns one record). Please note that C<undef> will be returned if
there is no record in the iterator, so this I<tap> does not allow
distinguishing an undefined record from a missing one (which becomes
relevant only if you are anticipating undefined records, of course).

=item C<sink>

this allows you to exhaust the iterator tossing the outcoming records
away. This is what you usually want in some *outer* pipeline, when you
are not interested in the records that go out of the pipeline because...
you already did all that you needed to do;

=back

=back

If C<tap> is present, C<pump> is ignored.

The returned value is always a subroutine reference. If neither C<tap>
nor C<pump> are present, the returned sub reference is a tube resulting
from the sequence or provided tubes, so you can use it as any other
tube. Otherwise, the returned sub reference will take care of invoking
the sequence for you with the parameters you provide, and will then pass
the iterator to the provided C<tap>/C<pump> as explained above.

Examples (the following alternatives all do the same thing, mostly):

   # no options, what comes back is just a plain tube
   $sequence = pipeline($tube1, $tube2, $tube3);
   (undef, $it) = $sequence->($record);
   # so far, nothing really happened because we have to run
   # the iterator until it's exhausted
   while (my ($record) = $it->()) { ... }

   # set a "sink" tap, we don't care about returned records
   $handler = pipeline($tube1, $tube2, $tube3, {tap => 'sink'});
   $handler->($record); # this will exhaust the iterator

   # set an explicit tap
   $handler = pipeline(
      $tube1, $tube2, $tube3,
      {
         tap => sub {
            my $iterator = shift;
            while (my ($record) = $iterator->()) { ... }
         }
      }
   );
   $handler->($record); # the tap will exhaust the iterator

   # set a pump
   $handler = pipeline(
      $tube1, $tube2, $tube3,
      {
         pump => sub {
            my $record = shift;
            ...
         }
      }
   );
   $handler->($record); # the pump will exhaust the iterator

=head2 B<< summon >>

   # Direct function import
   summon('Some::Package::subroutine');

   # DWIM, treat 'em as plugins under Data::Tubes::Plugin
   summon(
      [ qw< Plumbing sequence logger > ],
      'Reader::read_by_line',
      \%options,
   );

summon operations, most likely from plugins.  This is pretty much the
same as a regular C<import> done by C<use>, only supposed to be easier
to use in a script.

You can pass different things:

=over

=item I<array references>

the first item in the array will be considered the package name, the
following ones sub names inside that package;

=item I<strings>

this will be considered a fully qualified sub name, i.e. including the
package name at the beginning.

=back

The package name will be subject to some analysis that will make using
it a bit easier, by means of L<Data::Tubes::Util/resolve_module>.

You can optionally pass a hash reference with options as the last
parameter, with the following options:

=over

=item C<package>

the package where the loaded sub should be imported. Defaults to the
package calling the C<summon> function;

=item C<prefix>

the prefix to apply when needed. Defaults to C<Data::Tubes::Plugin>.
Note that you MUST NOT put the C<::>, it will be added automatically.

=back

=head2 B<< tube >>

   $tube = tube($factory_locator, @parameters); # OR
   $tube = tube(\@factory_locator, @parameters); # OR
   $tube = tube(\%opts, $factory_locator, @parameters); # OR
   $tube = tube(\%opts, \@factory_locator, @parameters);

this allows you to facilitate the creation of a tube, doing most of the
heavy-lifting automatically.

The first parameter can optionally be a I<hash reference> of options.
Currently, the only supported option is C<prefix>, which allows you to
set an alternative prefix with respect to what
C<Data::Tubes::Util/load_sub> would assume by default.

The following (or first, if C<%opts> is missing) parameter is used as a
I<locator> of a factory method to generate the real tube. It can be
either a string, or an array reference containing two elements, a
package name and a subroutine name inside that package. The function
L<Data::Tubes::Util/load_sub> is used to load the factory method
automatically, which means that the package name is subject to the same
rules described in L</summon> above.

After the factory function is loaded, it is called with the provided
C<@parameters> and the returned value... returned back.

Hence, this is a quick way to load some factory from a plugin and call
it in one, single call:

   # no additional parameters
   $files = tube('Source::iterate_files');

   # set some parameters for iterate_files
   $files = tube('Source::iterate_files', binmode => ':raw');

Most of the times, you are probably looking for L</pipeline> above,
as that will eventually call C<tube> automatically.

=head1 BUGS AND LIMITATIONS

Report bugs through GitHub (patches welcome) at
L<https://github.com/polettix/Data-Tubes>.

=head1 AUTHOR

Flavio Poletti <polettix@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2022 by Flavio Poletti <polettix@cpan.org>

This module is free software. You can redistribute it and/or modify it
under the terms of the Artistic License 2.0.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut
