Perl的ENV哈希中没有的怪异环境变量 [英] Weird environment variables that aren't in Perl's ENV hash

查看:180
本文介绍了Perl的ENV哈希中没有的怪异环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl可以访问一些环境变量:

Perl has access to some environment variables:

> echo $HOST
xtt006
> perl -E 'say $ENV{HOST}'
xtt006
> perl -E 'say `echo \$HOST`'
xtt006

但是显然有另一类不在ENV哈希中的环境变量:

But apparently there's another class of environment variables that aren't in the ENV hash:

> echo $env
opsd
> perl -E 'say $ENV{env}'
(no response from Perl)
> perl -E 'say `echo \$env`'
(no response from Perl)

那是怎么回事? Perl是否可以通过另一种方法获取$ env的值?

What's up with that? Is there another technique by which Perl can obtain the value of $env?

推荐答案

来自 perlvar中的%ENV
  (我的重点)

From %ENV in perlvar   (my emphasis)


哈希%ENV 包含您当前的环境

环境是从启动Perl进程的过程继承而来的,这里

The environment is inherited from the process that started the Perl process, here the shell.

但是, shell变量可以是 local ,它们不会传递给形成环境并由子进程继承的子进程或全局进程。全局文件大多在启动时从配置文件中读取,但是如果以后要添加它们,则它们必须为已导出

However, shell variables can be local, which aren't passed to child processes, or global, that form the environment and are inherited by child processes. The global ones are mostly read from configuration files at startup, but if they are to be added later then they need be exported


创建的变量类似于上面的示例,仅适用于当前shell。它是一个局部变量:当前shell的子进程将不知道此变量。为了将变量传递到子Shell,我们需要使用 export 内置命令将其导出。导出的变量称为环境变量。设置和导出通常在一个步骤中完成:

A variable created like the ones in the example above is only available to the current shell. It is a local variable: child processes of the current shell will not be aware of this variable. In order to pass variables to a subshell, we need to export them using the export built-in command. Variables that are exported are referred to as environment variables. Setting and exporting is usually done in one step:

export VARNAME="value" 


(我的重点) 根据您的描述,似乎将 $ env 添加为局部变量(具有 set ),然后还需要导出( export env )以便在单行中显示。

(my emphasis)   By your description it seems that $env is added as a local variable (with set), which then also need be exported (export env) in order to be seen in the one-liner.

环境变量可以与 env printenv ,而 set 列出所有变量,两者

Environment variables can be listed with env or printenv, while set lists all variables, both local and environment ones.

以上是指 bash 贝壳。 环境的概念在其他外壳程序中是相同的,并且在其中也适用相同的推理。为了使变量在子进程中可见,我们需要将其设置为全局(环境变量)。区别在于如何执行此操作。

The above refers to the bash shell. The notion of "environment" is the same in other shells, and the same reasoning applies there as well; for a variable to be seen in child processes we need to make it global ("environment variable"). The difference is in how to do this.

csh / tcsh 中使用

setenv env "value"

而不是 set var = value ,将创建​​局部变量。

instead of set var = "value", what would create a local variable.

除了使用全局变量(环境)而不是局部变量之外,还可以将变量传递给Perl代码。请参阅此帖子此帖子举例说明使用单行代码执行此操作。如果调用了实际的脚本,则使其带有参数。最好的标准方法是使用 Getopt :: Long

Apart from using a global ("environment") variable instead of a local one, one can pass the variable to Perl code. See this post and this post for examples of doing that with one-liners. If an actual script is invoked then make it take arguments; the standard, and nicest, way is with Getopt::Long,

这不是对用户的特殊要求;他们使用您的脚本并需要适当地调用它。

This isn't a special requirement on the users; they use your script and need to invoke it suitably.

这篇关于Perl的ENV哈希中没有的怪异环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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