perl - 为什么 Perl CGI 模块使用连字符开始命名参数?

标签 perl cgi hashtable

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

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

最佳答案

作者已经解释了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');

关于perl - 为什么 Perl CGI 模块使用连字符开始命名参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4511568/

相关文章:

php - 如何用 C 语言将独立的 PHP 进程与 apache CGI 进程分离?

c++ - 在 google::dense_hash_map 中存储 std::vectors 使其变慢

c - 二进制哈希函数族

perl - mod_perl 处理包含路径的方式与 cgi 不同?

perl - 为条件 OO 模块加载编写包装器模块的正确方法是什么?

perl - 使用 perl 将 html 转换为文本

c - 增长动态哈希表

perl - 一个perl命令行,打印用于运行脚本的perl版本

html - For 循环正在添加一个选项卡本身

perl - 应该使用CGI.pm的header方法来输出Content-Type header吗?