IPC::PrettyPipe - manage human readable external command execution pipelines
use IPC::PrettyPipe; my $pipe = IPC::PrettyPipe->new; $pipe->add( $command, %options ); $pipe->add( cmd => $command, %options ); $pipe->stream( $stream_op, $stream_file ); $cmd = $pipe->add( $command ); $cmd->add( $args ); print $pipe->render, "\n";
Connecting a series of programs via pipes is a time honored tradition. When it comes to displaying them for debug or informational purposes, simple dumps may suffice for simple pipelines, but when the number of programs and arguments grows large, it can become difficult to understand the overall structure of the pipeline.
IPC::PrettyPipe provides a mechanism to construct and output readable external command execution pipelines. It does this by treating commands, their options, and the options' values as separate entities so that it can produce nicely formatted output.
It is designed to be used in conjunction with other modules which actually execute pipelines, such as IPC::Run.
For a simpler, more intuitive means of constructing pipelines, see IPC::PrettyPipe::DSL.
Pipeline Rendering (Pretty Printing)
By default IPC::PrettyPipe provides a renderer which uses Template::Tiny to render a pipeline as if it were to be fed to a POSIX shell (which can be handy for debugging complex pipelines).
The same renderer may be fed a different template to use, or it may be replaced via the "renderer" attribute.
Just as with rendering, IPC::PrettyPipe doesn't execute a pipeline on its own. Instead it calls upon another object (which must consume the IPC::PrettyPipe::Executor role). By default it provides an executor which uses IPC::Run to run the pipeline. The executor may be replaced via the "executor" attribute.
Rewriting Commands' argument values
Sometimes it's not possible to fill in an argument's value until after a pipeline has been created. The "valsubst" method allows altering them after the fact.
$streams = $pipe->streams;
Optional. The value should be an arrayref of commands to load into the pipe. The contents of the array are passed to the "ffadd" method for processing.
Optional. The means by which the pipeline will be executed. It may be either a class name or an object reference, and must consume the IPC::PrettyPipe::Executor role. It defaults to IPC::PrettyPipe::Execute::IPC::Run.
Optional. The means by which the pipeline will be rendered. It may be either a class name or an object reference, and must consume the IPC::PretyyPipe::Renderer role. It defaults to IPC::PrettyPipe::Render::Template::Tiny.
# initialize the pipe with commands $pipe = IPC::PrettyPipe->new( cmds => [ $cmd1, $cmd2 ], %attrs ); # initialize the pipe with a single command $pipe = IPC::PrettyPipe->new( $cmd ); # create an empty pipeline, setting defaults $pipe = IPC::PrettyPipe->new( %attrs );
$cmds = $pipe->cmds;
Execute the pipeline.
my $string = $pipe->render
Return a prettified string of the pipeline.
$cmd_obj = $pipe->add( cmd => $cmd, %options );
add may also be passed a single parameter, which may be one of:
$cmd_obj = $pipe->add( $cmd_name );
The name of a command
$cmd_obj = $pipe->add( $cmd_obj );
$pipe_obj = $pipe->add( $pipe_obj );
IPC::PrettyPipeobject. This is intended to allow pipes to be nested. However, nested pipes with non-default streams may not be supported by the pipe executor.
$pipe->ffadd( @cmds );
A more relaxed means of adding commands.
@cmds may contain any
of the following items:
- an IPC::PrettyPipe::Cmd object
- A command name (i.e. a string), for a command without arguments.
- A string which matches a stream specification
("Stream Specification" in IPC::PrettyPipe::Stream::Utils), which will cause
a new I/O stream to be attached to the pipeline. If the specification
requires an additional parameter, the next value in
@cmdswill be used for that parameter.
- An arrayref. The first element is the command name; the rest are its
arguments; these are passed to
IPC::PrettyPipe::Cmd::new as the
- An IPC::PrettyPipe::Arg::Format object, specifying the argument prefix and separator attributes for successive commands.
$pipe->stream( $stream_spec ); $pipe->stream( $stream_spec, $file );
Add an I/O stream to the pipeline. See "Stream Specification" in IPC::PrettyPipe::Stream::Utils for more information.
$n = $pipe->valmatch( $pattern );
Returns the number of commands with a value matching the passed regular expression. (This is not equal to the number of total values which matched. To determine this, iterate over each command, calling it's valmatch method ).
$pipe->valsubst( $pattern, $value, %attr );
Replace arguments to options whose arguments match the Perl regular expression $pattern with $value. The following attributes are available:
The first matched argument will be replaced with this value
The last matched argument will be replaced with this value.
Note that matching is done on a per-command basis, not per-argument basis, so that if a command has multiple matching values, they will all use the same replacement string. To perform more specific changes, use each command's valsubst method directly.
Here's an example where the commands use parameters
output to indicate where they should write. The strings "stdout"
and "stdin" are special and indicate the standard streams. Using
valsubst allows an easy update of the pipeline after construction
to specify the correct streams.
$p = new IPC::PrettyPipe; $p->add( cmd => 'cmd1', args => [ [ input => 'INPUT', output => 'OUTPUT' ] ] ); $p->add( cmd => 'cmd2', args => [ [ input => 'INPUT', output => 'OUTPUT' ] ] ); $p->add( cmd => 'cmd3', args => [ [ input => 'INPUT', output => 'OUTPUT' ] ] ); $p->valsubst( qr/OUTPUT/, 'stdout', lastvalue => 'output_file' ); $p->valsubst( qr/INPUT/, 'stdin', firstvalue => 'input_file' ); print $p->render, "\n"
cmd1 \ input input_file \ output stdout \ | cmd2 \ input stdin \ output stdout \ | cmd3 \ input stdin \ output output_file
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=IPC-PrettyPipe or by email to bug-IPC-PrettyPipe@rt.cpan.org.
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Diab Jerius email@example.com
COPYRIGHT AND LICENSE
This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
This is free software, licensed under:
The GNU General Public License, Version 3, June 2007