Chinaunix

标题: 怎么样动态注入一段代码来运行 [打印本页]

作者: yakczh_cu    时间: 2013-05-26 12:17
标题: 怎么样动态注入一段代码来运行

  1. sub call(&$)
  2.   {
  3.                 my ($code, $para, $file, $re) = @_;
  4.              $processResult= $code->($para);
  5.                 print $processResult;
  6.                
  7.   }
  8.   

  9. my $para= "http://bbs.chinaunix.net";

  10. call  {
  11.       my $para=shift;
  12.           print "\npara is ".$para;
  13.           
  14.           return "\n$para  changed to -> ".uc($para)." ";
  15.           
  16.       
  17.    } $para;
复制代码
这相当于把一段代码当成参数传进去执行,但只能这样硬编码,有没有办法,可以从单独的文件读进来 然后再插入进去
作者: picbhan    时间: 2013-05-26 18:30
eval不行吗?或者读进来存为一个字符串传进去?
作者: yakczh_cu    时间: 2013-05-26 19:51
这是个结构,不是变量
作者: rubyish    时间: 2013-05-27 00:14
picbhan 发表于 2013-05-26 14:30
eval不行吗?或者读进来存为一个字符串传进去?

学习~ 3Q~
作者: laputa73    时间: 2013-05-27 08:37
perl是脚本语言,动态支持是天然的
变量\结构\甚至函数都可以动态生成和加载
而且,结构和函数本身也是一种变量
作者: grshrd49    时间: 2013-05-27 09:57
  1. &$
复制代码
这个是啥意思啊
作者: brantc    时间: 2013-05-27 10:39
回复 6# grshrd49

perldoc perlvar
......
$&      The string matched by the last successful pattern match (not
            counting any matches hidden within a BLOCK or "eval()" enclosed
            by the current BLOCK).

            The use of this variable anywhere in a program imposes a
            considerable performance penalty on all regular expression
            matches. To avoid this penalty, you can extract the same
            substring by using "@-". Starting with Perl 5.10, you can use
            the "/p" match flag and the "${^MATCH}" variable to do the same
            thing for particular match operations.

            This variable is read-only and dynamically-scoped.

            Mnemonic: like "&" in some editors.
作者: grshrd49    时间: 2013-05-27 10:51
本帖最后由 grshrd49 于 2013-05-27 10:52 编辑

回复 7# brantc


    反了啊 ,
我知道$&是获取正则中最后一次匹配的字符串
但是楼主那个函数中传的参数是 &$  诶
费解啊 手误?还是什么奇怪的特殊标量呢
作者: iakuf    时间: 2013-05-27 11:35
回复 8# grshrd49

brantc 的答案是错的,在函数中的这个叫函数原型。
   
作者: yakczh_cu    时间: 2013-05-27 13:48
回复 5# laputa73
  1. sub call(&$)
  2.   {
  3.                 my ($code, $para, $file, $re) = @_;
  4.              $processResult= $code->($para);
  5.                 print $processResult;
  6.                
  7.   }
  8.   
  9. my $code;
  10. open FH,'test.pl';
  11. do {local $/;$code=<FH>;};
  12. print $code;
  13. my $para= "http://bbs.chinaunix.net";

  14. call eval($code)   $para;
复制代码
Type of arg 1 to main::call must be block or sub {} (not eval "string")
作者: rubyish    时间: 2013-05-27 14:50
本帖最后由 rubyish 于 2013-05-27 10:51 编辑

2 example:

办法 1:
  1. sub hello {
  2.     my ( $c, $p ) = @_;
  3.     my $C = eval 'sub {' . $c . '}';
  4.     print $C->($p);
  5. }
  6. my $code = do { local $/; <DATA> };
  7. hello $code, 'world';
  8. __DATA__
  9. my $p = shift;
  10. uc $p
复制代码
  1. WORLD
复制代码

作者: rubyish    时间: 2013-05-27 14:53
办法 2:
  1. sub hello(&$) {
  2.     my ( $c, $p ) = @_;
  3.     print $c->($p);
  4. }
  5. my $code = do { local $/; <DATA> };
  6. my $sub = eval 'sub { ' . $code . '}';
  7. hello { &$sub } 'world';
  8. __DATA__
  9. my $p = shift;
  10. uc $p
复制代码
  1. WORLD
复制代码

作者: brantc    时间: 2013-06-07 09:25
回复 9# iakuf


    哦?perldoc文档呀,在这里不适用吗?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2