perl IPC :: run传递参数似乎失败了

时间:2013-10-12 01:16:43

标签: perl email grep

我一直在谷歌上搜索一段时间了,看了一堆stackoverflow的东西。问题是,我仍然没有答案,并尝试了迄今为止我找到的所有方法。我需要grep才能在比赛结束后获得这4行,并且我会遇到意外的行为和错误。

这是我的perl脚本:

#! /usr/bin/perl
use strict;
use warnings;
use List::Util 'first';  use Data::Dumper;
use IPC::Run 'run';
my $look;
my $file = 'reject.txt';
my @result;
my @args;

#my $from = first { /From:/ } @arr;
#my $to = first { /To: / } @arr;
#my $subject = first { /Subject:/ } @arr;

open my $fh, '>', $file or die $!;
while (<>) {
#print $fh $_;
$look = $_;
print $fh $look;
}
close $fh;
print 'doing grep now:'. "\n";

push(@args,"-iA 4 \"forwarded message\"");
push(@args,$file);

print Dumper(\@args) . "\n";

run [ "grep", @args ], ">" , \my $output;

print $output . "\n";

print 'done with grep' . "\n";

grep的输出应该是

---------- Forwarded message ----------
From: <email@address.com>
Date: Fri, Oct 4, 2013 at 9:35 AM
Subject: New Voicemail Message from (407) 221-0727
To: recipient@here.us

但是针对转发的电子邮件运行perl脚本的结果是:

macpro:perl test macpro$ ./reject.pl email
doing grep now:
$VAR1 = [
          '-iA 4 "forwarded message"',
          'reject.txt'
        ];

grep: Invalid argument

done with grep

任何指针都将不胜感激。正如我所提到的,我对perl一无所知,刚刚开始。

修复:请参阅下面的@ hobbs的回复。另外,对于noobs更简单的语义用法,这个其他选项也适用,你不必乱用数组:

use Capture::Tiny 'tee';
my $output = tee { system( "some command" ) };

1 个答案:

答案 0 :(得分:1)

push(@args,"-iA 4 \"forwarded message\"");错了。你想写:

push @args, "-iA", "4", "forwarded message";

这些表示为grep的三个独立参数。