Data-Unixish
view release on metacpan or search on metacpan
lib/Data/Unixish.pm view on Meta::CPAN
# whether function returns an array, a list, a filehandle, or calls a callback.
# If filehandle is chosen as output, a child process is forked to process input
# as requested.
use Data::Unixish qw(
aduxa cduxa fduxa lduxa
aduxc cduxc fduxc lduxc
aduxf cduxf fduxf lduxf
aduxl cduxl fduxl lduxl
siduxs
); # or you can use :all to export all functions
# apply function, without argument
my @out = lduxl('sort', 7, 2, 4, 1); # => (1, 2, 4, 7)
my $out = lduxa('uc', "a", "b", "c"); # => ["A", "B", "C"]
my $res = fduxl('wc', "file.txt"); # => "12\n234\n2093" # like wc's output
# apply function, with some arguments
my $fh = fduxf([trunc => {width=>80, ansi=>1, mb=>1}], \*STDIN);
say while <$fh>;
# apply function to a single item, function must be itemfunc
my $res = duxitem(, $item);
# apply function to multiple items, function must be itemfunc
my @res = aduxitem(, $item1, $item2, $item3);
=head1 DESCRIPTION
This distribution implements L<Unixish>, a data transformation framework
inspired by Unix toolbox philosophy.
=head1 FUNCTIONS
The functions are not exported by default. They can be exported individually or
altogether using export tag C<:all>.
=head2 aduxa($func, \@input) => ARRAYREF
=head2 aduxc($func, $callback, \@input)
=head2 aduxf($func, \@input) => FILEHANDLE
=head2 aduxl($func, \@input) => LIST (OR SCALAR)
The C<adux*> functions accept an arrayref as input. C<$func> is a string
containing dux function name (if no arguments to the dux function is to be
supplied), or C<< [$func, \%args] >> to supply arguments to the dux function.
Dux function name corresponds to module names C<Data::Unixish::NAME> without the
prefix.
The C<*duxc> functions will call the callback repeatedly with every output item.
The C<*duxf> functions returns filehandle immediately. A child process is
forked, and dux function is run in the child process. You read output as lines
from the returned filehandle. (Currently not yet supported on Windows due to no
support for open '-|').
The C<*duxl> functions returns result as list. It can be evaluated in scalar to
return only the first element of the list. However, the whole list will be
calculated first. Use C<*duxf> for streaming interface.
=head2 cduxa($func, $icallback) => ARRAYREF
=head2 cduxc($func, $icallback, $ocallback)
=head2 cduxf($func, $icallback) => FILEHANDLE
=head2 cduxl($func, $icallback) => LIST (OR SCALAR)
The C<cdux*> functions accepts a callback (C<$icallback>) to get input elements
from. Input callback function should return a list of one or more elements, or
an empty list to signal end of stream.
An example:
cduxa($func, sub {
state $a = [1,2,3,4];
if (@$a) {
return shift(@$a);
} else {
return ();
}
});
=head2 fduxa($func, $file_or_handle, @args) => ARRAYREF
=head2 fduxc($func, $callback, $file_or_handle, @args)
=head2 fduxf($func, $file_or_handle, @args) => FILEHANDLE
=head2 fduxl($func, $file_or_handle, @args) => LIST
The C<fdux*> functions accepts filename or filehandle. C<@args> is optional and
will be passed to L<Tie::File>. Currently not yet supported on Windows.
=head2 lduxa($func, @input) => ARRAYREF
=head2 lduxc($func, $callback, @input)
=head2 lduxf($func, @input) => FILEHANDLE
=head2 lduxl($func, @input) => LIST
The C<ldux*> functions accepts list as input.
=head2 siduxs($func, $item) => $res
=head2 aiduxa($func, \@items) => ARRAYREF
=head2 aiduxl($func, \@items) => LIST
=head2 liduxa($func, @items) => ARRAYREF
=head2 liduxl($func, @items) => LIST
The C<*idux*> functions apply dux function on single item(s). Only dux functions
tagged with C<itemfunc> can be used. These functions can operate on a single
item and return a single result. Examples of itemfunc functions are C<uc>,
C<lc>, C<sprintf>. Examples of non-itemfunc functions are C<head>, C<tail>,
C<wc>.
( run in 1.550 second using v1.01-cache-2.11-cpan-13bb782fe5a )