PipeC - manage command pipes
use lib '/proj/axaf/simul/lib/perl'; use PipeC;
my $pipe = new PipeC;
$pipe->argsep( ' ' );
$pipe->add( $command, $arg1, $arg2 );
$pipe->add( $command, $arg1, { option => value } );
my $cmd = $pipe->add( $command, $arg1,
[ option1 => value1, option2 => value2] );
$cmd->add( $arg1, $args, { option => value },
[option => value, option => value ] )
$cmd->argsep( '=' );
warn $pipe->dump, "\n";
$pipe->run;
PipeC provides a mechanism to maintain readable execution pipelines. Pipelines are created by adding commands to a PipeC object. Options to the commands are set using a readable format; PipeC takes care of quoting strings, sticking equal signs in, etc. The pipeline may be rendered into a nicely formatted string, as well as being executed (currently by the Bourne shell, /bin/sh).
PipeC actually manages a list of PipeC::Cmd objects. These objects also have methods; descriptions are available in PipeC::Cmd Methods
PipeC object with the optionally specified
attributes. The attributes are specified via a hash, which may have
the following keys:
= character. New commands inherit the
current value of the ArgSep string. The default value may also be
changed with the argsep method for PipeC objects. The separator
for a given command may be changed with the argsep method for the
command.
PipeC object. It returns a handle
to the command (which is itself a PipeC::Cmd object). The optional
hash may be used to set attributes for the command.
The \%attr hash
may contain one of the following key/value pairs:
PipeC::add method.
Arguments to the command may be specified in one of the following ways:
$pipe->add( 'ls', '-l' );
This option is useful for command options which do not take an argument.
$pipe->add( 'tar', { -f => 'foo.tar' } );
This method obviously is only useful for options which take an argument. Multiple options may be specified in the hash. Because it is a hash, the ordering of the options will not be kept as specified.
$pipe->add( 'tar', [ -f => 'foo.tar' ] );
This method obviously is only useful for options which take an argument. Multiple options may be specified in the array. The ordering of options is retained.
The different methods of option specification may be mixed, e.g.,
$pipe->add( 'tar',
'-v',
[ -f => 'foo.tar' ],
{ -b => 100 }
);
undef or unspecified,
it cancels any value previously set.
undef or unspecified, it
cancels any value previously set.
undef or unspecified, it
cancels any value previously set.
PipeC::add method.
For example,
my $pipe = new PipeC;
$pipe->add( 'cmd1', [ input => 'INPUT', output => 'OUTPUT' ] );
$pipe->add( 'cmd2', [ input => 'INPUT', output => 'OUTPUT' ] );
$pipe->add( 'cmd3', [ input => 'INPUT', output => 'OUTPUT' ] );
$pipe->valrep( 'OUTPUT', 'stdout', 'output_file' );
$pipe->valrep( 'INPUT', 'stdin', undef, 'input_file' );
print $pipe->dump, "\n"
results in
cmd1 \
input=input_file \
output=stdout \
| cmd2 \
input=stdin \
output=stdout \
| cmd3 \
input=stdin \
output=output_file
PipeC::Cmd objects have methods of their own.
$cmd = $pipe->add( 'ls' );
$cmd->add( '-l' ) if $want_long_listing;
PipeC::add method.
For example,
$cmd = $pipe->add( 'cmd1' );
$cmd->add( [ opt1 => 'FOO' ] );
$cmd->add( [ opt2 => 'FOO' ] );
$cmd->valrep( 'FOO', 'FOO1', 'FOO2' );
print $cmd->dump, "\n"
results in
cmd1 \
opt1=FOO1 \
opt2=FOO2
If $firstvalue is specified, the first matched argument will be replaced with $lastvalue:
$cmd = $pipe->add( 'cmd1' );
$cmd->add( [ opt1 => 'FOO' ] );
$cmd->add( [ opt2 => 'FOO' ] );
$cmd->valrep( 'FOO', 'FOO1', undef, 'FOO2' );
print $cmd->dump, "\n"
results in
cmd1 \
opt1=FOO2 \
opt2=FOO1
Sometimes it's not possible to determine beforehand which command in a
pipeline will be the final one in the pipe; thus, it's not possible to
specify which command actually writes the output file until the very
end. In the following example the programs recognize the token
stdout to refer to the standard output stream; this is specific to
their implementation.
my $pipe = new PipeC;
$pipe->add( 'genphot',
{ output => 'OUTPUT',
photdens => 0.001 }
);
$pipe->add( 'bp2rdb',
{ input => 'stdin',
output => 'OUTPUT' }
)
if $convert_to_rdb;
$pipe->valrep( 'OUTPUT', 'stdout', 'rays.out' );
print $pipe->dump, "\n"
This results in:
genphot \
output=stdout \
photdens=0.001 \
| bp2rdb \
input=stdin \
output=rays.out
If programs can write to stdout directly, one can use PipeC::stdout
(and PipeC::stderr, if need be):
my $pipe = new PipeC;
$pipe->add( 'ls' );
$pipe->add( 'wc' );
$pipe->stdout( 'line_count' );
print $pipe->dump, "\n";
This results in:
ls \
| wc \
> line_count
To redirect stderr, add the following line,
$pipe->stderr( 'error' );
which results in:
( ls \
| wc \
> line_count \
) \
2> error
The entire pipe is run as a subshell, to ensure that all of the commands' standard error streams go to the same place.
This software is released under the GNU General Public License. You may find a copy at
http://www.fsf.org/copyleft/gpl.html
Diab Jerius (djerius@cfa.harvard.edu)