In Perl, how do I send CGI parameters on the command line?

六月ゝ 毕业季﹏ 提交于 2019-12-10 04:33:15

问题


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).


回答1:


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'.



回答2:


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



来源:https://stackoverflow.com/questions/7403184/in-perl-how-do-i-send-cgi-parameters-on-the-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!