简体   繁体   English

system(“linux_command”)与Perl库函数

[英]system(“linux_command”) vs. Perl library functions

In Perl, one can either use a Perl builtin or use system() which calls a shell command to achieve the some goal. 在Perl中,可以使用Perl内置或使用system()调用shell命令来实现某个目标。 The problem is, for me as a Perl beginner, it's sometimes difficult to find the Perl equivalent to a Linux command. 问题是,对于我作为Perl初学者,有时很难找到与Linux命令相当的Perl。
Take this one for example: 以此为例:

cat file1 file2 | sort -u > file3

I really want to use only the Perl functions to make my more Perlish, but I can't easily find out how to avoid using system() in this case. 我真的只想使用Perl函数来制作更多的Perlish,但我不能轻易找到如何在这种情况下避免使用system()。

So I wonder, is there any advantage of using Perl Library functions than using the system() call? 所以我想知道,使用Perl库函数比使用system()调用有什么好处吗? Which is the better way? 哪种方式更好?

Often the advantage in using library functions is that you can give meaningful error messages when something goes wrong. 通常,使用库函数的优点是,当出现问题时,您可以提供有意义的错误消息。

For short-lived scripts or when development resources are at a premium, using system instead can make sense. 对于短期脚本或者当开发资源非常宝贵时,使用系统可能是有意义的。

local $^I;
local @ARGV = qw(file1 file2);
open my $fh, ">", "file3" or die $!;
my %s;

# print {$fh} (LIST) # or using foreach:
print $fh $_ for
  sort
  grep !$s{$_}++,
  <>;

Main advantage is portability and not having system dependencies. 主要优点是可移植性,没有系统依赖性。

More explicit version, 更明确的版本,

use List::MoreUtils qw(uniq);
local $^I;
local @ARGV = qw(file1 file2);
open my $fh, ">", "file3" or die $!;

for my $line (sort uniq readline()) {
  print $fh $line;
}

Use perl library. 使用perl库。 Saves the processor effort in spawning another process. 节省处理器工作以产生另一个进程。 Also able to get better indication when things go wrong. 当出现问题时,也能够得到更好的指示。

If you want to execute a system command and making it perlish i will recommend using open(). 如果你想执行一个系统命令并使它perlish,我会建议使用open()。

open(fh,"cat test.txt text_file.txt | sort -u >new_file.txt | ");

This makes your program simple and easy to understand.The support provided in perl for system commands is one of the beauties of it.So its better to use it the way it is rather then going the whole trip round just to make your code 'Perlish'. 这使得你的程序简单易懂。在perl中为系统命令提供的支持是它的优点之一。所以它更好地以它的方式使用它而不是整个行程只是为了使你的代码'Perlish ”。

You can of course use Perl script version as pointed out by mpapec . 您当然可以使用mpapec指出的Perl脚本版本。 But usage of system or open version pointed by Asif Idris have some advantages. Asif Idris指出的systemopen版本的使用有一些优点。 For example if you need sort big amount of data, using system command sort will bring you much further wit a lot less pain. 例如,如果您需要对大量数据进行sort ,使用系统命令sort将为您带来更多的痛苦。 Sorting few GB using perl is PITA but it is not big deal for system command sort and you will even use all your cores and much less memory. 使用perl对几GB进行排序是PITA,但它对系统命令sort并不重要,你甚至可以使用所有内核和更少的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM