在 Perl 中,为什么当我尝试使用字符串 eval 时会出现语法错误?

发布于 2024-07-30 18:52:42 字数 177 浏览 2 评论 0原文

为什么这不起作用?

eval "$response = $ua->request($r);"
print "$@";

给出:

syntax error at (eval 8) line 1, near "=" 

Why isn't this working?

eval "$response = $ua->request($r);"
print "$@";

gives:

syntax error at (eval 8) line 1, near "=" 

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

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

发布评论

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

评论(3

夜光 2024-08-06 18:52:49

一个更好的问题是为什么你首先要使用 eval ? 我怀疑您正在使用 LWP::UserAgent ,除非您实现自己的请求对象,否则“request”方法不太可能消失。

因此,为什么不简单地使用:

$response = $ua->request($r);

An even better better question is why you are using eval in the first place? I suspect that you are using LWP::UserAgent and unless you implement your own request object, the 'request` method is unlikely do die.

Thus, why not simply use:

$response = $ua->request($r);

?

月下凄凉 2024-08-06 18:52:48

它不起作用,因为双引号字符串会受到插值的影响,随着这些变量的扩展,这不会顺利进行。 并且您需要在 eval 外部添加分号,而不是在内部添加分号。 尝试像这样的单引号:

eval '$response = $ua->request($r)';

It isn't working because your double-quoted string is subject to interpolation, which is not going to go well, with those variables being expanded in place. And you need a semicolon outside your eval, not so much inside it. Try single quotes like so:

eval '$response = $ua->request($r)';
生生漫 2024-08-06 18:52:47

更好的问题是为什么使用字符串 eval,而不是块 eval?

eval { $response = $ua->request($r); }
print "$@";

A better question is why you are using a string eval, instead of a block eval?

eval { $response = $ua->request($r); }
print "$@";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文