【问题标题】:Why don't the keys with empty string values for Perl's %ENV show up Windows subprocesses?为什么 Perl 的 %ENV 的具有空字符串值的键不显示 Windows 子进程?
【发布时间】:2010-11-22 13:09:49
【问题描述】:

我想(需要)从一个检查某些环境变量的 perl 脚本启动一个子进程。在一种情况下,环境变量需要存在但为空。

 $ENV{"GREETING"} = "Hello World";        # Valid
 $ENV{"GREETING"} = "";                   # also valid

我可以设置 $ENV{"GREETING"} = "";并且在那个 perl 脚本中 $ENV{"GREETING"} 是空的,但在任何子进程中,环境变量都不存在。

这里有一些示例代码来演示。这个脚本,env_in.pl 设置了一些环境变量,ZZZ_3 为空。然后调用 env_out.pl 输出环境变量,输出中缺少 ZZZ_3。

#!/usr/bin/perl
# env_in.pl

use strict;`enter code here`
use warnings;

$ENV{ZZZ_1} = "One";
$ENV{ZZZ_2} = "Two";
$ENV{ZZZ_3} = "";
$ENV{ZZZ_4} = "Four";

my (@cmd) = ("perl", "env_out.pl");
system(@cmd) == 0 or die "system @cmd failed: $?";

这里是 env_out.pl 脚本。

#!/usr/bin/perl

use strict;
use warnings;

print ($_," = ", $ENV{$_}, "\n") for (sort keys %ENV);

我在 WinXP 机器上使用 ActiveState perl 版本 v5.8.8。

我知道这在 python 中可以工作,但我没有选择实现语言,它必须是 Perl。

【问题讨论】:

  • 看来问题出在 windows 而不是 perl(见下文)。
  • 这可能是与您的 Python 脚本进行交叉检查的好时机;)
  • 我没有在 DOS 中运行我的 perl 脚本,而是在 IPython 中运行它——Doh!

标签: windows perl environment-variables


【解决方案1】:

据我所知,Windows 中不存在空环境变量。将它们定义为空与取消定义它们是一回事。

在类 Unix 系统上运行时,您的 Perl 脚本确实会显示 ZZZ_3 条目。

【讨论】:

    【解决方案2】:

    这似乎是您的 activestate perl 的一些问题。在 windows2000 + cygwin 上测试给出:

    $ perl --version
    
    This is perl, v5.10.0 built for cygwin-thread-multi-64int
    (with 6 registered patches, see perl -V for more detail)
    
    Copyright 1987-2007, Larry Wall
    
    Perl may be copied only under the terms of either the Artistic License or the
    GNU General Public License, which may be found in the Perl 5 source kit.
    
    Complete documentation for Perl, including FAQ lists, should be found on
    this system using "man perl" or "perldoc perl".  If you have access to the
    Internet, point your browser at http://www.perl.org/, the Perl Home Page.
    
    
    $ perl env_in.pl | grep ZZZ
    ZZZ_1 = One
    ZZZ_2 = Two
    ZZZ_3 =
    ZZZ_4 = Four
    
    $
    

    【讨论】:

    • Cygwin 环境变量是否遵循与 Windows 相同的规则?
    • 可能不会出现(这并非不合理,因为 cygwin 的目的是提供类似 unix 的环境)。
    • @JB 如果他在 bash 中运行脚本,则不会。如果您从普通的旧 cmd.exe shell 运行脚本,使用 Cygwin perl,您仍然不会看到内容设置为空字符串的变量。
    【解决方案3】:

    在 vista 上同时使用草莓 perl 和 cygwin perl 会导致

    ZZZ_1 = One
    ZZZ_2 = Two
    ZZZ_4 = Four
    

    【讨论】:

      猜你喜欢
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 2021-04-20
      • 2019-07-30
      • 2018-12-11
      • 2014-02-03
      相关资源
      最近更新 更多