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

I can't get keyboard input into the variable.

The code is the rather simple

print "What is your name? "; $name = <STDIN>; print "Your name is $name\n";

The script doesn't wait for the keyboard. Any fix for this one, or is the Mac OS completely hopeless?

Thank you, and blessings.

Replies are listed 'Best First'.
Re: STDIN under MAC OS 10.4.11
by Your Mother (Archbishop) on May 24, 2008 at 21:08 UTC

    Perl on OS X -- Yur Doing it Rong.

    moo@cassidy[238]~>perl print "What is your name? "; $name = <STDIN>; print "Your name is $name\n"; ^D What is your name? Me Your name is Me

    Though I don't know what you're doing wrong because that runs fine for me (10.4 and 10.5). You're probably not running what you think you are.

Re: STDIN under MAC OS 10.4.11
by graff (Chancellor) on May 25, 2008 at 01:13 UTC
    Nothing wrong with macosx -- it behaves like any other unix system as far as perl is concerned. So maybe if you show us more information... What is the command line you use to run the script? You showed us three lines of code... Is that all there is to the script?

    Here are some examples of shell commands that execute the three lines of perl code you showed us, and these all work for me, using command lines in a Terminal shell window (or in an xterm shell) -- I'm showing the shell prompt ("bash$ ") so that you can see which lines are command-line inputs that I typed; the lines that don't start with "bash$ " are the command outputs.

    bash$ perl -e 'print "What is your name? "; $name = <STDIN>; print "Yo +ur name is $name\n"' What is your name? Me Your name is Me bash$ cat test.perl #!/usr/bin/perl print "What is your name? "; $name = <STDIN>; print "Your name is $name\n"; bash$ chmod +x test.perl bash$ test.perl What is your name? You Your name is you
    So, what are you doing that is different from those examples?

      Thank you.

      What I've got is a file example2.pl that has this in it and nothing else

      #!/usr/bin/perl print "What is your name? "; $name = <STDIN>; print "Your name is $name\n";

      I also have a file example1.pl that has this in it and runs exactly as you'd expect

      #!/usr/bin/perl print "Hello, there!\n";

      I'm not new to Perl or to programming, but I am having to switch from MacPerl to Perl on the Mac under Unix (Darwin); although, I used to work for Sun Microsystems, so Unix isn't unfamiliar, either.

      I thought I'd just learn Perl all over again from the beginning, when I ran up against the inability of the script to stop for the keystokes. What????????

        I ran up against the inability of the script to stop for the keystokes.

        So, are you saying that it is still the case, when you run "example2.pl" as a shell command line (i.e. in a Terminal window or xterm), the script does not wait for input from the keyboard? I don't get that sort of behavior on my mac -- I stored the four-line script to a file called "example2.pl", just as you posted it, and when I run "example2.pl" as a command in a shell, it shows the prompt, waits for me to hit "return", and echos whatever I typed before hitting "return".

        If you were running the script with input redirection ( example2.pl < some.file ) or if you were piping data from some other process to the script ( other_process | example2.pl), then of course the script won't wait for input from the keyboard, because STDIN is tied to a file or a pipe, respectively. But you probably knew that already, being familiar with unix.

        Also (of course) if you type "example2.pl" at the shell prompt, and then hit "return" twice in a row, the second return will be taken as input at line 3 of the script, and it will finish, echoing an empty string.

Re: STDIN under MAC OS 10.4.11
by moritz (Cardinal) on May 24, 2008 at 21:04 UTC
    How do you start the perl script? When you start it from the console like this:
    perl your_script.pl
    the behaviour you described shouldn't happen.
Re: STDIN under MAC OS 10.4.11
by dragonchild (Archbishop) on May 24, 2008 at 21:15 UTC
    What Perl version are you running? That snippet runs fine for me under 10.4.11

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: STDIN under MAC OS 10.4.11
by Anonymous Monk on Jul 31, 2008 at 03:36 UTC
    This happened to me when i used ./ to run the file, when using perl filename.pl it worked fine.