Perl:子程序输出;输入到 foreach 语句
我有一个子路由,输出 FQDN 列表,并用换行符分隔:
x1.server.com
s2.sys.com
5a.fdsf.com
^^ 它采用这种格式,因此除了 {variable text}.{variable text}.{variable text}
我的问题是我会如何能够将此输出作为 foreach 语句的输入,以便我可以迭代每个 FQDN?
I have a subrouting that outputs a list of FQDN's, separated by new lines:
x1.server.com
s2.sys.com
5a.fdsf.com
^^ It's in this format, so there's no pattern other than {variable text}.{variable text}.{variable text}
My question is how would I be able to get THIS output as the input of a foreach statement so that I can iterate through each FQDN?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NB:你说子输出一个列表,但我假设你的意思是它输出一个字符串。否则这个问题就没有意义了。
只需在换行符上拆分输出即可。假设子例程名为
subname
:正如 Brian Roach 在注释中指出的那样,最佳解决方案是使子例程返回列表而不是字符串。但是,这对您来说可能不是一个可行的解决方案。不管怎样,如果您想尝试一下,只需在子例程中的适当位置添加
split
即可。例如:如果您想获得高级,您可以使用
wantarray
函数,它检测在哪个上下文中调用子例程:虽然这非常可爱,但除非您知道自己在做什么,否则它可能会导致不良行为。
NB: You say the sub outputs a list, but I assume what you mean is that it outputs a string. Otherwise, this question is moot.
Just split the output on newline. Assuming the subroutine is called
subname
:As Brian Roach notes in the comments, the optimal solution is to make the subroutine return a list instead of a string. However, that may not be a viable solution for you. Either way, if you wish to try it, simply add the
split
at the appropriate place in the subroutine instead. E.g.:If you want to get advanced, you may make use of the
wantarray
function, which detects in which context the subroutine is called:While this is very cute, it can lead to unwanted behaviour unless you know what you are doing.
我想知道您是否真的意味着您的子例程“输出”一个列表 - 即它将列表打印到 STDOUT。你有这样的东西吗?
如果是这种情况,那么您需要聪明一点,将 STDOUT 重新打开到变量上。
I wonder if you really mean that your subroutine "outputs" a list - i.e. that it prints the list to STDOUT. Do you have something like this?
If that's the case then you need to be a bit clever and re-open STDOUT onto a variable.