我正在尝试制作一个使用 open3 与 mplayer 通信的脚本,但 mplayer 进程显示为已失效,我无法将标准输入发送到 mplayer。
这是代码:
#!/usr/bin/env perl
{
package mplayer::test;
use IPC::Open3;
sub new {
my $class = shift;
my $self = bless { @_ }, $class;
$self->start_mplayer();
$self;
}
sub start_mplayer{
my $self = shift;
local *DEVNULL;
open DEVNULL, ">/dev/null" or die "/dev/null: $!";
open OUTPUT, ">out.log" or die "out.log: $!";
$self->{r} = local *MPLAYER_READ;
$self->{w} = local *MPLAYER_WRITE;
$self->{pid} = open3($self->{w},$self->{r},">&DEVNULL",'mplayer -slave -idle -v');
die "Error opening mplayer!\n" unless $self->{pid};
}
sub do{
my ($self, $command) = @_;
print {$self->{w}} $command, "\n";
}
}
mplayer::test->new;
mplayer::test->do(qq~loadfile test.mp3~);
sleep(5);
我一定遗漏了一些明显的东西,我正在从其他模块的示例中学习 open3。