How to properly open pipe(s) with moose?
I'm still new to moose, but it has me drooling! Below is a simplified version of what I would like to do: generate object dependent input files that may be used to run an external program. These external programs can be intensive computationally, and I will process the output back into the objects many times each for many objects (as a function of some external parameter tweaking). The module below works (I think), but there may be better/cleaner ways to do this. What is the proper way to do this? Since all these objects live in their own little world, it seems as though I should be able to run batches of these in parallel to efficiently chomp through the collection of objects. Any tips for this!??
{
package input_genrun;
use Moose;
use IO::Pipe;
use FileHandle;
has 'name', is => 'ro', isa =>'Str';
has 'exe' => (
is => 'ro',
isa => 'Str',
default => '/usr/local/bin/bar',
);
has 'inp_fh' => (
is => 'rw',
isa=> 'FileHandle',
default => sub {
my $handle = FileHandle->new;
return $handle;
}
);
has 'out_fh' => (
is => 'rw',
isa=> 'IO::Pipe',
default => sub {
my $handle = IO::Pipe->new;
return $handle;
}
);
sub inp_wrt
{
my ($self,$object) = @_;
my $filename = $object->name() . ".inp";
my $fh = FileHandle->new;
$fh->open(">" . $filename);
print $fh "foo \n";
print $fh "bar \n";
$fh->close;
}
sub run
{
my ($self, $object) = @_;
my $name = $object->name() . ".inp";
my $exe = $self->exe;
my $command = $self->exe . " $name";
$s开发者_运维问答elf->out_fh()->reader($command);
return($self->out_fh());
}
}
my $l = input_genrun->new(name=> 'foo_l',exe=> 'wc');
my $m = input_genrun->new(name=> 'foo_m',exe=> 'cat');
my $n = input_genrun->new(name=> 'foo_n',exe=> 'tac');
$l->inp_wrt($l);
$m->inp_wrt($m);
$n->inp_wrt($n);
my $pipe_l = $l->run($l);
my $pipe_m = $m->run($m);
my $pipe_n = $n->run($n);
while (<$pipe_l>){
print "from_l: $_";
}
while (<$pipe_m>){
print "from_m: $_";
}
while (<$pipe_n>){
print "from_n: $_";
}
I'd start by taking a look at MooseX::Workers
which abstracts around POE::Wheel::Run
to solve this same problem. Failing that I'd look at a number of the other asynchronous job handlers out there (Parallel::Fork
, Proc::Simple::Async
, Gearman
, etc.) before really going to tackle implementing the wheel again.
If nothing else they'll give you a good idea of what has been done before in this space.
精彩评论