为什么在对 HTTP::Request.new 的调用中出现“传递的位置过多……”?

Why am I getting “Too many positionals passed…” in this call to HTTP::Request.new?

我在这段代码上尝试了六种左右的变体,除了像 GET => … 这样的硬编码 Str 之外,我总是遇到这个错误。为什么?我该如何修复它并理解它?是 HTTP::Request 代码中的错误吗?

代码
#!/usr/bin/env perl6
use HTTP::UserAgent; # Installed today with panda, for HTTP::Request.

HTTP::Request.new( GET => "/this/is/fine" ).WHICH.say;

# First, check that yes, they are there.
say %*ENV<REQUEST_METHOD>, " ", %*ENV<REQUEST_URI>; 

# This and single value or slice combination always errors-
HTTP::Request.new( %*ENV<REQUEST_METHOD>, %*ENV<REQUEST_URI> );
具有不变错误的输出
$ env REQUEST_METHOD=GET REQUEST_URI=/ SOQ.p6
HTTP::Request|140331166709152
GET /
Too many positionals passed; expected 1 argument but got 3
  in method new at lib/HTTP/Request.pm6:13
  in block <unit> at ./SOQ.p6:11

HTTP::Request 来自这个包 — https://github.com/sergot/http-useragent/ — 谢谢!

尝试

HTTP::Request.new(|{ %*ENV<REQUEST_METHOD> => %*ENV<REQUEST_URI> });

而不是更明显的

HTTP::Request.new( %*ENV<REQUEST_METHOD> => %*ENV<REQUEST_URI> );

如果 => 的左侧不是文字,我们将不会绑定到命名参数。相反,一对对象作为位置参数传递。

为了解决这个问题,我们构建了一个匿名散列,通过前缀 |.

将其扁平化到参数列表中

作为奖励,这里有一些更有创意的方法:

HTTP::Request.new(|%( %*ENV<REQUEST_METHOD REQUEST_URI> ));
HTTP::Request.new(|[=>] %*ENV<REQUEST_METHOD REQUEST_URI> );