NAME

IPC::PrettyPipe - manage human readable external command execution pipelines

SYNOPSIS

use IPC::PrettyPipe;

my $pipe = new IPC::PrettyPipe;

$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";

DESCRIPTION

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 entitites 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

This module (and its siblings IPC::PrettyPipe::Cmd, IPC::PrettyPipe::Arg, and IPC::PrettyPipe::Stream) present the object-oriented interface for manipulating the underlying infrastructure.

For a simpler, more intuitive means of constructing pipelines, see IPC::PrettyPipe::DSL.

Pipeline Rendering (Pretty Printing)

IPC::PrettyPipe doesn't render a pipeline directly; instead it passes that job on to another object (which must consume the IPC::PrettyPipe::Render role).

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.

Pipeline Execution

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::Execute 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.

METHODS

new
# 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 );

Create a new IPC::PrettyPipe object. The available attributes are:

cmds

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.

argpfx, argsep

Optional. The default prefix and separation attributes for arguments to commands. See IPC::PrettyPipe::Arg for more details. These override any specified via the "argfmt" object.

argfmt

Optional. An IPC::PrettyPipe::Arg::Format object specifying the default prefix and separation attributes for arguments to commands. May be overridden by "argpfx" and "argsep".

executor

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.

renderer

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.

add
$cmd_obj = $pipe->add( $cmd );
$cmd_obj = $pipe->add( cmd => $cmd, %options );

Create an IPC::PrettyPipe::Cmd object, add it to the IPC::PrettyPipe object, and return a handle to it. If passed a single parameter, it is assumed to be a cmd parameter.

This is a thin wrapper around the IPC::PrettyPipe::Cmd constructor, taking the same parameters. The only difference is that if the value of the cmd parameter is an IPC::PrettyPipe::Cmd object it is inserted into the pipeline.

ffadd
$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::Util), 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 @cmds will 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 cmd and args parameters.

  • An IPC::PrettyPipe::Arg::Format object, specifying the argument prefix and separator attributes for successive commands.

argpfx
argsep

These methods retrieve (when called with no arguments) or modify (when called with an argument) the similarly named object attributes. Changing these affects the defaults for future command arguments; it does not affect existing arguments.

See IPC::PrettyPipe::Arg for more information.

render
my $string = $pipe->render

Return a prettified string of the pipeline.

run
$pipe->run

Execute the pipeline.

stream
$pipe->stream( $stream_spec );
$pipe->stream( $stream_spec, $file );

Add an I/O stream to the pipeline. See "Stream Specification" in IPC::PrettyPipe::Stream::Util for more information.

streams
$streams = $pipe->streams

Return the streams associated with the command as an arrayref of IPC::PrettyPipe::Stream objects.

valmatch
$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 ).

valsubst
$pipe->valsubst( $pattern, $value, %attr );

Replace arguments to options whose arguments match the Perl regular expression $pattern with $value. The following attributes are avaliable:

firstvalue

The first matched argument will be replaced with this value

lastvalue

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 input and 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"

results in

	cmd1 \
  	  input input_file \
  	  output stdout \
  |	cmd2 \
  	  input stdin \
  	  output stdout \
  |	cmd3 \
  	  input stdin \
  	  output output_file

COPYRIGHT & LICENSE

Copyright 2014 Smithsonian Astrophysical Observatory

This software is released under the GNU General Public License. You may find a copy at

http://www.fsf.org/copyleft/gpl.html

AUTHOR

Diab Jerius <djerius@cfa.harvard.edu>