Perl:子程序输出;输入到 foreach 语句

发布于 2025-01-04 02:47:03 字数 226 浏览 1 评论 0原文

我有一个子路由,输出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落在眉间の轻吻 2025-01-11 02:47:03

NB:你说子输出一个列表,但我假设你的意思是它输出一个字符串。否则这个问题就没有意义了。

只需在换行符上拆分输出即可。假设子例程名为 subname

for my $fqdn (split /\n/, subname())

正如 Brian Roach 在注释中指出的那样,最佳解决方案是使子例程返回列表而不是字符串。但是,这对您来说可能不是一个可行的解决方案。不管怎样,如果您想尝试一下,只需在子例程中的适当位置添加 split 即可。例如:

sub foo {
    ...
    #return $string;
    return split /\n/, string;
}

如果您想获得高级,您可以使用 wantarray 函数,它检测在哪个上下文中调用子例程:

sub foo {
    ...
    return $string unless wantarray;
    return split /\n/, string;
}

虽然这非常可爱,但除非您知道自己在做什么,否则它可能会导致不良行为。

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:

for my $fqdn (split /\n/, 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.:

sub foo {
    ...
    #return $string;
    return split /\n/, string;
}

If you want to get advanced, you may make use of the wantarray function, which detects in which context the subroutine is called:

sub foo {
    ...
    return $string unless wantarray;
    return split /\n/, string;
}

While this is very cute, it can lead to unwanted behaviour unless you know what you are doing.

依 靠 2025-01-11 02:47:03
my $data = mySubRoutine()
# Data now contains one FQDN per line

foreach (my $line = split(/\n/,$data))
{
     doStuffWith($line);
}
my $data = mySubRoutine()
# Data now contains one FQDN per line

foreach (my $line = split(/\n/,$data))
{
     doStuffWith($line);
}
那一片橙海, 2025-01-11 02:47:03

我想知道您是否真的意味着您的子例程“输出”一个列表 - 即它将列表打印到 STDOUT。你有这样的东西吗?

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

sub print_list_of_fqdns {
  say "x1.server.com\ns2.sys.com\n5a.fdsf.com";
}

print_list_of_fqdns();

如果是这种情况,那么您需要聪明一点,将 STDOUT 重新打开到变量上。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

sub print_list_of_fqdns {
  say "x1.server.com\ns2.sys.com\n5a.fdsf.com";
}

sub get_list_of_fqdns {
  # Declare a buffer
  my $string;

  # Open a filehandle that writes to the buffer
  open my $fh, '>', \$string or die $!;

  # Set your new filehandle to the default output filehandle
  # (taking a copy of the original one)
  my $old_fh = select $fh;

  # Call the function. This will now write the list to the
  # variable $string instead of STDOUT
  print_list_of_fqdns();

  # Split $string to get the individual FQDNs
  my @fqdns = split /\n/, $string;

  # Replace the old default output filehandle
  select $old_fh;

  # Return the list of FQDNs
  return @fqdns;
}

say join ' / ', get_list_of_fqdns();

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?

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

sub print_list_of_fqdns {
  say "x1.server.com\ns2.sys.com\n5a.fdsf.com";
}

print_list_of_fqdns();

If that's the case then you need to be a bit clever and re-open STDOUT onto a variable.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

sub print_list_of_fqdns {
  say "x1.server.com\ns2.sys.com\n5a.fdsf.com";
}

sub get_list_of_fqdns {
  # Declare a buffer
  my $string;

  # Open a filehandle that writes to the buffer
  open my $fh, '>', \$string or die $!;

  # Set your new filehandle to the default output filehandle
  # (taking a copy of the original one)
  my $old_fh = select $fh;

  # Call the function. This will now write the list to the
  # variable $string instead of STDOUT
  print_list_of_fqdns();

  # Split $string to get the individual FQDNs
  my @fqdns = split /\n/, $string;

  # Replace the old default output filehandle
  select $old_fh;

  # Return the list of FQDNs
  return @fqdns;
}

say join ' / ', get_list_of_fqdns();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文