如何将变量传递到 Perl 脚本中的 URL 中?

发布于 2024-09-28 05:54:31 字数 321 浏览 0 评论 0原文

如何将变量传递到 Perl 脚本中的 URL 中? 我正在尝试将数组中的变量传递给 url。由于某种原因它不起作用。我不确定我做错了什么。代码大致如下:

@coins = qw(Quarter Dime Nickel);

foreach (@coins) {
  my $req = HTTP::Request->new(POST =>'https://url/$coins.com');
 } 

这不起作用,因为 $coins 不会分别切换到 Quarter、Dime、Nickel。

我做错了什么?

How do I pass a variable into a URL in a Perl script?
I am trying to pass the variables in an array to url. For some reason it is not working. I am not sure what I am doing wrong. The code roughly looks like this:

@coins = qw(Quarter Dime Nickel);

foreach (@coins) {
  my $req = HTTP::Request->new(POST =>'https://url/$coins.com');
 } 

This does not work as $coins does not switch to Quarter,Dime,Nickel respectively.

What am I doing wrong?

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

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

发布评论

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

评论(2

許願樹丅啲祈禱 2024-10-05 05:54:31

首先,变量不会插入单引号字符串中:

my $req = HTTP::Request->new(POST => "https://url/$coins.com");

其次,任何地方都没有定义变量 $coins

foreach my $coin (@coins) {
  my $req = HTTP::Request->new(POST => "https://url/$coin.com");
 }

另外,请确保使用 严格警告

您还应该投入一些时间来正确学习 Perl。

First, variables do not interpolate in single quoted strings:

my $req = HTTP::Request->new(POST => "https://url/$coins.com");

Second, there is no variable $coins defined anywhere:

foreach my $coin (@coins) {
  my $req = HTTP::Request->new(POST => "https://url/$coin.com");
 }

Also, make sure to use strict and warnings.

You should also invest some time into learning Perl properly.

2024-10-05 05:54:31

使用

'https://url/' . $_  . '.com'

而不是你的

'https://url/$coins.com'

Use

'https://url/' . $_  . '.com'

Instead of your

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