docdurdee has asked for the wisdom of the Perl Monks concerning the following question:
Dearest Monks, I need some guidance. I've decided to glue up a few different programs (let's call them A, B, C) that convert input to output. I would like to do fanciful things with my objects that can utilize these different programs. My goals: 1. ability to construct object from different inputs and output corresponding to a given program (A.inp A.out B.inp B.out). 2. ability to write inputs (A.inp B.inp C.inp) from object 3. ability to run the programs to generate output in files or memory. for 3. create scratch directory and run in that directory if requested. my current path: create objects for each program that can process/generate inputs and process outputs. I'm using roles to share code between them. I'm trying to get everything organized and maintainable. Do you have any advice to develop this with some sanity? it's driving me crazy! below is the role that binds up the File Pipe and Slurp roles.
package FhPhRole; use Moose::Role; use Moose::Util::TypeConstraints; #goals to be able to use molecular object to do fun stuff: # 0. read/write from/to input_filename # a. read # b. write # # 1. read/create output # a. read output file # b. pipe to output # c. pipe to memory # d. tee it to output and memory with qw(FileRole PipeRole SlurpRole); requires '_input'; has 'exe' => ( is => 'rw', isa => 'Str', default => '/usr/local/bin/bar', ); subtype 'MyMode', as 'Str', where { $_[0] =~ /0a|0b|1a|1b|1c|1d/ }, message { "0a. read from input \n 0b. write input \n" . "1a. read from output \n" . "1b. write to output \n 1c. pipe to memory\n" . "1d. tee it!\n""}; has 'mode' => ( is => 'ro', required => 1, isa => 'MyMode', ); has 'scratch' => ( is => 'ro', isa=> 'Str', predicate => 'has_scratch', ); # if we have scratch make the scratch and run in there # sub BUILD { my $self = shift; if ($self->has_scratch and $self->mode =~ /0b|1b|1c|1d/){ mkdir $self->scratch unless (-e $self->scratch); $self->exe("cd ". $self->scratch ." ; " . $self->exe); } $self->rw_input_filename('w'); $self->rw_input_filename('r') if $self->mode eq '0a'; $self->rw_output_filename('r') if $self->mode eq '1a'; $self->rw_output_filename('w') if $self->mode eq '1b'; $self->command($self->exe . " ". $self->input_filename); $self->command($self->command . " > " . $self->output_filename) if $ +self->mode eq '1b'; $self->command($self->command . " |tee " . $self->output_filename) i +f $self->mode eq '1d'; print $self->command . "\n"; $self->clear_command() if ($self->mode =~ /0/); }
|
|---|