为什么 Perl 的 glob 每次调用都返回 undef? [英] Why does Perl's glob return undef for every other call?

查看:25
本文介绍了为什么 Perl 的 glob 每次调用都返回 undef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不一定要寻找更好的方法来做到这一点,而是非常感谢对输出的解释.最近,一位资深程序员问我为什么他的代码有效,但只是一个例子.我发现它每隔一段时间都有效.这是我的例子:

#!/usr/bin/perl -w使用严格;我的@list_env_vars = ('$服务器','$服务器','$服务器','$服务器','$服务器','$服务器',);foreach (@list_env_vars){打印 "$_ = ".glob()."
";}

perl 5.004 的输出:

$SERVER = UNIX_SERVER$服务器 =$SERVER = UNIX_SERVER$服务器 =$SERVER = UNIX_SERVER$服务器 =

或 perl 5.10 的输出:

$SITE = $SITE在 glob_test.pl 第 14 行的连接 (.) 或字符串中使用未初始化的值.$网站=$站点 = $站点在 glob_test.pl 第 14 行的连接 (.) 或字符串中使用未初始化的值.$网站=$站点 = $站点在 glob_test.pl 第 14 行的连接 (.) 或字符串中使用未初始化的值.$网站=

我个人从未以这种方式使用过 glob(),所以我没有能力回答他.我通读了 perldoc glob 文档并遵循了 File::Glob 链接在该页面上,但仍然找不到任何可以解释输出的内容.任何帮助将不胜感激.

解决方案

glob 在标量上下文中:

<块引用>

在标量上下文中,glob 遍历此类文件名扩展,当列表耗尽时返回 undef.

foreach (@list_env_vars){打印 "$_ = ".glob()."
";}

glob() 真的有glob($_).每次迭代,$_ 都包含字符串 $SERVER.鉴于环境变量没有改变,$SERVER 被扩展为相同的字符串.第一次,这个字符串被返回.接下来,列表已用尽,因此返回 undef.第三次,我们重新开始....

澄清:第二次调用的参数与第一次调用的参数相同并不重要,因为无法重置 glob 的迭代器.

您可以使用以下示例更清楚地看到这一点(当前目录包含文件 '1.a'、1.b'、'2.a' 和 '2.b'):

#!/usr/bin/perl -w使用严格;我的@patterns = ('*.一个','*.b',);对于我的 $v (@patterns) {print "$v = ", 标量 glob($v), "
";}

输出:

<上一页>C:温度> d*.a = 1.a*.b = 2.a

我建议通过 %ENV 哈希访问环境变量:

我的@list_env_vars = ($ENV{SERVER}) x 6;

我的@list_env_vars = @ENV{qw(HOME TEMP SERVER)};

I'm not necessarily looking for a better way to do this, rather an explanations of the output would greatly be appreciated. Recently, a senior programmer asked me why his code worked but only for one instance. What I came to find out was that it worked every other occurrence. Here is my example:

#!/usr/bin/perl -w
use strict;

my @list_env_vars = (
    '$SERVER',
    '$SERVER',
    '$SERVER',
    '$SERVER',
    '$SERVER',
    '$SERVER',
);

foreach (@list_env_vars){
    print "$_ = ".glob()."
";
}

which output for perl 5.004:

$SERVER = UNIX_SERVER
$SERVER =
$SERVER = UNIX_SERVER
$SERVER =
$SERVER = UNIX_SERVER
$SERVER =

or output for perl 5.10:

$SITE = $SITE
Use of uninitialized value in concatenation (.) or string at glob_test.pl line 14.
$SITE =
$SITE = $SITE
Use of uninitialized value in concatenation (.) or string at glob_test.pl line 14.
$SITE =
$SITE = $SITE
Use of uninitialized value in concatenation (.) or string at glob_test.pl line 14.
$SITE =

I personally have never used glob() in this fashion so I was ill equipped to answer him. I read through perldoc glob documentation and followed the File::Glob link on that page and still couldn’t find anything that would explain the output. Any help would be much appreciated.

解决方案

glob in scalar context:

In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.

In

foreach (@list_env_vars){
    print "$_ = ".glob()."
";
}

The glob() there really is glob($_). Every iteration, $_ contains the string $SERVER. Given that the environment variable does not change, $SERVER is expanded to the same string. First time, this string is returned. Next, the list is exhausted, so undef is returned. Third time, we start over. ...

Clarification: It does not matter that the argument to the second call is the same as the one for the first call since there is no way to reset glob's iterator.

You can see this more clearly using the following example (current directory contains files '1.a', 1.b', '2.a' and '2.b'):

#!/usr/bin/perl -w
use strict;

my @patterns = (
    '*.a',
    '*.b',
);

for my $v ( @patterns ) {
    print "$v = ", scalar glob($v), "
";
}

Output:

C:Temp> d
*.a = 1.a
*.b = 2.a

I would recommend accessing environment variables via the %ENV hash:

my @list_env_vars = ($ENV{SERVER}) x 6;

or

my @list_env_vars = @ENV{qw(HOME TEMP SERVER)};

这篇关于为什么 Perl 的 glob 每次调用都返回 undef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆