【发布时间】:2019-11-24 10:41:28
【问题描述】:
我的bosswrap.pl 将生成任意数组,其中包含元素中的空格。它通过系统调用将数组重复发送到wrapped.pl,它会根据数组创建STDOUT。
随后,bosswrap.pl 必须将wrapped.pl 的标准输出附加到bosswrap.pl 控制的文件中。那就是我卡住的地方。先wrapped.pl:
#!/usr/bin/perl
use strict; use warnings;
print "inside $0\n";
my $countarrayelements=0;
for my $item ( @ARGV ) {
$countarrayelements++;
print "$countarrayelements: |$item|\n";
}
然后bosswrap.pl:
#!/usr/bin/perl
use strict; use warnings;
my $fileresult = "trash.txt";
unlink $fileresult; # rm any existing file
my $handlefileresult;
my @array = ( 5, "don't you . . . ", "\x09\x21", "\x3f \x21" );
my $count=0;
print "inside $0\n";
for my $element ( @array ) {
$count++;
print "$count\t|$element|\n";
}
#https://stackoverflow.com/questions/50553031/calling-a-shell-command-with-multiple-arguments
#list form: command line arguments pass verbatim, avoid shell interpolation
system("wrapped.pl", @array) == 0 or die "system failed: \$? = $?";
#This block sends the return code, not the STDOUT, to handlefileresult
open( $handlefileresult, ">> ", $fileresult) || die "$0: can't open $fileresult for appending$!";
print $handlefileresult "not a system call\n";
print {$handlefileresult} system("wrapped.pl", @array) == 0 or die "system failed: \$? = $?";
close ( $handlefileresult ) || die;
#In the following block, select unfortunately will not redirect output of system call:
open( $handlefileresult, ">> ", $fileresult) || die "$0: can't open $fileresult for appending$!";
select $handlefileresult;
print "selected; BEFORE system call\n";
system("wrapped.pl", @array) == 0 or die "system failed: \$? = $?";
print "selected; AFTER system call\n";
close ( $handlefileresult ) || die;
再次,我想从bosswrap.pl 内部反复调用wrapped.pl,并将这些系统调用的输出放入$fileresult。
一个回复@ikegami 建议use IPC::Run qw( run ); 但这会引发错误。我在 macos 上运行 perl。必须做些什么才能让它发挥作用?
> perl --version
This is perl 5, version 18, subversion 4 (v5.18.4) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)
Copyright 1987-2013, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
> bosswrap.pl
Can't locate IPC/Run.pm in @INC (you may need to install the IPC::Run module) (@INC contains: /sw/lib/perl5/darwin-thread-multi-2level /sw/lib/perl5 /sw/lib/perl5/darwin /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.4 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at /Users/BNW/u/kh/bin/bosswrap.pl line 3.
BEGIN failed--compilation aborted at /Users/BNW/u/kh/bin/bosswrap.pl line 3.
>
基于What's the easiest way to install a missing Perl module?我做了
> cpan IPC::Run
<snip>
Appending installation info to /Users/BNW/perl5/lib/perl5/darwin-thread-multi-2level/perllocal.pod
TODDR/IPC-Run-20180523.0.tar.gz
/usr/bin/make install -- OK
然后重启了这台 MacBook Pro 10.14.6。但我显然得到了同样的错误:
> bosswrap.pl
Can't locate IPC/Run.pm in @INC (you may need to install the IPC::Run module) (@INC contains: /sw/lib/perl5/darwin-thread-multi-2level /sw/lib/perl5 /sw/lib/perl5/darwin /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.4 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at /Users/BNW/u/kh/bin/bosswrap.pl line 3.
BEGIN failed--compilation aborted at /Users/BNW/u/kh/bin/bosswrap.pl line 3.
>
我该如何解决这个问题?
【问题讨论】:
标签: arrays perl stdout whitespace filehandle