In Perl, how do I send CGI parameters on the comma

2019-04-28 11:44发布

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

2条回答
冷血范
2楼-- · 2019-04-28 12:07

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'.
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-28 12:15

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

查看更多
登录 后发表回答