如何在 Perl 中运行动态代码?

发布于 2024-09-24 16:56:01 字数 658 浏览 3 评论 0原文

我有一个关于 Perl 动态代码的问题。 Perl 中是否有一个构造可以用来执行代码。

例如,

$command = " some command";
$perl -> execute($command);

$command 在运行时发生变化。抱歉我的术语。否则我不知道如何解释。

我试图实现这一点:我有一个函数,可以为数组中的每个引用执行一些命令。

 ...insert example call to function...

假设每个引用都有一个关联的标识符。

 ...insert sample reference...
 ...say something about how you determine the identifier...
 ...say something about how you want to use the identifier...

我无法迭代数组并对每个引用执行,因为命令的一部分因每个引用而异,因为它具有标识符的一部分,并且没有详尽的标识符列表。例如,如果 $r0 具有标识符“r0”,则应执行命令 $r0->test("r0")

I have a question relating to Perl dynamic code. Is there a construct in Perl where I could use to execute code.

For instance,

$command = " some command";
$perl -> execute($command);

$command changes in run time . Sorry for my terminology. I do not know how to explain it otherwise.

I am trying to accomplish this: I have a function which execute some command for each reference in a array.

 ...insert example call to function...

Let's say each reference has an identifier associated.

 ...insert sample reference...
 ...say something about how you determine the identifier...
 ...say something about how you want to use the identifier...

I can't iterate through the array and execute for every reference since a portion of the command varies for each reference as it has a part of the identifier and there is no exhaustive list of identifiers. For instance if $r0 has an identifier 'r0', the command $r0->test("r0") should be executed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨洒年华 2024-10-01 16:56:01

如果这是一个外部命令,您可以使用系统、反引号或 Perl 模块之一来完成此类操作。

如果你想在运行时编译并执行Perl代码,你可以使用eval。在许多情况下,人们滥用 eval 因为他们看不到更简单的方法来完成工作。

如果你想根据情况决定运行哪个子例程,你可以做各种事情,包括使用软引用(讨厌!但有时很有用)、调度表等等。

如果您想根据变量中的值选择一种方法,这也很容易:

 $object->$method(...)

但是,您必须告诉我们您想要完成什么。


我仍然不确定你在问什么,但这听起来并不像任何动态的东西。我想这就是你所描述的情况。告诉我这有多接近:

 my @array = ( [ 'r0', 'foo', 'bar' ] );

 foreach my $element ( @array ) {
      my( $identifier, @other_stuff ) = @$element;
      $element->test( $identifier );
      }

如果您可以显示一些示例元素和更完整的(即使是伪代码)概述您所拥有的内容,那将会非常有帮助。我们只知道您在问题中告诉我们的内容,而不是您对自己问题的所有了解。

If this is an external command, you can use one of system, backticks, or a Perl module for that sort of thing.

If you want to compile and execute Perl code at run time, you can use eval. In many cases, people abuse eval because they don't see the simpler way to get things done.

If you want to decide which subroutine to run based on the situation, you can do various things, including using soft references (ick!, but useful sometimes), dispatch tables, and so on.

If you want to choose a method based on the value in a variable, that's easy too:

 $object->$method(...)

However, you'd have to tell us what you are trying to accomplish.


I'm still not sure what you are asking, but it doesn't really sound like anything dynamic. I think this is the situation you are describing. Tell me how close this is:

 my @array = ( [ 'r0', 'foo', 'bar' ] );

 foreach my $element ( @array ) {
      my( $identifier, @other_stuff ) = @$element;
      $element->test( $identifier );
      }

It would really help if you can show some sample elements and a more complete, if even pseudo code, outline of what you have. We only know what you tell us in your question, not everything you know about your own problem.

情丝乱 2024-10-01 16:56:01

您可以使用 eval

eval($command);

来自 手册

评估 EXPR

在第一种形式中,EXPR 的返回值被解析并执行,就好像它是一个小型 Perl 程序...

You can use eval:

eval($command);

From the manual:

eval EXPR

In the first form, the return value of EXPR is parsed and executed as if it were a little Perl program...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文