windows - Strawberry Perl 是 %ENV 一个普通的对象

标签 windows perl environment-variables strawberry-perl

因此,在 Strawberry Perl 中,%ENV 似乎是一个普通的散列(至少它没有绑定(bind)), 但它似乎也不区分大小写地解析键。考虑到环境,这是有道理的 变量在 PowerShell 中不区分大小写(我不确定它们之间的确切关系 Windows 环境变量和大小写)。所以,我想知道 %ENV 如何同时成为 普通散列,并具有“隐式”键,当您称它们为内置键时,这些键未列出。 %ENV hash 不用绑定(bind)就神奇吗?

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,但包括 Strawberry 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 很神奇。但它没有绑定(bind);那将是魔术类型“P”而不是“E”。

关于windows - Strawberry Perl 是 %ENV 一个普通的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33448334/

相关文章:

windows - 为什么我的 Qt 应用程序即使在非管理员访问权限下也会写入 protected 位置?

python - 为什么默认情况下 Python 没有安装在 Windows 上?

c++ - 如何使用其客户区实现拖动窗口?

perl - 使用索引数组对数组进行子采样

Windows 中带有环境变量的 Ruby 系统调用

linux - 在 Apache 配置中有条件地设置环境变量

窗口命令 : List files in dir & subdir WITHOUT given extensions

mysql - Bugzilla 安装 usr/bin/perl install-module.pl DBD::mysql 时出错

perl - 如何在perl中将字符串截断为特定长度?

php - 从环境文件中删除后,Laravel Forge 继续通过 Mandrill 发送邮件