使用系统命令通过R运行perl程序的问题

时间:2012-07-30 13:16:49

标签: perl r system

当我使用系统运行我的perl程序时,我收到以下错误。

> system("perl txt2xlsx.pl", intern=TRUE, wait=TRUE)
character(0)
attr(,"status")
[1] 2
Warning message:
running command 'perl txt2xlsx.pl' had status 2 

然而,当我从终端/控制台运行脚本时,它完全正常。 出于测试目的,我将另一个perl脚本打印到屏幕上的“输出”到同一目录并运行它:

> system("perl test.pl",     intern=TRUE, wait=TRUE)

[1]“输出”

工作正常。

当您运行我的txt2xlsx.pl脚本而没有任何参数时,它会打印出来:

Usage: txt2xlsx.pl <directory> <output file>

有谁可以想象为什么R会返回此错误?它工作正常,直到我更新到最新的perl版本:

This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-multi-2level

干杯

以下是我的一些perl脚本:

#!/usr/bin/perl

use Excel::Writer::XLSX;

# Check command line args
if($#ARGV != 1) {
     die("Usage: $0 <directory> <output file>\n\n");
}

my $dir = $ARGV[0];
my $output = $ARGV[1];

print "Output    : $output\n";
print "Directory : $dir\n";

# Create excel workbook
my $wb = Excel::Writer::XLSX->new($output);

# Process files
foreach (glob("$dir/*.txt")) {

....

}
#EOF

我做了一些测试并减少了perl脚本,只打印了两个参数:

#!/usr/bin/perl

#use Excel::Writer::XLSX;

# Check command line args
if($#ARGV != 1) {
     die("Usage: $0 <directory> <output file>\n\n");
}

my $dir = $ARGV[0];
my $output = $ARGV[1];

print "Output    : $output\n";
print "Directory : $dir\n";

当我用两个预期的参数调用程序时系统工作:

> system("perl test.pl asd dasd", intern=TRUE, wait=TRUE)
[1] "Output    : dasd" "Directory : asd" 

没有它会产生相同的错误消息:

> system("perl test.pl", intern=TRUE, wait=TRUE)
character(0)
attr(,"status")
[1] 255
Warning message:
running command 'perl test.pl asd dasd' had status 255

我注意到,当我评论#use Excel::Writer::XLSX;时,使用system的系统调用失败了:

> system("perl test.pl asd dasd", intern=TRUE, wait=TRUE)
character(0)
attr(,"status")
[1] 2
Warning message:
running command 'perl test.pl asd dasd' had status 2

所以看起来它不喜欢XLSX perl模块。但是,正如我已经提到的,在终端中运行脚本非常正常。并且在更新之前它也适用于R系统调用...

--------------------

我正在使用StatET和Eclipse,我试图在正常的R版本中运行它。那里的错误信息更清晰。当我通过R调用perl调用时,perl无法找到代码中使用的XLSX包:

> system("perl test.pl asd asd", intern=TRUE, wait=TRUE)
character(0)
attr(,"status")
[1] 2
Warning message:
running command 'perl test.pl asd asd' had status 2 
Can't locate Excel/Writer/XLSX.pm in @INC (@INC contains: /Library/Perl/5.12/darwin- 
thread-multi-2level /Library/Perl/5.12 /Network/Library/Perl/5.12/darwin-thread-multi-
2level /Network/Library/Perl/5.12 /Library/Perl/Updates/5.12.4 
/System/Library/Perl/5.12/darwin-thread-multi-2level /System/Library/Perl/5.12 
/System/Library/Perl/Extras/5.12/darwin-thread-multi-2level 
/System/Library/Perl/Extras/5.12 .) at test.pl line 3.
BEGIN failed--compilation aborted at test.pl line 3.

有人知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

我怀疑你的perl脚本实际上确实返回了一个2的退出代码,这是一个非标准值,意在发出信号。

正是因为通过system()进行通信非常繁琐,你可以考虑在R本身做这件事。如果源文件是ascii,则易于阅读。包XLConnectxlsx允许您在R支持的任何平台上编写xlsx(并支持Java,因为xlsx读/写代码来自Java jar文件库)。

答案 1 :(得分:0)

您的perl文件需要命令行上的参数。它应该像这样执行:

perl txt2xlsx.pl dir_to_outputfile outputfile.xls

尝试将您的R文件更改为

system("perl txt2xlsx.pl dir_to_outputfile output.xls", intern=TRUE, wait=TRUE)

答案 2 :(得分:0)

当您在终端中运行程序时,您将有一些环境变量集(如PERL5LIB)在运行R时丢失,并且是指向perl以查找Excel :: Writer :: XLSX。

运行env | grep PERL以找出

然后您可以将其导出,或将所需的值传递给-I参数。

相关问题