我希望能够抓住从Perl eval中可变的任务。 也就是说,确定在法典中指定哪些可变名称并提取其价值。
例如,如果我跑的话:
eval $foo=42; $bar=3.14;
eval是3.14(最后估价值),但我也能够确定“$”和“bar”的名称及其价值(不事先知道姓名)。
我阅读了通过安全、平等:文字,但还不是提取这些变量的任何方法,将各种变量纳入电子阀门。 我更熟悉为支持这一点而建造的Sharma Eval/exec。
我希望能够抓住从Perl eval中可变的任务。 也就是说,确定在法典中指定哪些可变名称并提取其价值。
例如,如果我跑的话:
eval $foo=42; $bar=3.14;
eval是3.14(最后估价值),但我也能够确定“$”和“bar”的名称及其价值(不事先知道姓名)。
我阅读了通过安全、平等:文字,但还不是提取这些变量的任何方法,将各种变量纳入电子阀门。 我更熟悉为支持这一点而建造的Sharma Eval/exec。
在此,我试图根据Eric Strom的建议,在
注:package main;
use warnings; use strict;
use Safe;
my $cpt = new Safe;
$cpt->permit_only(qw(sassign lineseq padany const rv2sv leaveeval));
my $name_space = $cpt->root;
my $no_strict = 0;
#
# populate the clean symbol table
#
$cpt->reval( 0 );
die "safe compartment initialisation error: $@" if $@;
my %symtab_clean = do {no strict refs ; %{$name_space. :: } };
my $result = $cpt->reval( $foo=42; $bar=3.14; , $no_strict);
if ($@) {
warn "eval error: $@";
}
else {
#
# symbol table additions
#
my %symtab_dirty = do {no strict refs ; %{$name_space. :: } };
my @updated_variables = grep { ! exists $symtab_clean{$_} } (sort keys %symtab_dirty);
foreach my $variable (@updated_variables) {
my $value = do{ no strict refs ; ${$name_space. :: .$variable} };
print "variable $variable was set to: $value
"
}
}
在等值内宣布的任何弹性变量将在等值结束后丢失。 为了捕获和孤立一个电子版本中确定的全球变量,你可以研究如何使用Safe。 建立新全球名称空间的模块。 类似于:
use Safe;
my $vars = Safe->new->reval(qq{
$code_to_eval;
$code_to_search_the_symbol_table_for_declared_variables
});
如果搜索代码被界定为绕过封顶的<代码>%main:的符号表格搜寻任何感兴趣的变量。 你们可以返回包含信息的数据结构,然后可以与你一样这样做。
如果你只担心在根基层面界定的变量,你可以写出如下内容:
use strict;
use warnings;
my $eval_code = $foo=42; $bar=3.14; ;
use Safe;
my $vars = Safe->new->reval(
$eval_code . q{;
my %vars;
for my $name (keys %main::) {
next if $name =~ /::$/ # exclude packages
or not $name =~ /[a-z]/; # and names without lc letters
my $glob = $main::{$name};
for (qw($SCALAR @ARRAY %HASH)) {
my ($sigil, $type) = /(.)(.+)/;
if (my $ref = *$glob{$type}) {
$vars{$sigil.$name} = /$/ ? $$ref : $ref
}
}
}
\%vars
});
print "$_: $$vars{$_}
" for keys %$vars;
# $foo: 42
# $bar: 3.14
搜索法还可以使用Padwalker,利用peek_my
功能,搜索任何界定的变量的现有弹性范围。
$的价值在<代码>eval之后将达42美元。 范围没有变化,因此无法预见到 $。
但这当然假定存在 $。
无论如何,这只能是在没有<条码>使用严格的条码>的情况下进行的,这几乎肯定是坏的。 有了<条码>严格使用条码>,该笔文字汇编了。 我以你的榜样更为复杂,你重新审视了由您的<代码>eval<>/代码”进行的对现有散射物的改动。 但不清楚您的<代码>eval。 不能知道它正在发生变化,除非有某种动态的守则注射。
I am building a Web interface to monitor an embedded system. I have built a Perl script which runs remote commands and gathers output from that system. Now what I need is a Web interface which makes ...
How do I tell what type of value is in a Perl variable? $x might be a scalar, a ref to an array or a ref to a hash (or maybe other things).
When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...
I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...
I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...
I would like to submit a form to a CGI script localy (w3c-markup-validator), but it is too slow using curl and apache, I want to use this CGI script more than 5,000 times in an another script. and ...
So I m running perl 5.10 on a core 2 duo macbook pro compiled with threading support: usethreads=define, useithreads=define. I ve got a simple script to read 4 gzipped files containing aroud 750000 ...
A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...