简体   繁体   English

如何通过管道打开程序并将输出存储到Perl中的变量中?

[英]How can I open a program by pipe and store the output into a variable in Perl?

I have two cols of data. 我有两个数据列。 How can I get the min and max values of each column? 如何获取每列的最小值和最大值?

I'm trying something like: 我正在尝试类似:

open $in, "|gmt minmax -C";
print $in ...;
...
close($in);

However, the output will be printed to the screen. 但是,输出将被打印到屏幕上。 How can I store the results in @minmax(with backticks?) 如何将结果存储在@minmax中(带反引号?)

There are many ways to pull this off. 有很多方法可以实现这一目标。

  1. Use a module. 使用模块。

There are many modules that can handle piping for you in a way that is much simpler than doing it yourself. 有许多模块可以比您自己做的简单得多。 IPC::Run , IPC::Run3 , or, my current favourite, AnyEvent::Util ::run_cmd. IPC :: RunIPC :: Run3或我当前最喜欢的AnyEvent :: Util :: run_cmd。 This is just the tiniest of sampling - look around in CPAN, I'm sure you'll find dozens more. 这只是最小的抽样-在CPAN中环顾四周,我相信您还会发现更多。 Some of these can direct output into scalars, some also allow you to handle the input as it comes in, which can be advantageous when you receive a lot of input possibly over a long period of time, and can filter it on the way through to only extract the portions of data you're interested in. 其中一些可以将输出定向为标量,另一些还可以让您处理输入的输入,这在您可能会长时间接收大量输入并且可以在过滤到输入的途中进行过滤时很有用。仅提取您感兴趣的数据部分。

  1. Use pipe yourself. 自己使用管道

This is what some of the modules do. 这就是某些模块的功能。 Create a pair of bidirectional pipes, fork, close off the ends of the pipes you don't need in their respective processes, hook up the right pipes to your stdout and stdin, and exec the child process. 创建一对双向管道,进行分叉,关闭在各自过程中不需要的管道末端,将正确的管道连接到stdout和stdin,然后执行子进程。 It's tricky to get it all right, thus the reason for the modules. 使其完全正确是棘手的,因此是选择模块的原因。

  1. Find the min and max yourself. 找到自己的最小值和最大值。

You have the data in your process space already (or you have the file), just read it in to a pair of arrays, and call List::Util::min and List::Util::max on the arrays. 您已在进程空间中拥有数据(或拥有文件),只需将其读入一对数组,然后在数组上调用List :: Util :: minList :: Util :: max (I'm assuming they're simple numbers - if they're complex in some form, you may have to implement your own min/max.) (我假设它们是简单的数字-如果它们以某种形式复杂,则可能必须实现自己的最小/最大。)

  1. Treat data as data. 将数据视为数据。

If your data is structured, one of my favourites is to simply feed it in to DBD::CSV and then I can execute SQL on it - you can even feed your data directly from in memory if that's what you have. 如果您的数据是结构化的,我的最爱之一就是简单地将其输入到DBD :: CSV中 ,然后我就可以对它执行SQL了-如果您拥有的话,您甚至可以直接从内存中输入数据。 It's a bit slower at runtime, but can do some pretty useful things. 它在运行时有点慢,但是可以做一些非常有用的事情。 This also is a good segue into putting the data into a real (or real-ish) database, whether that's SQLite as an intermediate (this is never my ultimate plan, but it makes a great dev tool or proof-of-concept that may turn out to be viable for years), or mySQL, postgres, DB2, Oracle, whatever, especially if your data set continues to grow. 将数据放入真实(或真实)数据库中也是一个很好的选择,无论是将SQLite作为中介(这都不是我的最终计划,但它可以成为一个出色的开发工具或概念证明,最终证明是可行的),或者使用mySQL,postgres,DB2,Oracle等,尤其是在您的数据集持续增长的情况下。

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

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