Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Is it possible to write to STDIN ?

by exilepanda (Friar)
on Jul 21, 2014 at 04:56 UTC ( [id://1094421]=perlquestion: print w/replies, xml ) Need Help??

exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to provide a simulation on Web request, GET and POST, during my shell development, coz I don't want to implement a batch form query combination and see the results via my browser. This is very inefficient if I use my browser to fill in the blanks, change something and submit again time to time.

My simulation module is simply to set $ENV{REQUEST_METHOD} = "GET" and then $ENV{QUERY_STRING} = "Var1=Val1&Var2=Val2" (when I run the code in shell) Now the GET request is done, but the POST request won't work as it read (STDIN, my $buf, 4096) and I can't find anyway to feed the STDIN

My recent shortcoming approach it to alter a POST request to GET and URI encode the data string, it works on some cases, however it won't pass through when performs :

if ( $ENV{REQUEST_METHOD} eq "GET" ) { print_the_form(); } elsif ( $ENV{REQUEST_METHOD} eq "POST" ) { handle_the_form(); } else { print "GET and POST request only" }
Any clues for this ? Thanks a lot!

Replies are listed 'Best First'.
Re: Is it possible to write to STDIN ? (fake CGI POST STDIN)
by Anonymous Monk on Jul 21, 2014 at 07:10 UTC
      Thanks for the suggestion, however, I think I am not going to trigger any http mechanism at all (for the sake of a slow notebook with 10K+ batch requests). I just wanted to write into STDIN (if any chance or alternative approach). Just want my main script get the form like it's from server self.

        Yes, I think both parts of my answer cover both situations :) 10K+ really ain't that many :)

Re: Is it possible to write to STDIN ?
by zentara (Archbishop) on Jul 21, 2014 at 10:33 UTC
    I'm not sure about your question regarding writing to STDIN, but does the following work for you in a POST?
    #!/usr/bin/perl -wT use strict; my $buffer; read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); print "Content-type: text/plain\n\n"; print $buffer;
    Also here is a script you may find interesting.
    #!/usr/bin/perl # Description: Takes all CGI parameters (both GET and POST data) # and builds a query string to send as a GET request. use CGI::Simple; # or use CGI; use URI::Escape; my $CGI = CGI::Simple->new; # or CGI->new; my $query = join( ';', map { uri_escape($_) . '=' . uri_escape($CGI->param($_)) } $CGI->param ); # now we can send a GET rquest somewhere $CGI->redirect('' . $query);

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks zentara, that's actually what I am up to so far, I alter a POST request to a GET request, however, like the code I stated at the last paragraph, this won't work when there's a single script that handle GET and POST in a total different manner, provided I want to leave the old codes as is. So I just hope to find a way to write into the STDIN, so the read() later can just implement like the data really comes from the web server self. Is it possible to write into STDIN anyway ?
Re: Is it possible to write to STDIN ?
by DrHyde (Prior) on Jul 21, 2014 at 14:54 UTC
    $ foo | bar

    'bar's STDIN is whatever 'foo' spits to STDOUT. So if you can control how your code is executed from the shell, you can control its STDIN. For example ...

    for i in `seq 1 10`; do echo $i|./; done
    will execute your script ten times, each time with a different number on its STDIN.
      Hey, this one is interesting and seem very likely what I am looking for... but please have a look for what I am planning to do, and could you advise can I do this inside the code rather than pipe it from the shell?
      $ENV{REQUEST_METHOD} = "POST"; open F, "testplan.txt" or die $!; while ( chomp ( my $data = <F> ) ) { ### DO ANYTHING TO MAKE $data BECOME STDIN ### # the main() script that reads and handle the form # print final result # next test plan data } close F;
        You could close STDIN and re-open it as a normal filehandle:
        close(STDIN); open(STDIN, 'foo'); print $_ foreach(<STDIN>);
        The documentation for 'open' shows how to save and restore filehandles, and some other dirty tricks you can play.
Re: Is it possible to write to STDIN ?
by RonW (Parson) on Jul 21, 2014 at 20:46 UTC

    Generically speaking, you can feed the STDIN of a child process like so:

    open my $fh, '|command' or die "Can't run command: $!"; print $fh "input for command\n"; print $fh "more input for command\n"; close $fh;

    More specific to your situation, years ago, I wrote a simple script to copy STDIN to a file, then set up Lighttp to run that script as a CGI. Then I opened a HTML file, with the form I needed to fill, in a web browser, filled in the form and submitted. I then edited (a copy of) the resulting file to create a template. with thta, I could use a script to test my target CGI by inserting various values into the template and feeding the result to the CGI script similarly to my code snippet above.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1094421]
Approved by stefbv
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-07-23 14:13 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.