为什么Perl的全球电话返回undef? [英] Why does Perl's glob return undef for every other call?

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

问题描述

我不一定要寻找更好的方式来做到这一点,而是对输出的解释将非常感激。最近,一位高级程序员问我为什么他的代码工作,但只有一个实例。我来了解的是,它发挥其他作用。这是我的例子:

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()."\n";
}

perl的输出5.004:

which output for perl 5.004:

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

或输出perl 5.10:

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 =



我个人从来没有使用过glob(),所以我没有能力回答他。我阅读了 perldoc glob 文档,并遵循 File :: Glob 链接,仍然找不到任何可以解释输出的内容。任何帮助将不胜感激。

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 在标量上下文中:

glob in scalar context:


在标量上下文中,glob遍历这样的文件名扩展,当列表耗尽时返回undef。

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

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

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

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. ...

澄清:第二个呼叫的参数与第一个呼叫的参数相同,因为那里无法重置 glob 的迭代器。

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.

您可以使用以下示例(当前目录包含文件'1.a',1.b','2.a'和'2.b'):

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), "\n";
}

输出:


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

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

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

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

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

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

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