在Perl中,如何在命令行上发送CGI参数?

sli*_*oad 6 parameters perl cgi command-line-arguments

通常我从网页获取数据但我想从命令行发送它以方便调试.

为了得到数据,我做了类似的事情:

my $query = new CGI;
my $username = $query->param("the_username");
Run Code Online (Sandbox Code Playgroud)

这似乎不起作用:

$ ./script.pl the_username=user1

编辑:

实际上以上的作品.if检查的语句$username是错误的(使用==而不是eq).

Nat*_*C-K 7

CGI从标准输入读取变量.

请参阅CGI.pm文档的这一部分:

http://search.cpan.org/dist/CGI/lib/CGI.pod#DEBUGGING


Sin*_*nür 6

正如我很久以前发现的那样,您确实可以使用CGI.pm将查询字符串参数传递给脚本.我不建议将此作为首选调试方法(最好将可复制的东西保存在文件中,然后将其导向STDIN脚本),但是,它确实有效:

#!/usr/bin/env perl

use warnings; use strict;

use CGI;

my $cgi = CGI->new;

my $param_name = 'the_username';

printf(
    "The value of '%s' is '%s'.\n",
    $param_name, $cgi->param($param_name)
);
Run Code Online (Sandbox Code Playgroud)

输出:

$ ./t.pl the_username=yadayada
The value of 'the_username' is 'yadayada'.