[英]perl IPC::run passing arguments seems to fail
我已經搜索了一段時間,並查看了一堆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" ) };
push(@args,"-iA 4 \\"forwarded message\\"");
是錯的。 您要寫:
push @args, "-iA", "4", "forwarded message";
這樣它們就顯示為grep的三個獨立參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.