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

发布于 2024-12-04 11:45:16 字数 342 浏览 1 评论 0原文

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

为了获取数据,我做了类似的事情:

my $query = new CGI;
my $username = $query->param("the_username");

这似乎不起作用:

$ ./script.pl the_username=user1

编辑:

实际上上面的方法有效。检查 $usernameif 语句是错误的(使用 == 而不是 eq)。

Normally i get the data from a webpage but i want to send it from the command line to facilitate debugging.

To get the data i do something like:

my $query = new CGI;
my $username = $query->param("the_username");

this doesn't seem to work:

$ ./script.pl the_username=user1

EDIT:

Actually the above works. The if statement that checked $username was wrong (using == instead of eq).

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

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

发布评论

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

评论(2

不醒的梦 2024-12-11 11:45:16

正如我很久以前发现的那样,您确实可以使用 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)
);

输出:

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

As I found out long time ago, you can indeed pass query string parameters to a script using CGI.pm. I am not recommending this as a preferred debugging method (better to have replicable stuff saved in files which are then directed to the STDIN of the script), however, it does work:

#!/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)
);

Output:

$ ./t.pl the_username=yadayada
The value of 'the_username' is 'yadayada'.
白况 2024-12-11 11:45:16

CGI 从标准输入读取变量。

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

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

CGI reads the variables from standard input.

See this part of the CGI.pm documentation:

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

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