为什么 Perl CGI 模块使用连字符来启动命名参数?
我是新手。我的问题是键(类型、过期名称等)之前的“-”代表什么?为什么不直接使用普通的哈希表方式并丢弃连字符呢?
# #!/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
作者已经在文档中进行了解释。
The author already explained in the documentation.
请参阅 perlop:
See perlop:
这只是一种较旧的 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.
我不知道你所说的“简单的哈希表方式”是什么意思。 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.