How can I run a perl script from R with specific modules?

狂风中的少年 提交于 2021-01-24 07:14:26

问题


I can run a perl script "myperlscript.pl" from Terminal without any problems. However, if I try to run the same perl script from within RStudio, it gives me the following error:

command <- "myperlscript.pl outputfile.txt"

system2('perl', command)

Can't locate Sort/Fields.pm in @INC (you may need to install the Sort::Fields module) 
(@INC contains: 
/Library/Perl/5.28/darwin-thread-multi-2level 
/Library/Perl/5.28 
/Network/Library/Perl/5.28/darwin-thread-multi-2level 
/Network/Library/Perl/5.28 
/Library/Perl/Updates/5.28.2 
/System/Library/Perl/5.28/darwin-thread-multi-2level 
/System/Library/Perl/5.28 
/System/Library/Perl/Extras/5.28/darwin-thread-multi-2level 
/System/Library/Perl/Extras/5.28) at myperlscript.pl line 4.
BEGIN failed--compilation aborted at myperlscript.pl line 4.

I have the Sort::Fields module installed in /Users/admin/perl5/perlbrew/perls/5.26.2/lib/site_perl/5.26.2 and it works fine using Terminal but it seems that perl in RStudio does not index that directory for perl modules - I tried adding it to @INC but somehow I cannot get it to work... any thoughts or ideas? Help is greatly appreciated!

Best, Heiko


回答1:


The problem you have is that your R Code is using the system perl (5.28) and not your pelbrew perl (5.26.2).

You need to do the system2('perl', command) call to use the perlbrew perl instead of the system perl.

To do so from your shell where your perlbrew perl is activated type which perl this should give you the full path to your pelbrew perl.

Pass this full path as the first argument to the system2(--result of 'which perl'--, command).



来源:https://stackoverflow.com/questions/65553325/how-can-i-run-a-perl-script-from-r-with-specific-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!