Perl中的$ ENV {$ variable} [英] $ENV{$variable} in perl

查看:412
本文介绍了Perl中的$ ENV {$ variable}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中是不是通过$ ENV {$ variable}扩展变量?

Is that anyway in Perl to expand the variable by in $ENV{$variable}?

我在外壳程序中导出了"a = T"和"T = b",并运行了Perl脚本,其中打印了"$ ENV {$ a} \ n",但没有打印任何内容.我想"b"可以打印,那我应该在Perl中怎么做?

I exported "a=T" and "T=b" in shell, and I run the Perl script in which print "$ENV{$a}\n", but nothing printed. I want to "b" can be printed, then how should I do in Perl?

推荐答案

您说的应该链接这些环境变量,所以

Those environment variables should be chained you say, so

$ENV{ $ENV{a} };

注意:不是$a,而是a,例如$ENV{USER}等.这使用哈希%ENV(请参见 perlvar ),它具有当前环境,因此键是环境变量的名称.

Note: not $a but a, like $ENV{USER} etc. This uses the hash %ENV (see perlvar), which has the current environment, so with keys being names of environment variables.

显然在%ENV中使用Perl变量(用于shell变量的名称)是有趣的,而不是上面的字符串文字.在这种情况下,我们需要以某种方式将该shell变量(其名称或值)传递给Perl程序,以便将其存储在变量中.不能直接使用它.

It is apparently of interest to use a Perl variable (for the shell variable's name) in %ENV, and not a string literal as above. In that case we need to pass that shell variable, its name or the value, to the Perl program somehow so to have it stored in a variable; can't just use it directly.

顺便说一句,将变量从shell传递到Perl的方法之一就是通过导出它,然后通过%ENV使它可用.但是,它也可以照常通过命令行传递.假设使用Perl单行代码(在shell脚本中很常见),我们有两种方法可以通过

Incidentally, one of the ways to pass a variable from shell to Perl is precisely by exporting it, what then makes it available via %ENV. However, it can also be passed as usual, via command line. Assuming the use of a Perl one-liner (common in shell scripts), we have two options for how to pass

  • perl -we'...' "$var"作为参数,在这种情况下,它可以在@ARGV

  • As an argument, perl -we'...' "$var", in which case it is available in @ARGV

通过 -s命令开关,然后在单行代码中用值$var设置$shv变量. --标记参数的开始.

Via the -s command switch, perl -s -we'...' -- -shv="$var", what sets up $shv variable in the one-liner, with the value $var. The -- mark the start of arguments.

有关详细信息,请参见此帖子,也许还请参见

See this post for details, and perhaps this one for another, more involved, example.

注意 注释询问如何传递变量的名称(字符串a),而不是其值($a).对于我来说,这似乎不是最好的设计.如果出于某种原因需要传递变量名称,则将 that 存储在变量(var="a")中并如上所述传递该变量是有意义的.

Note  A comment asks how to pass variable's name (string a), not its value ($a). This doesn't seem as the best design to me; if the name of a variable for some reason need be passed around then it makes sense to store that in a variable (var="a") and pass that variable, as above.

但是,如果确实要传递名称本身的想法,则改为这样做,所以无论哪种方式

But if the idea is indeed to pass the name itself around, then do that instead, so either of

perl -we'...' "a"
perl -we'...' -s -- -shv="a"

其余部分相同,并且%ENV使用分配了输入的变量.

The rest is the same and %ENV uses the variable that got assigned the input.

如果使用了完整的Perl脚本(而不是单行),则使用 Getopt ::很长,可以很好地处理广告.

If a full Perl script is used (not a one-liner) then use Getopt::Long to nicely handle arugments.

 注释询问有关将shell变量的 name 传递给Perl变量的信息—因此,OP中的a而不是其值$a.我不确定它的用途,但它当然是可能的.

 A comment asks about passing the shell variable's name to a Perl variable — so a from the OP, not its value $a. I am a little uncertain of the utility of that but it is of course possible.

然后,如何将变量从shell传递到Perl的两种方式在传递的内容上有所不同.

The two ways for how to pass a variable from shell to Perl then differ in what is passed.

这篇关于Perl中的$ ENV {$ variable}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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