Testing CGI.pm-using code

K

kj

Here's the problem. I'm trying to test some code that uses CGI.pm,
running on Linux. I want to do this "locally", without involving
an HTTP server (Apache, etc.).

Each of the intended tests will result in the execution of the
following code from CGI.pm:

read(\*STDIN, $$buff, $len, $offset);

I want the input that is read by this line to come from a scalar
variable (a different one for each test). But I can't get CGI to
read different inputs each time.

The following snippet illustrates the problem:

use CGI ();

sub test {
my $in = shift;
$ENV{ REQUEST_METHOD } = 'POST';
$ENV{ CONTENT_TYPE } = 'text/plain';
$ENV{ CONTENT_LENGTH } = length $in;

close STDIN;
open STDIN, '<', \$in or die $!;

print CGI->new->param( 'POSTDATA' ), "\n";
}

test( 'foo' );
test( 'bar' );

__END__
foo
foo


As you can see, the first call to test() produces the required
output, but the second one doesn't. In fact all calls to test()
will produce the same output as the first one does.

Can anyone suggest a way in which I could, within the execution of
a single script, give CGI different inputs via STDIN?

TIA!

Kynn Jones
 
M

Mumia W.

Here's the problem. I'm trying to test some code that uses CGI.pm,
running on Linux. I want to do this "locally", without involving
an HTTP server (Apache, etc.).
[...]

Hello Kynn. Try this for your test function:

sub test {
my $cgi = CGI->new(\$_[0]);
print $cgi->param('POSTDATA'), "\n";
}

I hope that your code was not meant to test reading from STDIN because
you don't need STDIN to set up a CGI.pm object.

__HTH__
 
X

xhoster

kj said:
Here's the problem. I'm trying to test some code that uses CGI.pm,
running on Linux. I want to do this "locally", without involving
an HTTP server (Apache, etc.).

What is it you are trying to test?
Each of the intended tests will result in the execution of the
following code from CGI.pm:

read(\*STDIN, $$buff, $len, $offset);

I want the input that is read by this line to come from a scalar
variable (a different one for each test). But I can't get CGI to
read different inputs each time.

The following snippet illustrates the problem:

use CGI ();

sub test {
my $in = shift;
$ENV{ REQUEST_METHOD } = 'POST';
$ENV{ CONTENT_TYPE } = 'text/plain';
$ENV{ CONTENT_LENGTH } = length $in;

close STDIN;
open STDIN, '<', \$in or die $!;

print CGI->new->param( 'POSTDATA' ), "\n";

CGI->new can take an argument.

open my $fh, '<', \$in or die $!;
print CGI->new($fh)->param( 'POSTDATA' ), "\n";

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top