Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

STDIN refuses input

by lcole (Initiate)
on Dec 15, 2013 at 23:57 UTC ( [id://1067267]=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl -w # # =========== # realname.pl # =========== use strict; use warnings; # always use these print "content-type:text/html\n\n"; print "<html>\n"; print "<head>\n"; print "</head>\n"; print "<body>\n"; print "What is your name: "; my $input = <STDIN>; chomp $input; print "Hello, $input"; print "</body>\n"; print "</html>\n";
---

As a newbie to Perl, I grabbed 3 samples of simple Perl scripts from the web. At this point, I was more interested in knowing how to install a script on my host's site than creating a script.

One of the scripts was the popular Hello World, and another printed the time and date. So, I installed them correctly, and the first two worked correctly. Then I tried the script above and a dozen minor changes to make it work, but the script <STDIN> will not accept input. The script works(completes and prints the last message, but it does not allow me to enter any input from the keyboard. I have varied the variable names and alternated from <STDIN> to <>, but it does not wait for input.

Sorry to bother you with such a beginner problem.

lcole

Replies are listed 'Best First'.
Re: STDIN refuses input
by davido (Cardinal) on Dec 16, 2013 at 00:35 UTC

    This is a CGI script, right? I want to make sure I'm understanding your question. Are you expecting that your web browser will provide a text box? Your script doesn't create a web form.

    You should probably read Ovid's CGI Course, and the documentation for CGI, so that you have a more complete understanding of how CGI works.


    Dave

Re: STDIN refuses input
by atcroft (Abbot) on Dec 16, 2013 at 00:33 UTC

    How are you expecting to run this script? Are you expecting to access it on a web server via a browser, or are you running it on the command line?

    The problem, I believe, is "install a script on my host's site". As written, this script will work (verified) from the command line (perl scriptname.pl, or perl -T scriptname.pl, or even ./scriptname.pl). If you are trying to run it as a CGI script on a web browser, then you may need to look at using CGI or a similar module to retrieve the parameters, although I do not expect it would work in that mode as you expect.

Re: STDIN refuses input
by GrandFather (Saint) on Dec 16, 2013 at 01:07 UTC

    What do you do to run the scripts?

    It looks to me like you are running the scripts on a remote machine ("host's site"). If that is the case then your script doesn't have access to STDIN on your machine - it's a different computer!

    For playing around you really should figure out how to run the scripts on your local computer. What OS are you using locally?

    True laziness is hard work
      You could test STDIN on your local computer using CGI::Capture. Try this:
      #!perl -lw use strict; use CGI (); use CGI::Capture (); print "What is your name?"; sleep 5; SCOPE: { my $input = <STDIN>; chomp $input; my $input_ref = \$input; if ( CGI::Capture->_stdin($input_ref) ) { print "Hello, $input" or die $!; } }

        I'm not sure what you think that does, and I'm pretty sure it doesn't do what the OP wants, and I absolutely know that it's not testing STDIN in any way I think the OP is interested in. Aside from all that, I'm not the person interested in doing the testing.

        True laziness is hard work

      You are correct. I put the script in the host's CGI-BIN, and I call the script from the HTML page. There is no box. The script will print the question, "What is your name?" Then the script should accept a response to that question. A web user would type in a response, and that response would print immediately after the words, "My name is "

      Of course there is no reason for such a dumb script except to test where to put the script (CGI-BIN) and to see that the whole script works.

      It never occurred to me that THAT script would need to execute on a local computer or browser. If I ever get by this problem, I will eventually work on a Search Box in HTML and call pre-written script which I already have. I need to feel confident that I know how to modify script and where to put it.

      Looks like this test script I copied from the Web was a bad idea. It just leaves me with questions of why it didn't work. I don't really need to use it--just understand why it doesn't work.

      Thanks again.

      lcole

        Your fundamental issue is you don't know how web servers and CGI work. It takes a bit of bending your mind around for a start and even when you think you understand what's going on there are plenty of traps lying in wait for you. You will find it helpful to read the Wikipedia CGI article. Take special note of mentions of "standard output" and "standard input".

        The key thing is that your script is running in a special "box" on the server and that STDIN and STDOUT are special. Take a deep breath, then go do some reading to get an understanding of how web pages, servers and CGI all hang together.

        True laziness is hard work

        There is no box..... Then the script should accept a response to that question.

        It is correct that there is no box. It is incorrect that the script should accept a response to that question; your script does nothing of the sort. Your script reads from STDIN. Your script doesn't produce an HTML form. It should. Without that, it will never work as you want it to in a CGI environment.

        See my post earlier in this thread. If you plan to produce a CGI script, you need to know how CGI works. Currently you do not. It's not hard to gain this understanding. But you do have to do a little research.

        There's no point in us spending all day typing up a reply here that explains how CGI works, because that has already been written many times. Start with the links I provided earlier. And understand that if you don't present the user with a form, you aren't going to get a meaningful reply.

        You may have the amplifier with a mic input, but you haven't got a microphone plugged into it yet.


        Dave

Re: STDIN refuses input
by taint (Chaplain) on Dec 16, 2013 at 06:57 UTC
    Greetings, lcole.

    Like perhaps most of the responses thus far. It isn't very clear what it is you are hoping to achieve with the script you've posted. Your script, as it is, won't work. If I understand your intended implementation. If I were to guess. I'd think you wanted to simply post a clever reply to someone who has perhaps entered their name in a web page you have on your web site (hosted, not on your own computer). As such, you might well modify your script above to look something like this

    #!/usr/bin/perl -w use strict; use CGI qw/:standard/; my $input = param("input")||""; print "content-type:text/html; charset=utf-8\n\n"; print qq(<!DOCTYPE html><head></head><body> <form method="post" action="realname.pl"> <label for="input">What is your name: </label><input type="text" name="input" value="" /> <input type="submit" value="Post Input" />); if ($input) { print qq(<br />Hello, $input<br />); }else{ print qq(<br /><br />); } print qq(</form></body></html>);
    Give it a try, and see if it's what you were hoping to achieve. If it's not. I'm afraid you'll need to be a bit more specific as to your intended goal.

    Best wishes.

    --Chris

    Yes. What say about me, is true.
    

      Well, I said I am a beginner, and it certainly shows as I do not understand many of the comments. Let me try again.

      I copied three sample Perl scripts from the Web, because I wanted to know how to install them on my host's system. Two of three scripts were put into my host's CGI-BIN and worked perfectly. The other script was also installed and works to a point.

      It is a simple script and I expected it would ask for input through <STDIN> and save the input in a $variable. The script would then immediately print "Hello, $variable." I am sure that <STDIN> does not wait for input and prints the incomplete hello message without the $variable.

      From what I understand, I could achieve this test with only my local computer. That is not what I need. I got the installation correct, I think, but I am missing something. Perhaps the script I copied is meant for a local computer.

      Ultimately, I have some pre-written scripts that I want to use, and I need to know how to modify them for my site, and install them.

      What will it take to make my simple script work correctly? As soon as it works, I will go on to learn something more difficult.

      Thanks to all.

      lcole

        Greetings, lcole.

        If I understood you correctly, I've already given you pretty much what you asked for; Take input from a web page, and respond to said input.

        There are other possible variations. The way I chose to present it, was what I thought might be easiest to follow, and seemed closest to the example you presented.

        From the looks of it (your script), it seems pretty old, in style. Usually taking input from <STDIN> is chosen when you are at a console, or tty/terminal (at a local computer -- within a shell). You can still use that same process within a web page. Something like

        read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
        But it can get pretty hairy. Because you must take additional measures to "sanitize" your input. To prevent users from entering shell commands like rm -rf /. Which could effectively delete your(hosters) file system. The commands are only limited to what is available with your hosting account, or how clever the user is, that uses your (unsanitized) input. So given that you indicate that you're fairly new to Perl. I would strongly advise against taking the <STDIN> approach. At least until you become more comfortable with Perl. But by then, I suspect you'll see that's it's probably more work than it's worth. Just to say you did it with <STDIN> :)

        Hope this helped, and best wishes.

        --Chris

        Yes. What say about me, is true.
        
Re: STDIN refuses input
by choroba (Cardinal) on Dec 16, 2013 at 21:43 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found