Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 267
DanielST Avatar asked Sep 13 '11 14:09

DanielST


People also ask

How do I run CGI in Perl?

Before you proceed with CGI Programming, make sure that your Web Server supports CGI functionality and it is configured to handle CGI programs. All the CGI programs to be executed by the web server are kept in a pre-configured directory. This directory is called CGI directory and by convention it is named as /cgi-bin.

What is CGI query string format in Perl?

In Perl, CGI(Common Gateway Interface) is a protocol for executing scripts via web requests. It is a set of rules and standards that define how the information is exchanged between the web server and custom scripts.


2 Answers

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'.
like image 140
Sinan Ünür Avatar answered Oct 03 '22 23:10

Sinan Ünür


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

like image 27
Nate C-K Avatar answered Oct 03 '22 23:10

Nate C-K