使用eval初始化perl变量 [英] Initializing perl variables using eval

查看:96
本文介绍了使用eval初始化perl变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜想这对那些了解Perl的人来说应该是显而易见的,但我只是不明白..我也猜想这与-但我无法在我的情况下应用任何这种方法.

I'm guessing this should be something obvious to those knowing Perl, but I simply don't get it.. I also guess it has to do with problems described in Perl scoping « darkness - but I cannot apply any of that in my case.

无论如何,这是代码:

#!/usr/bin/env perl
# call with:
# ./test.pl

use strict;

my $tvars = "my \$varA = 1;
my \$varB = 2;
my \$varC = 3;
";

my @lines = split /\n/, $tvars;
foreach my $line (@lines) {
  print "$line\n";
  eval $line; warn $@ if $@;
}

#~ print "$varA\n"; # Global symbol "$varA" requires explicit package name at ./test.pl line 18.
#~ print "$varB\n"; # Global symbol "$varB" requires explicit package name at ./test.pl line 19.
#~ print "$varC\n"; # Global symbol "$varC" requires explicit package name at ./test.pl line 20.

$tvars = "our \$varA = 1;
our \$varB = 2;
our \$varC = 3;
";

@lines = split /\n/, $tvars;
foreach my $line (@lines) {
  print "$line\n";
  eval $line; warn $@ if $@;
}

print "$varA\n"; # Global symbol "$varA" requires explicit package name at ./test.pl line 33.
print "$varB\n"; # Global symbol "$varB" requires explicit package name at ./test.pl line 34.
print "$varC\n"; # Global symbol "$varC" requires explicit package name at ./test.pl line 35.

简而言之,我想将类似"$varA = 1;"的内容写为字符串(文本文件);并且我想将perl转换为eval,这样以后我就可以在同一脚本中访问变量"$varA"-当我尝试访问eval之后的变量时遇到的错误上面代码的注释(但是,在eval中未报告任何警告). (我猜,如果eval在与主脚本不同的上下文中运行,我需要的是全局"变量之类的东西?)

Simply speaking, I'd like to have something like "$varA = 1;" written as a string (text file); and I'd like perl to eval it, so that afterwards I have access to variable "$varA" in the same script - the errors I get when I try to access those after an eval are in the comments of the code above (however, no warnings are reported during the eval). (I'm guessing, what I'd need is something like "global" variables, if the eval runs in a different context than the main script?)

我该怎么做呢?我是否必须经历所有的包定义业务,即使是像上面这样的简单脚本也是如此?

How would I go about doing that? Do I have to go through all of that package definition business, even for a simple script like the above?

在此先感谢您的回答,
干杯!

Many thanks in advance for any answers,
Cheers!

推荐答案

它与范围界定有关.变量在eval表达式中用my声明.这使它们在eval语句本地,并且eval语句退出后将无法访问.不过,您可以先声明它们:

It has everything to do with scoping. The variables are declared with my inside the eval expression. This makes them local to the eval statement and not accessible once the eval statement exits. You can declare them first, though:

my ($varA, $varB, $varC);  # declare outside the eval statement

my $tvars = "\$varA = 1;
\$varB = 2;
\$varC = 3;
";

eval $tvars;
# local $varA, $varB, $varC variables are now initialized

或者按照您的建议,可以使用全局变量.最简单(尽管不一定是最佳"方法)是将::放在所有变量名的前面,并将其放入主程序包.

or as you suggest, you can use global variables. The easiest (though not necessarily the "best" way) is to prepend :: to all variable names and get them in the main package.

my $tvars = "\$::varA = 1;
\$::varB = 2;
\$::varC = 3;
";

eval $tvars;
print "A=$::varA, B=$::varB, C=$::varC\n";

现在,在示例中尝试使用our变量时,实际上是在初始化程序包(全局)变量.但是在eval语句之外,您仍然需要限定(即,指定程序包名称)它们才能访问它们:

Now when you tried our variables in your example, you actually were initializing package (global) variables. But outside the eval statement, you still need to qualify (i.e., specify the package name) them in order to access them:

$tvar = "our \$foo = 5";
eval $tvar;

print $main::foo;    # ==> 5

这篇关于使用eval初始化perl变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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