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

Fellow monks, I know you are all rolling your eyes and going for the -- button. But hold on. I know this is Chapter 1 stuff in any Perl book, and I should have figured this out years ago. But for some reason I have never been able to make it work. Am I'm tired of writing an HTML form every time I need some simple input.

I tried examples from Learning Perl and Programming Perl (The Cookbook uses Term::Readkey for all its examples), those found from Google searches and PM's very own Super Search--and there are hundreds of them.

When I save this insanely simple script to my server and access it from a browser, it prints the prompt and then nothing. I thought I'd have a cursor, I would type something, see what I was typing, and then hit RETURN and the results would be printed to screen.

What am I not getting?
#!/usr/bin/perl print "Content-type: text/plain\n\n"; use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; # $| = 1; print "What is your name? "; my $name= <STDIN>; chomp $name; print $name,"\n";
TIA!

—Brad
"A little yeast leavens the whole dough."

Replies are listed 'Best First'.
Re: Really, how do I get input from the keyboard.
by Mr. Muskrat (Canon) on Feb 20, 2004 at 04:09 UTC

    Perl CGI scripts are still CGI scripts. As such have a different understanding of <STDIN> (<STDOUT> and <STDERR> too). <STDIN> comes from the browser and not directly from the keyboard like with shell scripts.

    Stick with the forms (I will not recommend appending parameters to the URL) or start experiencing the true power of Perl from within a shell.

      Thanks Mr.Muskrat, I figured it was *something* like that. As a web guy only, I guess I'll go with the form. But I will read up on the shell...always learning to be done around here.

      —Brad
      "A little yeast leavens the whole dough."
Re: Really, how do I get input from the keyboard.
by davido (Cardinal) on Feb 20, 2004 at 04:09 UTC
    Are you trying to get input from a CGI textbox? In that case, you need to use an HTML form with a textbox, and then use the CGI.pm module to grab the input. There isn't a way to read directly into your CGI script as a user is typing.

    While it's true that CGI scripts get POST input from STDIN, you can't just do what you're trying to do without setting up a textbox in an HTML form. And as for reading the input, don't do it yourself, use the CGI.pm module's param() method.


    Dave

      you need to use . . . the CGI.pm module to grab the input.

      You know, I never liked param(). It's badly named IMHO. Why not call it input() or read() or received() or whatever. Is anybody with me?

        <UPDATE> Original unedited text of this node's parent preserved as follows:</UPDATE>

        There is more than one way to do it.

        Of course there is. There are thousands of ways of outputting "Just another Perl hacker.\n" too, and yet, none is easier, clearer, and more robust than:

        print "Just another Perl hacker.\n";

        When the job can be best accomplished with a hammer don't try using a heavy wrench or the heel of a shoe to drive a nail, unless of course, you're just showing off. ;)


        Dave

        I'm all over you. But I hate CGI.pm anyways, so.
Re: Really, how do I get input from the keyboard.
by Anonymous Monk on Feb 20, 2004 at 10:01 UTC
    Hi there. If you're trying to send input to a cgi program from the keywboard, you have to send in the values of the parameters that the cgi program is expecting. For example, if you have the line

    $somevariable=param('formvariable');

    in your code, then you'll have to type the following at the command line to pass values to the 'formvariable' parameter:

    % perl myprogram.cgi formvariable=test%20input

    Remember that you also have to type in the escape characters. In this case, %20 is the escaped value for a space.

    Hope this helps.

    Gorby