如果我使用backtics调用它,如何将curl的输出转换为Perl中的变量?

Gen*_* S. 8 perl curl backticks qx

我试图将curl调用的响应转换为perl中的变量.

my $foo = `curl yadd yadda`;

print $foo;
Run Code Online (Sandbox Code Playgroud)

不起作用.当我在命令行运行它时,curl调用在终端中正确打印其所有输出,但该变量未填充该数据.

有没有办法在不安装和调用Perl curl lib的情况下执行此操作?

sun*_*256 13

它可能会将其内容发送给stderr.尝试

my $foo = `curl yadd yadda 2>&1`;
Run Code Online (Sandbox Code Playgroud)

  • 2> foo表示"将stderr重定向到文件foo".&1表示stdout,即将stderr重定向到stdout.您也可以通过其他方式执行此操作:">&2"表示"将stdout重定向到stderr".要重定向stdout和stderr,可以使用">&",即foocommand&> file_with_boththings.txt (2认同)

jig*_*ggy 5

您也可以考虑查看LWP :: UserAgent甚至LWP :: Simple.