草莓Perl是%ENV一个普通的对象

所以,在草莓Perl中,%ENV似乎是一个普通的散列(至less它不是绑定的),但它似乎也不区分大小写的解决密钥。 这是有道理的,因为环境variables在PowerShell中不区分大小写(我不确定Windows环境variables和大小写之间的确切关系)。 所以,我想知道如何%ENV可以同时是一个普通的散列,并有“隐式”的键,当你打电话他们内置的密钥没有列出。 %ENV哈希是不是被绑定的神奇?

use strict; use warnings; # Env appears to be an ordinary hash # 'TEMP' is present in the list of environment variables # but 'temp' is not printf "$_\n" foreach keys %ENV; print "\n"; # this gives us the value of $ENV:TEMP # which makes sense. printf "uppercase 'temp' maps to %s\n", $ENV{TEMP}; # even though temp is not present in the # list of keys, the same value appears as for # $ENV{TEMP} # This seems to make sense given the behavior of powershell # (I am not quite sure what the relationship is between # Windows environment variables and case, but this seems reasonable.) printf "lowercase 'temp' maps to %s\n", $ENV{temp}; # However, %ENV is not a tied hash. printf "Tie status: %s\n", tied(%ENV) // "not a tied variable"; 

以下是来自ActivePerl,但每个包括草莓Perl的Perl都会给你同样的东西:

 >perl -E"use Devel::Peek; Dump(\%ENV, 1);" SV = IV(0x74b154) at 0x74b154 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x63e33c SV = PVHV(0x62a8ac) at 0x63e33c REFCNT = 2 FLAGS = (SMG,RMG,SHAREKEYS) MAGIC = 0x639084 <------- MG_VIRTUAL = &PL_vtbl_env MG_TYPE = PERL_MAGIC_env(E) ARRAY = 0x640794 (0:30, 1:22, 2:11, 3:1) hash quality = 107.7% KEYS = 47 FILL = 34 MAX = 63 

正如你所看到的, %ENV是神奇的。 但是它并不捆绑; 这将是魔法类型“P”而不是“E”。

%ENV哈希是不是被绑定的神奇?

是的,%ENV是一个不受约束的魔术变量,类似于其他各种变量,如%SIG。 更多的这些参见perldoc perlvar 。