代码之家  ›  专栏  ›  技术社区  ›  Simon Peverett

为什么Perl的%env的空字符串值的键不显示Windows子进程?

  •  3
  • Simon Peverett  · 技术社区  · 15 年前

    我想(需要)从检查特定环境变量的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。

    3 回复  |  直到 15 年前
        1
  •  9
  •   JB.    15 年前

    据我所知,Windows中没有空的环境变量。将它们定义为空与取消定义它们是相同的。

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

        2
  •  0
  •   hlovdal    15 年前

    这似乎是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
    
    $
    
        3
  •  0
  •   Nifle Hassan Syed    15 年前

    在Vista上同时使用草莓Perl和Cygwin Perl会导致

    ZZZ_1 = One
    ZZZ_2 = Two
    ZZZ_4 = Four