为什么 Perl CGI 模块使用连字符来启动命名参数?

发布于 2024-10-08 19:35:28 字数 262 浏览 0 评论 0原文

我是新手。我的问题是键(类型、过期名称等)之前的“-”代表什么?为什么不直接使用普通的哈希表方式并丢弃连字符呢?

# #!/usr/local/bin/perl -w
use CGI; 
$q = CGI->new; 
print $q->header(-type=>'image/gif',-expires=>'+3d');
$q->param(-name=>'veggie',-value=>'tomato');

I am a novice. My question is what is the "-" before the keys (type, expires name etc) standing for? Why not just use the plain hash table way and discard the hyphen?

# #!/usr/local/bin/perl -w
use CGI; 
$q = CGI->new; 
print $q->header(-type=>'image/gif',-expires=>'+3d');
$q->param(-name=>'veggie',-value=>'tomato');

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

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

发布评论

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

评论(4

调妓 2024-10-15 19:35:28

作者已经在文档中进行了解释。

大多数 CGI.pm 例程接受多个
参数,有时多达 20 个
可选的!为了简化这个
接口,所有例程都使用命名的
参数调用风格看起来像
这个:

打印
$q->header(-type=>'image/gif',-expires=>'+3d');

每个参数名称前面都有一个
短跑。大小写和顺序都不重要
在参数列表中。 -类型,-类型,
和-TYPE 都可以接受。在
事实上,只有第一个参数需要
以破折号开头。如果破折号是
出现在第一个参数中,CGI.pm
假定后续的破折号
的。

常用的几个例程
只有一个论点。在这种情况下
在这些例程中,您可以提供
没有参数的单个参数
姓名。 header() 恰好是其中之一
这些惯例。在这种情况下,
单个参数是文档类型。

打印 $q->header('text/html');

The author already explained in the documentation.

Most CGI.pm routines accept several
arguments, sometimes as many as 20
optional ones! To simplify this
interface, all routines use a named
argument calling style that looks like
this:

print
$q->header(-type=>'image/gif',-expires=>'+3d');

Each argument name is preceded by a
dash. Neither case nor order matters
in the argument list. -type, -Type,
and -TYPE are all acceptable. In
fact, only the first argument needs to
begin with a dash. If a dash is
present in the first argument, CGI.pm
assumes dashes for the subsequent
ones.

Several routines are commonly called
with just one argument. In the case
of these routines you can provide the
single argument without an argument
name. header() happens to be one of
these routines. In this case, the
single argument is the document type.

print $q->header('text/html');

预谋 2024-10-15 19:35:28

请参阅 perlop

如果操作数是标识符,则返回由减号与标识符连接组成的字符串。否则,如果字符串以加号或减号开头,则返回以相反符号开头的字符串。 这些规则的一个效果是 -bareword 相当于字符串 "-bareword"。 (强调我的)

See perlop:

If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned. Otherwise, if the string starts with a plus or minus, a string starting with the opposite sign is returned. One effect of these rules is that -bareword is equivalent to the string "-bareword". (emphasis mine)

何以畏孤独 2024-10-15 19:35:28

这只是一种较旧的 perl 参数样式,通常不会在较新的模块中使用。它并没有完全被弃用,它只是一种旧的样式,基于 Perl 如何允许您不引用以破折号开头的散列键。

This is just an older style of perl arguments that isn't usually used in newer modules. It's not exactly deprecated, it's just an older style based on how Perl allows you to not quote your hash keys if they start with a dash.

会傲 2024-10-15 19:35:28

我不知道你所说的“简单的哈希表方式”是什么意思。 CGI::pm 的实现方式(在大多数情况下)要求属性名称前面带有“-”,大概这样才能识别它们。

或者换句话说,CGI::header 标识“type”属性所需的哈希键是“-type”。

这就是 CGI.pm 的定义方式。

I don't know what you mean by the 'plain hashtable way'. The way CGI::pm is implemented, names of properties are (in most cases) required to be preceded by '-', presumably so that they can be identified.

Or to put it another way, the hash-key required by CGI::header to identify the 'type' property is '-type'.

That's just the way CGI.pm is defined.

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