开发者

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

开发者 https://www.devze.com 2023-04-04 18:29 出处:网络
Normally i get the data from a webpage but i want to send it from the command line to facilitate debugging.

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号